• Post Reply Bookmark Topic Watch Topic
  • New Topic

Draw rectangle given parameters  RSS feed

 
Papote Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a hard time trying to draw a simple rectangle on the JFrame where I supply the parameters from main. OOP is not my strong suit.



Not sure what to send as first parameter for paint(g). I put in gui.getGraphics() cause I have no idea, but doesn't work, obviously.

This just displays the blank JFrame.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what to send as first parameter for paint

There is a paint() method define in the JFrame class.  If you intend to override that method, you need to code its definition the same as the definition in the class.  See the API doc for JFrame.
The JFrame's paint() method is automatically called by the JVM and passed a Graphics object when changes are made to the GUI that need to be redrawn and after the repaint() method is called.  Normally code doing custom drawing overrides the paint() method.

If you want to define your own method to do the drawing, you should give it a different name and you will be required to call it to have it be executed.

See the tutorial about custom painting: http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is almost always better to subclass a JPanel and add that to the main JFrame, rather than to subclass JFrame itself.
Then your JPanel subclass should override paintComponent:

Note the call to super.paintComponent(). This ensures that the background gets refreshed any time you make a change to what is to be drawn, and also ensures that that any borders are properly rendered. The variables needed to draw things (x,y,width,height,etc.) should be input via the constructor or other methods. You should never call paintComponent() yourself; calling repaint() after you make changes to the drawing parameters will force the JVM to call your paintComponent method with the proper Graphics instance passed to it.
 
Papote Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
what to send as first parameter for paint

There is a paint() method define in the JFrame class.  If you intend to override that method, you need to code its definition the same as the definition in the class.  See the API doc for JFrame.
The JFrame's paint() method is automatically called by the JVM and passed a Graphics object when changes are made to the GUI that need to be redrawn and after the repaint() method is called.  Normally code doing custom drawing overrides the paint() method.

If you want to define your own method to do the drawing, you should give it a different name and you will be required to call it to have it be executed.

See the tutorial about custom painting: http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html


The tutorial in this page helped a lot.
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!