This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes jpanel paint image using method like jpanel1.paint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jpanel paint image using method like jpanel1.paint" Watch "jpanel paint image using method like jpanel1.paint" New topic
Author

jpanel paint image using method like jpanel1.paint

Jhovarie Guiang
Greenhorn

Joined: Jul 13, 2011
Posts: 15
hello guys there are many source code to paint image in jpanel but this time im trying to do this code like for example
i have a JPanel jpanel1 = new JPanel();
and i want to paint method link this code

"jpanel1.paint"

here is my code.



"Graphics graphics = " is my big problem how to draw image using this code? thanks in advance.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

There's only one correct way.
http://download.oracle.com/javase/tutorial/uiswing/painting/index.html

If all you want to do is to display an image, the better way to do that is with JLabel and ImageIcon.


luck, db
There are no new questions, but there may be new answers.
Jhovarie Guiang
Greenhorn

Joined: Jul 13, 2011
Posts: 15
if using a jlabel and icon still there is a problem the image doest not fit into jlabel. and still we must use something like



or other to fit the image into jlabel.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

the image doest not fit into jlabel


It will, if you use a layout manager correctly.
http://download.oracle.com/javase/tutorial/uiswing/layout/index.html

Unless of course, you want the image to be scaled to a certain size, which could be one interpretation of your rather vague post. IMO that's better achieved by extending ImageIcon than by custom painting.

Why don't you go through the entire Swing tutorial trail? Programming is so much more fun when you understand what you're doing.
Jhovarie Guiang
Greenhorn

Joined: Jul 13, 2011
Posts: 15
Sorry but how does layoutmanager help to fit the image? is that by setting the layout of jlabel?

see my code but it does not work


and i also try to do like this

 
 
subject: jpanel paint image using method like jpanel1.paint
 
Similar Threads
How to draw a line on a JPanel without extending it o overriding the paint method?
Confused about painting in JPanels
Drawing
placing image on jpanel and then add other components
How to Draw a Line in Java?