i am having a problem to write the code for a project that i have
i need to get the label to the picture attached
to count how many oval and rectangle are in the picture
i have the code for the rest i just cant figure how to arrange the code

Edited 5 Years Ago by shyla: n/a

Attachments Screen_shot_2011-04-16_at_2.17_.39_PM_.png 91.51 KB

thats what i have i will appreciate any help

i need to make MyShape class and make it superclass

//DrawPanel.java - implementation of Fig. 8.22 for Lab #8.
//Mike Qualls
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
//MyLine.java - implementation of Fig. 8.21 for Lab #8
//Mike Qualls


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
	

}
//MyRectangle.java - implementation of MyRectangle objects - lab #8.
//Mike Qualls
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 = 400, WINDOW_HEIGHT = 400;
		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
		

	}

}

Edited 5 Years Ago by shyla: n/a

I'm confused. Do you need help getting the counts, or displaying them in a label, or adding a label to the bottom of your window, or creating a superclass???

@ JamesCherrill why did you confused, he/she started a new topic like as yesterday ...

1/ add MyOval
2/ count elements
3/ add JLabel to SOUTH (that's homework requirements, attached to previous thread)
4/ display counts in JLabel

sorry about that :
i need to create a class named MyShape, and make it a super class. its instance variables should be the x1 x2 y1 y2 values shared by all subclasses.
the class needs to include
1. a no argument constructor
2. constructor that takes the argument necessary to initialize its instance variables
3.sets and gets for all instance variables
4. a toString() method that generates and return a string representation of my shape object
make myLine MyRectangle MyOval subclasses of MyShape delete all instance variables and method posible that are in those classes

if you can help me its for a project i need to present at school

OK, that seems clear enough. No-one here is going to do it for you. Have a go, see how you get on, come back here with specific questions if you get stuck.

i cannot see the label

package Lab8;
import java.awt.Color;

public class MyShape 
{


	private int x1, y1, x2, y2;
    private Color myColor;
    public MyShape()
    {
        setX1(1);
        setY1(1);
        setX2(1);
        setY2(1);
        setMyColor(Color.BLACK);
    }
    public MyShape(int x1, int y1, int x2, int y2, Color myColor)
    {
        setX1(x1);
        setY1(y1);
        setX2(x2);
        setY2(y2);
        setMyColor(myColor);
    }
    public void setX1(int x1)
    {
        if(x1 >= 0 && x1 <= 300)
        {
                this.x1 = x1;
        }
        else
        {
                this.x1 = 0;
        }
    }
    public int getX1()
    {
        return x1;
    }
    public void setY1(int y1)
    {
        if(y1 >= 0 && y1 <= 300)
        {
                this.y1 = y1;
        }
        else
        {
                this.y1 = 0;
        }
    }
    public int getY1()
    {
        return y1;
    }
    public void setX2(int x2)
    {
        if(x2 >= 0 && x2 <= 300)
        {
                this.x2 = x2;
        }
        else
        {
                this.x2 = 0;
        }
    }
    public int getX2()
    {
        return x2;
    }
    public void setY2(int y2)
    {
        if(y2 >= 0 && y2 <= 300)
        {
                this.y2 = y2;
        }
        else
        {
                this.y2 = 0;
        }
    }
    public int getY2()
    {
        return y2;
    }
    public void setMyColor(Color myColor)
    {
        this.myColor = myColor;
    }
    public Color getMyColor()
    {
        return myColor;
    }
    public String toString()
    {
        return String.format("X1: %d, X2: %d, Y1: %d, Y2: %d, Color: %s", getX1(), getX2(),
                        getY1(), getY2(), getMyColor());
    }
	
	
	
	
	
	
	
}

What label? Where is the code that declares, initialises, and updates the label. Where is the coed that adds the label to some visible window?

What label? Where is the code that declares, initialises, and updates the label. Where is the coed that adds the label to some visible window?

what do i need to add to i follow the instruction did i?
i need only that the picture that i add will show up

Attachments Screen_shot_2011-04-17_at_12.46_.36_PM_.png 14.95 KB

i did able to show the label but how do i count them

package Lab8;
//import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;


public class TestDraw extends MyShape {

	
	public static void main(String[] args) {
		// declare local variables/objects
		final int WINDOW_WIDTH = 400, WINDOW_HEIGHT = 400;
		DrawPanel panel = new DrawPanel ();	// call constructor creating MyLine objects
		JFrame application = new JFrame ();	// the window and its components
		TestDraw testDraw = new TestDraw();
	    JLabel southLabel = new JLabel(testDraw.toString());

		application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		application.add (panel);
		application.setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
		application.setVisible (true);		// show the window
        application.add(southLabel, BorderLayout.SOUTH);
        application.setVisible (true);	
       
	}

}
Attachments Screen_shot_2011-04-17_at_12.46_.36_PM_.png 14.95 KB Screen_shot_2011-04-17_at_2.52_.34_PM_.png 30.87 KB

Way back you had this code:

for (MyLine line : lines)
line.draw (g);
for (MyRectangle rect : rects)
rect.draw (g);

You can use the same loop constructs to count the number of lines & rects.

Another approach is to have a static int (eg "instanceCounter")in each of your subclasses, with a static accessor method to get its value. Then in the constructor you can just increment the int each time an instance is created. This is a common approach to keeping track of how many instances of a given class there are.

i need to modify the code that you wrote in the my shape class ??

No, I didn't suggest changing anything.
You asked "how do i count them?"
I wanted you to think about the code you already have that loops thru your arrays of lines and rects. Can you see how easy it would be to use the same kind of loops to count how many there are in each of those arrays?
There are also quite a few other ways to get these counts; I suggested another one in my previous post.
Here's one more: you store your shapes in arrays that you fill up with the appropriate shapes. So the count of each shape is the same as the size of the array...
This is really a lot easier than many of the things you have already done. You can do it if you just keep a clear head and think constructively.

thanks for your cluse i just need final direction do i need to put the array into my shape class and rom there
to carry on ?

Don't know what array you are talking about here, sorry.
I was just thinking of the arrays that you already have in your DrawPanel class

private MyLine lines [];
private MyRectangle rects [];

you know how big they are, so you know how many of each shape there is.

shapeType =randomNumber.nextInt(3);
            if (shapeType==0){    // Line
                lineCount++;
                shapes[i] = new MyLine(x1,y1,x2,y2,color);
            }
            else if (shapeType==1){   // Rectangle
                rectCount++;
                shapes[i] = new MyRectangle(x1,y1,x2,y2,color,true);
            }
            else {     // Oval
                ovalCount++;
                shapes[i] = new MyOval(x1,y1,x2,y2,color,true);
            }
        }
 
 }
 
    public void paint(Graphics g){
        for (MyShape shape:shapes)
            shape.draw(g);

so - you have created 3 variables and used them to count the number of each shape as the shapes are created? That looks like a valid approach to me. Now you just need to use those 3 variables to create the line of text that goes into your JLabel.

ps: I just saw where you stole that code from. Shame on you.

Edited 5 Years Ago by JamesCherrill: ps

so - you have created 3 variables and used them to count the number of each shape as the shapes are created? That looks like a valid approach to me. Now you just need to use those 3 variables to create the line of text that goes into your JLabel.

ps: I just saw where you stole that code from. Shame on you.

i just asked you if this is a good code, i didn't ment to stole it, if it looked like this i am sorry
can you just tell me on which class do i need to work on, and implement those methods
on DrawPanel or testDraw or MyShape i am getting confused

You copied code from another Daniweb user and posted it as part of your own post without giving the author any recognition or credit. Even if you had intention of misleading anyone, that's still bad manners. Anyway, enough said, let's move on.

can you just tell me on which class do i need to work on, and implement those methods

You need to
1. Add the JLabel to your window before anything is displayed (I think you already have that)
2. Set the label's text correctly when you create the initial set of shapes
3. Update the label's text whenever you add or remove a shape
You know your code best. Where would you put the extra code so that it's executed at the right time(s)?

on what class do i need to focus and work on, the MyShape, TestDraw, or DrawPanel ???
i am trying to understand your instructions
sorry i am an international student
so the english is kinda making it second challenge

that nothing about if is English your 1.st language or not, but about how to learn to write code by ourself

don't tell us something about international student, that's more poor, more than as you expedcted, just you abuse this forum

ok back to topic, let's code that

1/ you have some classes, some works but MyOval not (sorry I don't want to check previous posts),
2/ check 1st. class (which works) how is added to the DrawPanel and how way(s), which methods are used there, check 2nd. one if is same, if yes then same way add MyOval,
3/ continue with JamesCherrill's advices about JLabel......

if you'll have some (really Java relevant) problem, feel free to ask for help

now I'm out from this thread

Hi iam trying to fix the problem with my label i was abel to show the label on the bittom but i cannot figure how to able the label to count the figure which class to i need focus on ?
i think i gave enough info

is there a way to show me those clues
on actual code, but of course not wright the acual code, cuz i understand how it would look if i can see the code

Good morning S.
This is your homework for you to do. I'll help, but I won't do it for you. I've given you a stack of info to think about, so now it's your turn to put in some serious effort.
Sorry if that sounds harsh, but it's how things work around here, and it really is the best way.
J

i think the oval was fixed,
but the label was changed to what you see in the attachment
i need to modify my MyShape to String Method?

package Project3;
import java.awt.Color;
 import java.awt.Graphics;
public class MyOval extends MyShape {

	private int x1; // x coordinate of first endpoint
	 private int y1; // y coordinate of first endpoint
	 private int x2; // x coordinate of second endpoint
	 private int y2; // y coordinate of second endpoint
	 private Color myColor; // Color of this oval
	 private boolean filled; // whether this shape is filled
	
	 // constructor initializes private vars with default values
	 public MyOval()
	 {
	 this( 0, 0, 0, 0, Color.BLACK, false ); // call constructor
	 } // end MyOval no-argument constructor
	
	 // constructor with input values
	 public MyOval( int x1, int y1, int x2, int y2,
	 Color color, boolean isFilled )
	 {
	 setX1( x1 ); // set x coordinate of first endpoint
	 setY1( y1 ); // set y coordinate of first endpoint
	 setX2( x2 ); // set x coordinate of second endpoint
	 setY2( y2 ); // set y coordinate of second endpoint
	 setColor( color ); // set the color
	 setFilled( isFilled );
	 } // end MyOval constructor
	
	 // set the x-coordinate of the first point
	 public void setX1( int x1 )
	 {
	 this.x1 = ( x1 >= 0 ? x1 : 0 );
	 } // end method setX1
	
	 // get the x-coordinate of the first point
	 public int getX1()
	 {
	 return x1;
	 } // end method getX1
	// set the x-coordinate of the second point
	  public void setX2( int x2 )
	  {
	  this.x2 = ( x2 >= 0 ? x2 : 0 );
	  } // end method setX2
	 
	  // get the x-coordinate of the second point
	  public int getX2()
	  {
	  return x2;
	  } // end method getX2
	 
	  // set the y-coordinate of the first point
	  public void setY1( int y1 )
	  {
	  this.y1 = ( y1 >= 0 ? y1 : 0 );
	  } // end method setY1
	 
	  // get the y-coordinate of the first point
	  public int getY1()
	  {
	  return y1;
	  } // end method getY1
	 
	  // set the y-coordinate of the second point
	  public void setY2( int y2 )
	  {
	  this.y2 = ( y2 >= 0 ? y2 : 0 );
	  } // end method setY2
	 
	  // get the y-coordinate of the second point
	  public int getY2()
	  {
	  return y2;
	  } // end method getY2
	 
	  // set the color
	  public void setColor( Color color )
	  {
	  myColor = color;
	  } // end method setColor
	 
	  //get the color
	   public Color getColor()
	   {
	   return myColor;
	   } // end method getColor
	  
	   // get upper left x coordinate
	   public int getUpperLeftX()
	   {
	   return Math.min( getX1(), getX2() );
	   } // end method getUpperLeftX
	// get upper left y coordinate
	    public int getUpperLeftY()
	    {
	    return Math.min( getY1(), getY2() );
	    } // end method getUpperLeftY
	   
	    // get shape width
	    public int getWidth()
	    {
	    return Math.abs( getX2() - getX1() );
	    } // end method getWidth
	   
	    // get shape height
	    public int getHeight()
	    {
	    return Math.abs( getY2() - getY1() );
	    } // end method getHeight
	   
	    // determines whether this shape is filled
	    public boolean isFilled()
	    {
	    return filled;
	    } // end method is filled
	   
	    // sets whether this shape is filled
	    public void setFilled( boolean isFilled )
	    {
	    filled = isFilled;
	    } // end method setFilled
	   
	    // draws an oval in the specified color
	    public void draw( Graphics g )
	    {
	    g.setColor( getColor() );
	   
	    if ( isFilled() )
	    g.fillOval( getUpperLeftX(), getUpperLeftY(),
	    getWidth(), getHeight() );
	    else
	    g.drawOval( getUpperLeftX(), getUpperLeftY(),
	     getWidth(), getHeight() );
	    } // end method draw
	  } // end class MyOval
package Project3;
import java.awt.Color;
//import java.awt.Graphics;

public class MyShape {

	private int x1, y1, x2, y2;
    private Color myColor;
    public MyShape()
    {this.setX1(0);
       this.setY1(0);
       this.setX2(0);
       this. setY2(0);
        setMyColor(Color.BLACK);
    }
    public MyShape(int x1, int y1, int x2, int y2, Color myColor)
    {setX1(x1);
        setY1(y1);
        setX2(x2);
        setY2(y2);
        setMyColor(myColor);
    }
    public void setX1(int x1)
    {
        if(x1 >= 0 && x1 <= 300)
        { this.x1 = x1;}
        else
        {this.x1 = 0;}
    }
    public int getX1()
    { return x1;}
    
    public void setY1(int y1)
    {
        if(y1 >= 0 && y1 <= 300)
        { this.y1 = y1;  }
        else
        { this.y1 = 0; }
        
    }
    public int getY1()
    { return y1;}
    public void setX2(int x2)
    {
        if(x2 >= 0 && x2 <= 300)
        { this.x2 = x2; }
        else
        { this.x2 = 0; }
    }
    public int getX2()
    { return x2; }
    public void setY2(int y2)
    {
        if(y2 >= 0 && y2 <= 300)
        { this.y2 = y2; }
        else
        { this.y2 = 0; }
    }
    public int getY2()
    { return y2; }
    
    public void setMyColor(Color myColor)
    { this.myColor = myColor; }
    
    public Color getMyColor()
    { return myColor; }
    
    
   
    public String toString()
    {return String.format("Lines: %d, Ovals: %d, Rectangles: %d" ,getX1(), getX2(),
                        getY1(), getY2(), getMyColor());
    
	
    }
}
Attachments Screen_shot_2011-04-21_at_8.34_.24_AM_.png 35.97 KB

I'm really sorry, but there's no point my talking if you aren't listening.
Good luck with your assignment, I'm sure you will get it OK in the end.
J

This question has already been answered. Start a new discussion instead.