Hello, i need help on how to create a JLabel as a status bar that display count representing the number of each shape display
in addition i need to create a class called MyShape and make it a superclass

this my code for for al shapes :

package Lab8;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {

	// declare instance variables
	private Random randomNumbers = new Random ();
	private MyLine lines [];
	private MyRectangle rects [];
	static final long serialVersionUID = 0;

	// the constructor
	public DrawPanel () {
		// declare local variables
		final int NUMBER_COLORS = 256;	// (0-255 color values)
		final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
		int x1, y1, x2, y2;
		Color color;
		boolean fill;
		
		
		// set background color
		setBackground (Color.WHITE);
		
		// allocate the arrays  (5 to 9 references)
		lines = new MyLine [5 + randomNumbers.nextInt (5)];
		rects = new MyRectangle [5 + randomNumbers.nextInt (5)];
		
		// create the MyLine objects
		for (int count = 0; count < lines.length; count++) {
			// generate random coordinates (0 to 299)
			x1 = randomNumbers.nextInt (WINDOW_WIDTH);
			y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
			x2 = randomNumbers.nextInt (WINDOW_WIDTH);
			y2 = randomNumbers.nextInt (WINDOW_HEIGHT);
			
			// generate a random color
			color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
										randomNumbers.nextInt (NUMBER_COLORS),
											randomNumbers.nextInt (NUMBER_COLORS));
			
			// construct a MyLine object
			lines [count] = new MyLine (x1, y1, x2, y2, color);
		}  // end for loop to create MyLine objects

		// create the MyRectangle objects
		for (int count = 0; count < rects.length; count++) {
			// generate random coordinates (0 to 299)
			x1 = randomNumbers.nextInt (WINDOW_WIDTH);
			y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
			x2 = randomNumbers.nextInt (WINDOW_WIDTH);
			y2 = randomNumbers.nextInt (WINDOW_HEIGHT);
			
			// generate a random color
			color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
										randomNumbers.nextInt (NUMBER_COLORS),
											randomNumbers.nextInt (NUMBER_COLORS));
			fill = randomNumbers.nextBoolean ();
			
			// construct a MyRectangle object
			rects [count] = new MyRectangle (x1, y1, x2, y2, color, fill);
		}  // end for loop to create MyRectangle objects

	}  // end drawPanel constructor
	
	
	// when time to paint the panel, draw the lines, rectangles, and ovals
	public void paintComponent (Graphics g) {
		// paint the parent first
		super.paintComponent (g);
		
		// draw the lines, rectangles, and ovals using the enumerated for statement
		for (MyLine line : lines) 
			line.draw (g);
		for (MyRectangle rect : rects)
			rect.draw (g);
	}  // end method paintComponent

}  // end class DrawPanel
package Lab8;


import java.awt.Graphics;
import java.awt.Color;
	// declare instance variables
public class MyLine {
	private int x1, y1, x2, y2;
	private Color myColor;
	
	// the constructors
	public MyLine () {
		// the default
		this.x1 = 0;
		this.y1 = 0;
		this.x2 = 0;
		this.y2 = 0;
		this.myColor = Color.BLACK;	
	}  // end default constructor
	public MyLine (int x1, int y1, int x2, int y2, Color color) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.myColor = color;
	}  // end constructor
	
	// sets/gets
	public void setX1 (int x1) {
		this.x1 = x1;
	}
	public void setY1 (int y1) {
		this.y1 = y1;
	}
	public void setX2 (int x2) {
		this.x2 = x2;
	}
	public void setY2 (int y2) {
		this.y2 = y2;
	}
	public void setColor (Color color) {
		this.myColor = color;
	}
	public int getX1 () {
		return x1;
	}
	public int getY1 () {
		return y1;
	}
	public int getX2 () {
		return x2;
	}
	public int getY2 () {
		return y2;
	}
	public Color getColor () {
		return myColor;
	}

	// draw the shape
	public void draw (Graphics g) {
		g.setColor (myColor);
		g.drawLine (x1, y1, x2, y2);
	}  // end method draw
	
}  // end class MyLine
package Lab8;
import java.awt.Color;
import java.awt.Graphics;
public class MyOval {
	// declare instance variables
	private int x1, y1, x2, y2;
	private Color myColor;
	private boolean fill;		// to fill or not
	
	// the constructors
	public MyOval () {
		// the default
		this.x1 = 0;
		this.y1 = 0;
		this.x2 = 0;
		this.y2 = 0;
		this.myColor = Color.BLACK;
		this.fill = false;
	}  // end default constructor
	
	public MyOval (int x1, int y1, int x2, int y2, Color color, boolean fill) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.myColor = color;
		this.fill = fill;
	}  // end constructor
	
	// sets/gets
	public void setX1 (int x1) {
		this.x1 = x1;
	}
	public void setY1 (int y1) {
		this.y1 = y1;
	}
	public void setX2 (int x2) {
		this.x2 = x2;
	}
	public void setY2 (int y2) {
		this.y2 = y2;
	}
	public void setColor (Color color) {
		this.myColor = color;
	}
	public void setFill (boolean fill) {
		this.fill = fill;
	}
	public int getX1 () {
		return x1;
	}
	public int getY1 () {
		return y1;
	}
	public int getX2 () {
		return x2;
	}
	public int getY2 () {
		return y2;
	}
	public Color getColor () {
		return myColor;
	}
	public boolean getFill () {
		return fill;
	}
	
	// now the gets for calculated values
	public int getUpperLeftX () {
		// defined as the minimum of x1 and x2
		return Math.min (x1, x2);
	}
	public int getUpperLeftY () {
		// defined as the minimum of y1 and y2
		return Math.min (y1, y2);
	}
	public int getWidth () {
		return Math.abs (x1 - x2);
	}
	public int getHeight () {
		return Math.abs (y1 - y2);
	}

	// draw the shape using calculated values
	public void draw (Graphics g) {
		g.setColor (myColor);
		
		// draw the right type of shape
		if (fill)
			g.fillOval(getUpperLeftX (), getUpperLeftY (),
												getWidth (), getHeight ());
		else
			g.drawOval (getUpperLeftX (), getUpperLeftY (),
												getWidth (), getHeight ());
	}  // end method draw
	

}
package Lab8;

import java.awt.Graphics;
import java.awt.Color;

public class MyRectangle {
		// declare instance variables
		private int x1, y1, x2, y2;
		private Color myColor;
		private boolean fill;		// to fill or not
		
		// the constructors
		public MyRectangle () {
			// the default
			this.x1 = 0;
			this.y1 = 0;
			this.x2 = 0;
			this.y2 = 0;
			this.myColor = Color.BLACK;
			this.fill = false;
		}  // end default constructor
		
		public MyRectangle (int x1, int y1, int x2, int y2, Color color, boolean fill) {
			this.x1 = x1;
			this.y1 = y1;
			this.x2 = x2;
			this.y2 = y2;
			this.myColor = color;
			this.fill = fill;
		}  // end constructor
		
		// sets/gets
		public void setX1 (int x1) {
			this.x1 = x1;
		}
		public void setY1 (int y1) {
			this.y1 = y1;
		}
		public void setX2 (int x2) {
			this.x2 = x2;
		}
		public void setY2 (int y2) {
			this.y2 = y2;
		}
		public void setColor (Color color) {
			this.myColor = color;
		}
		public void setFill (boolean fill) {
			this.fill = fill;
		}
		public int getX1 () {
			return x1;
		}
		public int getY1 () {
			return y1;
		}
		public int getX2 () {
			return x2;
		}
		public int getY2 () {
			return y2;
		}
		public Color getColor () {
			return myColor;
		}
		public boolean getFill () {
			return fill;
		}
		
		// now the gets for calculated values
		public int getUpperLeftX () {
			// defined as the minimum of x1 and x2
			return Math.min (x1, x2);
		}
		public int getUpperLeftY () {
			// defined as the minimum of y1 and y2
			return Math.min (y1, y2);
		}
		public int getWidth () {
			return Math.abs (x1 - x2);
		}
		public int getHeight () {
			return Math.abs (y1 - y2);
		}

		// draw the shape using calculated values
		public void draw (Graphics g) {
			g.setColor (myColor);
			
			// draw the right type of shape
			if (fill)
				g.fillRect(getUpperLeftX (), getUpperLeftY (),
													getWidth (), getHeight ());
			else
				g.drawRect (getUpperLeftX (), getUpperLeftY (),
													getWidth (), getHeight ());
		}  // end method draw
		
}  // end class MyRectangle
package Lab8;
import javax.swing.*;
public class TestDraw {

	
	public static void main(String[] args) {
		// declare local variables/objects
		final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
		DrawPanel panel = new DrawPanel ();	// call constructor creating MyLine objects
		JFrame application = new JFrame ();	// the window and its components
		
		application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		application.add (panel);
		application.setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
		application.setVisible (true);		// show the window
		

	}

}

Recommended Answers

All 8 Replies

thanks for that

my question is how do i connect the MyOval class with the rest of the classes so it would count the rectangles and lines ?

same as other class

to what class as example i need refer to ?

part of my instruction are to create no argument constructor , and a constructor that takes the argument necessary to initialize its instance variables
and a to string method that return a string representation of my shape object

these (both draw) clases which author already added to DrawPanel

Thats the requirements
i didn't get your last explanation i didn't figure it out

which part(s) of requirments you did't ....

do i need to write a new class? to modify the variables in the draw panel or do i need to write it in the draw panel itself

i am international student so sometimes the english is little hard to understand i am a visual learner if i see code i understand it immediately

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.