// LineArt.java
import java.awt.*;
import javax.swing.*;
class LineArt {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Line Art");
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable {
private Ball ball1;
private Ball ball2;
public MyPanel() {
setBackground(Color.white);
ball1 = new Ball(100,100,10,5,0,0,630,450);
ball2 = new Ball(200,100,5,10,0,0,630,450);
Thread refresh = new Thread(this);
refresh.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ball1.forward();
ball2.forward();
g.setColor(Color.red);
g.drawLine(ball1.getX(), ball1.getY(), ball2.getX(), ball2.getY());
//g.fillOval(ball1.getX(), ball1.getY(), 10, 10 );
//g.setColor(Color.blue);
//g.fillOval(ball2.getX(), ball2.getY(), 10, 10 );
}
public void run() {
while(true) {
repaint();
try {
Thread.sleep(20);
//Thread.sleep(50);
}
catch(Exception e) {
}
}
}
}
class Ball {
private int x;
private int y;
private int vx;
private int vy;
private int left;
private int right;
private int top;
private int bottom;
public Ball(int x, int y, int vx, int vy, int l, int t, int r, int b) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
right = r;
left = l;
top = t;
bottom = b;
}
public void forward() {
x = x + vx;
y = y + vy;
if (x < left || x > right) {
vx = -vx;
}
if (y < top || y > bottom) {
vy = -vy;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}