Движущийся графический объект - Java
Формулировка задачи:
Используя технологию объектно-ориентированного программирования разработать два варианта программы, реализующей движущийся графический объект в соответствии с индивидуальным заданием:
с использованием статического объекта;
с использованием динамического объекта.
Движение закрашенного прямоугольника по прямоугольному контуру.
Решение задачи: «Движущийся графический объект»
textual
Листинг программы
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package графика;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Графика extends JFrame{
private final int Height=500;
private final int Width=500;
public Графика() {
super("simpleApp");
setSize(Height, Width);
setVisible(true);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paint ( Graphics g ) {
Graphics2D graph = (Graphics2D) g;
Color Color1 = new Color(185,34,25);
int x=0,y=0, r=100;
int step = 40, Rad = 500;
graph.setStroke(new BasicStroke(4));
graph.setPaint(new Color(240,240,240));
graph.fillRect(0, 0, Height, Width);
int i=0;
x =200;
y =200;
while (i<100) {
i++;
if (x==200 && y==200){
while (x<300)
x+=i;
}
if (x==300 && y==200){
while (y<300)
y+=i;
}
if (x==300 && y==300){
while (x>200)
x-=i;
}
if (x==200 && y==300){
while (x>200)
y-=i;
}
graph.setPaint(new Color(240, 240, 240));
graph.fillRect(0, 0, Height, Width);
graph.setPaint(Color1);
g.fillRect(/*x*/ x,/*у*/y,/*ширина*/20,/*высота*/10);
try {
Thread.sleep(step);
} catch (Exception e) {
}
}
}
public static void main(String args[]) {
Графика app = new Графика();
}
}