Results 1 to 7 of 7
Thread: How I can draw..
- 11-08-2012, 09:11 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 61
- Rep Power
- 0
-
Re: How I can draw..
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.
- 11-09-2012, 11:12 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 61
- Rep Power
- 0
- 11-09-2012, 01:23 PM #4
- 11-10-2012, 04:24 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 61
- Rep Power
- 0
Re: How I can draw..
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); } }
- 11-10-2012, 04:46 PM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-10-2012, 04:54 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 61
- Rep Power
- 0
Similar Threads
-
Draw a pyramid
By MisterNikos in forum New To JavaReplies: 23Last Post: 04-02-2012, 01:44 PM -
draw to file with GDC
By johnyjj2 in forum New To JavaReplies: 6Last Post: 12-04-2011, 05:03 PM -
why cant i draw this box???
By stefandanielsen in forum New To JavaReplies: 2Last Post: 05-12-2011, 02:53 PM -
how to draw an arc
By Baker in forum New To JavaReplies: 1Last Post: 04-16-2009, 09:05 PM -
help me draw... please...
By kureikougaiji in forum New To JavaReplies: 1Last Post: 01-28-2008, 12:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks