Hello, I am trying to write a GUI program for my computer class which requires us to create a superclass containing basic data for creating lines, rectangles, and ovals, subclasses of that superclass which have methods specific to each, a class that draws a randomized amount of each shape (and also randomizes the color and whether or not it is filled in for rectangles and ovals), and finally one that ties it all together (the test class). Our teacher gave us code to start from, which contained the superclass and Line subclass. In it was included two toString methods, the main(supeclass) one being:
public String toString () { return String.format ("x1 = %d...", x1...); }
and the subclass toString method:
public String toString (int count) { return String.format ("Line: %d", super.toString()); }
The first one has ellipses because he intended us to be able to figure it out, which I followed up by adding in "x2 = %d", so on and so forth. From what I understand, the toString in the superclass is just there to be there essentially, as it will get overridden by the subclasses. Beyond that, I have no idea of how I would go about making a count of how many of each shape appears. Here is the rest of my code in totality...
package project3;
import java.awt.*; public class MyShape { private int x1, x2, y1, y2; private Color myColor; public MyShape (int x1, int x2, int y1, int y2, Color myColor) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.myColor = myColor; } public void setx1 (int x1) { this.x1 = x1; } public int getx1 () { return x1; } public void setx2 (int x2) { this.x2 = x2; } public int getx2 () { return x2; } public void sety1 (int y1) { this.y1 = y1; } public int gety1 () { return y1; } public void sety2 (int y2) { this.y2 = y2; } public int gety2 () { return y2; } public void setColor (Color myColor) { this.myColor = myColor; } public Color getColor () { return myColor; } public String toString () { return String.format ("x1 = %d x2 = %d y1 = %d y2 = %d", x1, x2, y1, y2); } }
package project3; import java.awt.*; public class MyLine extends MyShape { int count = 0; public MyLine (int x1, int y1, int x2, int y2, Color myColor) { super (x1, y1, x2, y2, myColor); count++; } public String toString (int count) { return String.format ("Line: %d", super.toString()); } public void draw (Graphics g) { g.setColor (getColor()); g.drawLine (getx1(), gety1(), getx2(), gety2()); } }
package project3; import java.awt.*; public class MyRectangle extends MyShape { private boolean fill; public MyRectangle (int x1, int x2, int y1, int y2, Color myColor, boolean fill) { super (x1, x2, y1, y2, myColor); this.fill = fill; } public int getUpperLeftX () { return Math.min (getx1(), getx2()); } public int getUpperLeftY () { return Math.min (gety1(), gety2()); } public int getWidth () { return Math.abs (getx2() - getx1()); } public int getHeight () { return Math.abs (gety2() - gety1()); } public String toString () { return String.format ("Rectangle: %s", super.toString()); } public void draw (Graphics g) { g.setColor (getColor()); if (fill) g.fillRect (getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); else g.drawRect (getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); } }
package project3; import java.awt.*; public class MyOval extends MyShape { private boolean fill; public MyOval (int x1, int x2, int y1, int y2, Color myColor, boolean fill) { super (x1, x2, y1, y2, myColor); this.fill = fill; } public int getUpperLeftX () { return Math.min (getx1(), getx2()); } public int getUpperLeftY () { return Math.min (gety1(), gety2()); } public int getWidth () { return Math.abs (getx2() - getx1()); } public int getHeight () { return Math.abs (gety2() - gety1()); } public String toString () { return String.format ("Oval: %s", super.toString()); } public void draw (Graphics g) { g.setColor (getColor()); if (fill) g.fillOval (getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); else g.drawOval (getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); } }
package project3; import java.awt.*; import java.util.Random; import javax.swing.JPanel; public class DrawPanel extends JPanel { private Random randomNumbers = new Random(); private MyLine lines[]; private MyRectangle rects[]; private MyOval ovals[]; static final long serialVersionUID = 0; public DrawPanel() { 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; setBackground (Color.WHITE); lines = new MyLine [5 + randomNumbers.nextInt (5)]; rects = new MyRectangle [5 + randomNumbers.nextInt (5)]; ovals = new MyOval [5 + randomNumbers.nextInt(5)]; for (int count = 0; count < lines.length; count++) { x1 = randomNumbers.nextInt (WINDOW_WIDTH); y1 = randomNumbers.nextInt (WINDOW_HEIGHT); x2 = randomNumbers.nextInt (WINDOW_WIDTH); y2 = randomNumbers.nextInt (WINDOW_HEIGHT); color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt (NUMBER_COLORS)); lines [count] = new MyLine (x1, y1, x2, y2, color); } for (int count = 0; count < rects.length; count++) { x1 = randomNumbers.nextInt (WINDOW_WIDTH); y1 = randomNumbers.nextInt (WINDOW_HEIGHT); x2 = randomNumbers.nextInt (WINDOW_WIDTH); y2 = randomNumbers.nextInt (WINDOW_HEIGHT); color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt (NUMBER_COLORS)); fill = randomNumbers.nextBoolean (); rects [count] = new MyRectangle (x1, y1, x2, y2, color, fill); } for (int count = 0; count < ovals.length; count++) { x1 = randomNumbers.nextInt (WINDOW_WIDTH); y1 = randomNumbers.nextInt (WINDOW_HEIGHT); x2 = randomNumbers.nextInt (WINDOW_WIDTH); y2 = randomNumbers.nextInt (WINDOW_HEIGHT); color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt (NUMBER_COLORS)); fill = randomNumbers.nextBoolean (); ovals [count] = new MyOval (x1, y1, x2, y2, color, fill); } } public String statusText() { //this would be the status text that is displayed at the bottom of the JPanel window, but I don't know //how to implement it correctly, this is the problem return String.format (lines.toString(), rects.toString(), ovals.toString()); } public void paintComponent (Graphics g) { super.paintComponent (g); for (MyLine line : lines) line.draw (g); for (MyRectangle rect : rects) rect.draw (g); for (MyOval oval : ovals) oval.draw(g); } }
package project3; import javax.swing.*; import java.awt.BorderLayout; public class TestDraw { public static void main(String[] args) { JLabel south = new JLabel(); final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300; DrawPanel panel = new DrawPanel (); JFrame application = new JFrame (); application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); application.add(south, BorderLayout.SOUTH); application.add (panel); application.setSize (WINDOW_WIDTH, WINDOW_HEIGHT); application.setVisible (true); } }
The official prompt for this is the following at the bottom of the page, with a visual representation of how it should look when the program runs:
http://kvazar-micro.zp.ua:18888/Java...9lev1sec8.html
I am just seeking some direction or a push in the right direction, I don't want or expect you all to give me the answer, I know how programming experts feel about people just posting to do that. :] Please help any way you can, thank you!
PS: Everything else works like a charm, it's just figuring out how to get counts for how many objects are created.