4 Replies - 22944 Views - Last Post: 25 May 2007 - 10:20 AM Rate Topic: -----

#1 aluur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-April 07

draw a square

Posted 25 May 2007 - 07:09 AM

i want to draw square when button named square clicked, i preapared that ;
rectangle extends shape, and square extends rectangle . it seems true when compiled but i cannot draw a square i can draw a rectangle.
public class Rectangle extends XYShape {
	
	private int height,width;
	
  
	public Rectangle(int x,int y) {	 
		super(x,y);
	}
	public Rectangle(int x ,int y,int width ,int height){
		super(0, 0,x,y);
		setWidth(width);
		setHeight(height);
	}	
	
		
	public void setWidth(int w){
		if( w > 0 )width=w;
	}
	
	public void setHeight(int h){
		if(h > 0)height=h;
	}
	
	public int getWidth(){return width;}
	
	public int getHeight(){return height;}

	public void Draw(java.awt.Graphics g) {
		g.drawRect(x0+x, y0-y, width,height);
	}
		  
}	

public class Square extends Rectangle{
	
	private int side;
   
  
	public Square(int x ,int y ) {	 
		super(x,y);
	}
	public Square(int x, int y,int side){
			super(side,side);
		  }	
  
   public int getSide() {
   	 return getWidth(); } 
	
   public void setSide(int side) {
	  super.setWidth(side);
	  super.setHeight(side);
   }
   public void setWidth(int width) {
   	 this.setSide(side); }
   public void setHeight(int width) {
   	 this.setSide(side); }
			
}	




whats the problem with me i couldnt understand exactly, i jsut wonder wheter my main class or square class

Is This A Good Question/Topic? 0
  • +

Replies To: draw a square

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: draw a square

Posted 25 May 2007 - 07:23 AM

Not entirely sure what your problem is, but a square is a rectangle with a length and width that are the same. If you can draw a rectangle, then you can draw a square.
Was This Post Helpful? 0
  • +
  • -

#3 aluur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-April 07

Re: draw a square

Posted 25 May 2007 - 09:03 AM

View PostWilliam_Wilson, on 25 May, 2007 - 07:23 AM, said:

Not entirely sure what your problem is, but a square is a rectangle with a length and width that are the same. If you can draw a rectangle, then you can draw a square.


i know that , only need proper square class
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: draw a square

Posted 25 May 2007 - 09:46 AM

No you don't, just as William_Wilson said: square is a rectangle with a length and width that are the same.

Use the same class.
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: draw a square

Posted 25 May 2007 - 10:20 AM

if you must have a square class, have it be a subclass of rectangle, but do not define and methods as all for rectangle will apply, perhaps have a single method which ensures that length == width.

you could also add a method to your rectangle class called isSquare(), or something to that effect.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1