The Artima Developer Community
Sponsored Link

Java Answers Forum
Displaying image in JPanel

2 replies on 1 page. Most recent reply: Jun 25, 2002 4:12 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 2 replies on 1 page
Jacques

Posts: 6
Nickname: pichard
Registered: Jun, 2002

Displaying image in JPanel Posted: Jun 25, 2002 9:03 AM
Reply to this message Reply
Advertisement
I have to display a PianoRoll and to modelize the keyboard, I want to use a JPanel for each note (this notes are gif files).
In addition, I want to resize this JPanels with a zoom function I have ever implemented.
Please help me, I don't know how to display image to JPanel and the resize this JPanels without cuting the image.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Displaying image in JPanel Posted: Jun 25, 2002 4:11 PM
Reply to this message Reply

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: Displaying image in JPanel Posted: Jun 25, 2002 4:12 PM
Reply to this message Reply
The following is a demo of how to use the above class in an application.




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

public class ImagePanelDemo extends JFrame{

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

public void init(){
getContentPane().add(new ImagePanel("EinsteinBike.jpg"), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
show();
}
}

Flat View: This topic has 2 replies on 1 page
Topic: Report in JSP Previous Topic   Next Topic Topic: calling unix script from a java program

Sponsored Links



Google
  Web Artima.com   

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