i am new in java and wanna to draw some random size & color rectangles concurrently. I use Jframe and Canvas to present the output of my threads. And awt to draw my rectangles. I Also making my class to implements Runnable to make it multi-threading;
What my code runs just shows the output of one thread(which draws 10 random rectangles here). And it seems like two thread are not showing their output in the same canvas.(otherwise it will be 20) And since my canvas is called in main class first and then two threads been called, so i don't know how to make the canvas globally to let both two threads draw on it.
how to solve this problem?
(there are many useless api imported just in case)
import java.awt.Graphics;
import java.awt.image.*;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.io.*;
import javax.imageio.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw1 extends Canvas implements Runnable {
//set thread attributes
private Thread t;
public static void main(String[] args) {
JFrame frame = new JFrame("my test draw");
Canvas canvas = new Draw1();
frame.setSize(400, 400);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
Draw1 R1 = new Draw1();
R1.start();
Draw1 R2 = new Draw1();
R2.start();
}
public void paint(Graphics g) {
// DrawRect(int x, int y, int width, int height, Style style)
// Returns DrawRect with given rectangle.
//each thread draw 10 rects
for(int i = 10; i>0; i--)
{
//ramdom color
int c1 = (int) (Math.random() * ((255 - 0) + 1)) + 0;
int c2 = (int) (Math.random() * ((255 - 0) + 1)) + 0;
int c3 = (int) (Math.random() * ((255 - 0) + 1)) + 0;
g.setColor(new Color(c1, c2, c3));
//set the x,y position and height,width
int x = (int) (Math.random() * ((400 - 0) + 1)) + 0;
int y = (int) (Math.random() * ((400 - 0) + 1)) + 0;
int w = (int) (Math.random() * ((100 - 0) + 1)) + 0;
int h = (int) (Math.random() * ((100 - 0) + 1)) + 0;
g.drawRect(x, y, w, h);
}
}
@Override
public void run() {
System.out.println("Running " );
try {
repaint();
} catch (Exception e) {
System.out.println("Thread interrupted.");
}
System.out.println("Thread exiting.");
}
public void start() {
System.out.println("Starting ");
if (t == null) {
t = new Thread(this);
t.start();
}
}
}