Java画图
[code=Java][/code]import java.awt.*;
import java.awt.event.*;
public class DrawRectTest2 extends Frame implements Runnable{
private Color[] colors={Color.red,Color.blue,Color.green,Color.orange};
private Color color;
int count1=0;
int count2=0;
int count3=0;
Thread t=new Thread(this);
Boolean bool=true;
int x=0,y=0,lengsize=100,widthsize=100;
private int[][] sizes={{50,50},{170,50},{50,170},{170,170}};
public DrawRectTest2(String title){
super(title);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent event){
if(bool==true){
bool=false;
t.interrupt();
}
else{
bool=true;//如何恢复
t.run();
}
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setSize(300,300);
setVisible(true);
t.start();
}
public void run(){
while(bool){
color=colors[count1++%colors.length];
x=sizes[count2++%4][0];
y=sizes[count3++%4][1];
repaint();
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g){
g.setColor(color);
g.drawRect(x,y,lengsize,widthsize);
}
public static void main(String args[])throws Exception{
new DrawRectTest2("hello");
}
}
我想在窗体上点击鼠标时停止画图,在点一次接着画图。但是代码不知道如何实现。上面是我写的一些代码。
------解决方案--------------------已经实现你的要求,但应该还有其他方法
Java code
import java.awt.*;
import java.awt.event.*;
public class DrawRectTest2 extends Frame implements Runnable{
private Color[] colors={Color.red,Color.blue,Color.green,Color.orange};
private Color color;
int count1=0;
int count2=0;
int count3=0;
Thread t=new Thread(this);
Boolean bool=true;
int x=0,y=0,lengsize=100,widthsize=100;
private int[][] sizes={{50,50},{170,50},{50,170},{170,170}};
public DrawRectTest2(String title){
super(title);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent event){
if(bool==true){
bool=false;
//t.interrupt();
}
else{
bool=true;//如何恢复
//t.run();
}
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setSize(300,300);
setVisible(true);
t.start();
}
public void run(){
while(true){
while(bool);
color=colors[count1++%colors.length];
x=sizes[count2++%4][0];
y=sizes[count3++%4][1];
repaint();
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g){
g.setColor(color);
g.drawRect(x,y,lengsize,widthsize);
}
public static void main(String args[])throws Exception{
new DrawRectTest2("hello");
}
}