The Artima Developer Community
Sponsored Link

Java Answers Forum
An image in a panel

3 replies on 1 page. Most recent reply: Jul 12, 2002 6:15 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
klonic

Posts: 1
Nickname: klonic
Registered: Jul, 2002

An image in a panel Posted: Jul 11, 2002 9:30 AM
Reply to this message Reply
Advertisement
I don't know how to embed an image into a panel. Can you help me, please. Thank you.


Stefan Parijs

Posts: 16
Nickname: stefan
Registered: May, 2002

Re: An image in a panel Posted: Jul 12, 2002 10:20 AM
Reply to this message Reply
Panel p = new JPanel();
You also have Your Image (for example imag)
then you do
p.add(imag);
That is all.
http://java.sun.com/products/jdk/1.3/docs/api/index.html
or
http://java.sun.com/products/jdk/1.4/docs/api/index.html

Best regards
Stefan Parijs

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: An image in a panel Posted: Jul 12, 2002 6:13 PM
Reply to this message Reply
Check out:
http://www.quantumhyperspace.com/SourceBank/ImagePanel.java


import java.awt.*;
import java.io.*;
import javax.swing.*;

public class ImagePanel extends JPanel{

private ImageIcon imageIcon;
public ImagePanel(File imageFile){
super();
imageIcon = new ImageIcon(imageFile.getAbsolutePath());
setPreferredSize(new Dimension(imageIcon.getIconWidth(), imageIcon.getIconHeight()));
}

public ImagePanel(String imageFileName){
super();
imageIcon = new ImageIcon(imageFileName);
setPreferredSize(new Dimension(imageIcon.getIconWidth(), imageIcon.getIconHeight()));
}

public void paint(Graphics g){
super.paintComponent(g);
g.drawImage(imageIcon.getImage(),0,0,this);
}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: An image in a panel Posted: Jul 12, 2002 6:15 PM
Reply to this message Reply
This is used as follows:


/* ImagePanelTest.java
*/

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class ImagePanelTest extends JFrame{

public ImagePanelTest(){
super("ImagePanelTest");
init();
}
public static void main(String[] args){
ImagePanelTest test = new ImagePanelTest();
}

public void init(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File imageFile = new File("EinsteinBike.jpg");
getContentPane().add(new ImagePanel(imageFile), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null); //centers everything on screen
show();
}
}


You can get this from:
http://www.quantumhyperspace.com/SourceBank/ImagePanelTest.java

Flat View: This topic has 3 replies on 1 page
Topic: how can I change it? Previous Topic   Next Topic Topic: How to generate reports

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use