Results 1 to 7 of 7
  1. #1
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default How I can draw..

    How I can draw square in my JFrame from the class that extends JFrame?I Want after that to redraw it or remove it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How I can draw..

    Quote Originally Posted by gyijhbk View Post
    How I can draw square in my JFrame from the class that extends JFrame?I Want after that to redraw it or remove it.
    You shouldn't draw directly in the JFrame, even if you want to. Draw in a class that extends JPanel or other class that derives from JComponent. Do the drawing in the class's paintComponent method, just as is shown to you in the painting with Swing tutorials.

  3. #3
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: How I can draw..

    Quote Originally Posted by Fubarable View Post
    You shouldn't draw directly in the JFrame, even if you want to. Draw in a class that extends JPanel or other class that derives from JComponent. Do the drawing in the class's paintComponent method, just as is shown to you in the painting with Swing tutorials.

    How I can draw something without override the paint method?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: How I can draw..

    Quote Originally Posted by gyijhbk View Post
    How I can draw something without override the paint method?
    Go through the linked tutorial and you'll know. Or were you expecting someone here to create a new tutorial especially for you?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: How I can draw..

    Quote Originally Posted by DarrylBurke View Post
    Go through the linked tutorial and you'll know. Or were you expecting someone here to create a new tutorial especially for you?

    db
    But I cannot redraw my shape.Why?
    Java Code:
    class sq extends JPanel{	
    	 RedSquare2 redSquare = new RedSquare2();
    
    public void move(int x ,int y)
    {  final int OFFSET = 1;
    	final int CURR_X = redSquare.getX();
    final int CURR_Y = redSquare.getY();
    final int CURR_W = redSquare.getWidth();
    final int CURR_H = redSquare.getHeight();
    	 repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
    	
    	 redSquare.setX(x);
         redSquare.setY(y);
    
        // Repaint the square at the new location.
        repaint(redSquare.getX(), redSquare.getY(), 
                redSquare.getWidth()+OFFSET, 
                redSquare.getHeight()+OFFSET);}
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
        g.drawString("This is my custom Panel!",10,20);
    
        redSquare.paintSquare(g);
    }  
    }
    
    class RedSquare2{
    
       private int xPos = 50;
       private int yPos = 50;
       private int width = 20;
       private int height = 20;
    
       public void setX(int xPos){ 
           this.xPos = xPos;
       }
    
       public int getX(){
           return xPos;
       }
    
       public void setY(int yPos){
           this.yPos = yPos;
       }
    
       public int getY(){
           return yPos;
       }
    
       public int getWidth(){
           return width;
       } 
    
       public int getHeight(){
           return height;
       }
    
       public void paintSquare(Graphics g){
           g.setColor(Color.RED);
           g.fillRect(xPos,yPos,width,height);
           g.setColor(Color.BLACK);
           g.drawRect(xPos,yPos,width,height);  
       }
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: How I can draw..

    1. I don't see anywhere that the move() method is invoked.

    2. I don't see any code that actually moves the rectangle.

    3. I think you need to go through the API for repaint(int, int, int, int) to learn what it really does.

    Additionally, learn to follow Java coding conventions: class names should start with an uppercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: How I can draw..

    Quote Originally Posted by DarrylBurke View Post
    1. I don't see anywhere that the move() method is invoked.

    2. I don't see any code that actually moves the rectangle.

    3. I think you need to go through the API for repaint(int, int, int, int) to learn what it really does.

    Additionally, learn to follow Java coding conventions: class names should start with an uppercase letter.

    db
    The move() method is invoked from other class.
    Why repaint does not repaint the shape?

Similar Threads

  1. Draw a pyramid
    By MisterNikos in forum New To Java
    Replies: 23
    Last Post: 04-02-2012, 01:44 PM
  2. draw to file with GDC
    By johnyjj2 in forum New To Java
    Replies: 6
    Last Post: 12-04-2011, 05:03 PM
  3. why cant i draw this box???
    By stefandanielsen in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:53 PM
  4. how to draw an arc
    By Baker in forum New To Java
    Replies: 1
    Last Post: 04-16-2009, 09:05 PM
  5. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 12:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •