Welcome to Dream.In.Code
Become a Java Expert!

Join 149,535 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,507 people online right now. Registration is fast and FREE... Join Now!




draw a square

 
Reply to this topicStart new topic

draw a square

aluur
25 May, 2007 - 06:09 AM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 8


My Contributions
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.
CODE

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
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Draw A Square
25 May, 2007 - 06:23 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

aluur
RE: Draw A Square
25 May, 2007 - 08:03 AM
Post #3

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 8


My Contributions
QUOTE(William_Wilson @ 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.


i know that , only need proper square class
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Draw A Square
25 May, 2007 - 08:46 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Draw A Square
25 May, 2007 - 09:20 AM
Post #5

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month