package drawgraphics;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.Event;
import java.util.*;
public class DrawGraphics extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
public DrawGraphics() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
this.setTitle("随机画图");
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(400, 400);
this.setLocation((screen.width - 400) / 2, (screen.height - 400) / 2);
this.setVisible(true);
this.setResizable(false);
this.setVisible(true);
this.setBackground(Color.white);
}
public static void main(String[] args) {
DrawGraphics drawgraphics = new DrawGraphics();
Container container = drawgraphics.getContentPane();
myThread mt1 =new myThread(container,0);
myThread mt2 = new myThread(container,1);
myThread mt3 = new myThread(container,2);
myThread mt4 = new myThread(container,3);
mt1.start();
mt2.start();
mt3.start();
mt4.start();
}
}
class myThread extends Thread {
Container container;
int flags = 0;
myThread(Container container, int flags) {
this.container = container;
this.flags = flags;
}
public void draw() {
int x, y, w, h,ww;
Color c;
Graphics2D g = (Graphics2D) container.getGraphics();
w = container.getWidth();
h = container.getHeight();
x = (int)(Math.random() * w);
y = (int) (Math.random() * h);
ww = (int) (Math.random() * (w > h ? h : w)/3) + 20;
c = new Color((int)( Math.random() * 256), (int)( Math.random() * 256),
(int)( Math.random() * 256));
g.setColor(c);
if (flags == 0) {
g.fillOval(x, y, ww, ww);
} else if(flags == 1){
g.fillRect(x, y, ww, ww);
}else if(flags == 2)
{
g.fillArc(x,y,ww,ww,45,160);
}else
{
g.fill3DRect(x,y,ww,ww,true);
}
}
public void run() {
while (true) {
draw();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}