• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Draw rectangle given parameters

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
My pie came with a little toothpic holding up this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic