Hey guys! Working on a project that I am stuck on at the moment! Any help would be really appreciated! I need to get my JLabel to display a count of the three differnet shapes my RandomShapesGenerator program. I know I need to use a String.format but just dont know how to go about it!
public class DrawPanel2 extends JPanel {
private Random randomNumbers = new Random();
private MyRect2[] rects;
private MyOval2[] ovals;
private MyLine2[] lines;
private BorderLayout layout;
private JLabel label;
private JLabel label2;
public DrawPanel2() {
this.setBackground(Color.WHITE);
this.lines = new MyLine2[4 + this.randomNumbers.nextInt(4)];
this.ovals = new MyOval2[4 + this.randomNumbers.nextInt(4)];
this.rects = new MyRect2[4 + this.randomNumbers.nextInt(4)];
layout = new BorderLayout(5, 5);
setLayout(new BorderLayout());
for (int count = 0; count < this.lines.length; ++count) {
int x1 = this.randomNumbers.nextInt(300);
int y1 = this.randomNumbers.nextInt(300);
int x2 = this.randomNumbers.nextInt(300);
int y2 = this.randomNumbers.nextInt(300);
Color color = new Color(this.randomNumbers.nextInt(256), this.randomNumbers.nextInt(256),
this.randomNumbers.nextInt(256));
boolean filled = this.randomNumbers.nextBoolean();
this.lines[count] = new MyLine2(x1, y1, x2, y2, color);
for (int countR = 0; countR < this.rects.length; ++countR) {
int x1R = this.randomNumbers.nextInt(300);
int y1R = this.randomNumbers.nextInt(300);
int x2R = this.randomNumbers.nextInt(300);
int y2R = this.randomNumbers.nextInt(300);
Color colorR = new Color(this.randomNumbers.nextInt(256), this.randomNumbers.nextInt(256),
this.randomNumbers.nextInt(256));
this.rects[countR] = new MyRect2(x1R, y1R, x2R, y2R, colorR, filled);
for (int countO = 0; countO < this.ovals.length; ++countO) {
int x1O = this.randomNumbers.nextInt(300);
int y1O = this.randomNumbers.nextInt(300);
int x2O = this.randomNumbers.nextInt(300);
int y2O = this.randomNumbers.nextInt(300);
Color colorO = new Color(this.randomNumbers.nextInt(256), this.randomNumbers.nextInt
(256), this.randomNumbers.nextInt(256));
this.ovals[countO] = new MyOval2(x1O, y1O, x2O, y2O, colorO, filled);
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (MyRect2 rectangle : rects)
rectangle.draw(g);
for (MyOval2 oval : ovals)
oval.draw(g);
for (MyLine2 line : lines)
line.draw(g);
}
}
public class TestDraw2 {
public static void main(String[] args) {
DrawPanel2 panel = new DrawPanel2();
JFrame application = new JFrame();
JLabel label = new JLabel(panel.toString());
JLabel label2 = new JLabel(panel.toString());
label.setForeground(Color.BLACK);
label2.setForeground(Color.BLACK);
Border border = BorderFactory.createLineBorder(Color.DARK_GRAY);
label.setBorder(border);
label2.setBorder(border);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(label, BorderLayout.SOUTH);
panel.add(label2, BorderLayout.NORTH);
application.add(panel);
application.setSize(500, 500);
application.setVisible(true);
}
}