사각형 크기 줄였다 컸다하는거
package ex;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPanel1234 extends JPanel implements Runnable{
Color color;
int x=0,y=0,sx=5,sy=5;
public MyPanel1234(){
new Thread(this).start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);;
g.setColor(Color.RED);
if(x < -300 || x> 0){ sx=-sx;}
if(y < -300 || y>0) sy=-sy;
System.out.println("x: "+x+"sx: "+sx);
g.fillRect(500,500,x=x-sx,y=x-sy);
}
public void run() {
while(true){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}this.repaint();
}
}
}
public class RectTest1 extends JFrame{
public RectTest1(){
this.setTitle("zz");
this.setSize(700,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel1234 mp = new MyPanel1234();
add(mp);
this.setVisible(true);
}
public static void main(String[] args)
{
new RectTest1();
}
}