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.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),
            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.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) {

        for (MyRect2 rectangle : rects)

        for (MyOval2 oval : ovals)

        for (MyLine2 line : lines)

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());
        Border border = BorderFactory.createLineBorder(Color.DARK_GRAY);

        panel.add(label, BorderLayout.SOUTH);
        panel.add(label2, BorderLayout.NORTH);
        application.setSize(500, 500);


i know its along these lines but not to sure what to do next!


If you have numbers to display in a JLabel there are two steps:

  1. create a String that contains the info you want to display by combining descriptive text with the actual numbers
  2. Use the JLabel's setText method to display the String in the JLabel


String textToDisplay = "I have " + numPhones + " telephones and " + numTVs + " televisions";

of course you can do both in one statement, eg

myLabel.setText("I have " + numPhones + " telephones and " + numTVs + " televisions");

You can use String.format to create the string if you need special formatting, but in this case it seems unecessary. Anyway, here's how it works with trivial (decimal number) formatting. There are lots of examples and tutorials on the web if you want to specify formats in more detail.

myLabel.setText(String.format("I have %d telephones and %d televisions", numPhones, numTVs));

Edited 2 Weeks Ago by JamesCherrill