Results 1 to 8 of 8
Thread: Load image to jPanel
- 12-22-2010, 06:31 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
Load image to jPanel
hi all,
how can i load image to an jPanel
i see example in Computer Graphics Using Java™ 2D and 3D book, but not using netbeans, it usse the jpanel as a class.
here is my netbeans project.
ImgView.zip
how can i reuse the jpanel paint method to load the image.
thanks.
- 12-22-2010, 02:56 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
Hello,
Next time, put your code in the post, not as zip. I'm not touching your file for security reasons ;)
You say you are using the JPanel class, I assume you created your own class which extends JPanel. Then simply override paintComponent(Graphics g) method.
Simply write a file to your image and call for a repaint method, the repaint methode invokes paintComponent method :DJava Code:@Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // adds more functionality super.paintComponent(g2); // don't forget this ! if (image != null) { // field image g.drawImage(image, 0, 0, this); // draw at point (0,0) } }
Java Code:File file = new File("path/to/your/image.jpg"); BufferedImage image = ImageIO.read(file); this.image = image; // field image is set to local repaint();
- 12-22-2010, 02:58 PM #3
If all you want to do is display an image, the alternative is to simply load your image as an icon in a JLabel and add that JLabel to the JPanel. If you do it that way, there is no need to subclass JPanel.
- 12-22-2010, 11:17 PM #4
And learn Swing.
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
That's easier than struggling to get things the way you want them in the NetBeans visual designer.
db
- 12-22-2010, 11:19 PM #5
- 12-25-2010, 08:44 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
You are storing the local variable named 'image' in the field named 'image'.
- 12-26-2010, 07:12 AM #7
So why have a local variable that's never used? Why not assign the field directly from ImageIO#read(...)?
db
- 12-27-2010, 12:48 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Load website on a JPanel.
By Masochist in forum New To JavaReplies: 2Last Post: 05-11-2010, 07:44 PM -
load image in the jsp page
By maneuk in forum EclipseReplies: 0Last Post: 04-09-2010, 09:23 PM -
ImageIcon won't load my image
By JaiRaj in forum AWT / SwingReplies: 5Last Post: 03-04-2010, 06:35 PM -
Can not load image in SWT/Eclipse
By janice in forum SWT / JFaceReplies: 4Last Post: 10-03-2008, 04:53 PM -
Help with load image
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:16 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks