Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

paintcomponent method to draw on jframe  RSS feed

 
Sam Hackett
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out how to draw directly onto a jframe overriding the jpanel method paintcomponent. I appreciate any help.

 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably better adding a panel to paint on, and overriding paintComponent in that panel. You might call paintIcon but where is the Icon? Start with something simpleThat should draw a black circle. I can never remember how to draw icons, but you will need an image to draw.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never add the panel to the frame, so the frame has nothing to paint other than itself.

Anyway, the whole structure of your code is wrong. You don't define a JFrame in a JPanel class.

Read the section from the Swing tutorial on Custom Painting for more information and examples on a better way to structure your code.

Start with the working demo and make changes to that code to experiment with painting functionality.
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JFrame is your top level component and container, as such you need to use paint instead of paintComponent. Everywhere you have paintComponent in your example change it to paint and you should be alright.

If you add anything to your JFrame you will have that portion of the JFrame covered by the component unless you set the transparency where available.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Les,

No you should NOT do painting directly to the frame and no you should NOT override paint() on the JFrame.

 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:@Les,

No you should NOT do painting directly to the frame and no you should NOT override paint() on the JFrame.



@Bob,

While I respect what you are saying, even the Sun and now Oracle Tutorials tell us that we can, but it is not advised. To do so you override paint, since JFrame is a top level container, and make a call to super.paint. If you follow those rules as outlined by Sun and Oracle you have a well minded top level container.

Les

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it is not advised.


Exactly, it is not advised and there is absolutely no reason to do that in this case.

The OP's original code is the proper way to do custom painting. The problem is the panel wasn't added to the frame.

There is too much junk code found in forums on the web which promote bad habits that will ultimately lead to problems down the road.

Promote proper coding techniques.
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
but it is not advised.


Exactly, it is not advised and there is absolutely no reason to do that in this case.

The OP's original code is the proper way to do custom painting. The problem is the panel wasn't added to the frame.

There is too much junk code found in forums on the web which promote bad habits that will ultimately lead to problems down the road.

Promote proper coding techniques.


@Rob

At some point in a person's life of coding graphics they are going to need to modify the top level container for some reason, teach them how to do it when they ask originally and it is not a mystery, the short example I posted is according to how Sun and Oracle say to modify/paint a JFrame.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
teach them how to do it when they ask


The OP is not asking how to draw on the frame. Don't complicate the answer.

The OP is asking how to draw on a frame by using JPanel when overriding the paintComponent() method.

The OP simply forgot to add the panel to the frame.
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
teach them how to do it when they ask


The OP is not asking how to draw on the frame. Don't complicate the answer.

The OP is asking how to draw on a frame by using JPanel when overriding the paintComponent() method.

The OP simply forgot to add the panel to the frame.


My Humblest apologies. I took the title and code snipet and didn't fully look over his original post--delete all my comments in this thread if you can.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!