会变色的文字
public class paint extends JFrame{
public paint(){
setLocation(0, 0);
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container con=getContentPane();
// con.setLayout(new BorderLayout());
draw dra=new draw();
con.add(dra);
// con.add(new button(),BorderLayout.SOUTH);
}
public static void main(String args[]){
paint pai=new paint();
Container con=pai.getContentPane();
// con.setLayout(new BorderLayout());
draw dra=new draw();
con.add(dra);
pai.setVisible(true);
Thread th=new Thread(dra);
th.start();
}
}
class draw extends JPanel implements Runnable{
Color color;
@Override
public void paint(Graphics g) {
// TODO 自动生成的方法存根
Graphics2D g2=(Graphics2D)g;
int x=100;
int y=100;
String str=new String("mafeng");
Font font =new Font("华文行楷",Font.BOLD,72);
g2.setFont(font);
g2.setColor(color);
g2.drawString(str, x, y);
System.out.println("456");
}
public void run(){
Random ran=new Random();
while(true){
int r=ran.nextInt(256);
int g=ran.nextInt(256);
int b=ran.nextInt(256);
color=new Color(r, g, b);
repaint();
System.out.println("123");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}