Putting an Image in JPanel

I am trying to put an image in JPanel. Using something other than ImageIcon. When I run the program only a white screen appears.
package game;
import gui.FullScreenDisplay;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.Runnable;
import java.lang.Thread;
public class Runner implements Runnable
     private static final long serialVersionUID = 1L;
     private FullScreenDisplay display;
     public static void main(String args[])
          Thread t = new Thread(new Runner());
          t.start();
     public Runner()
          makeGui();
     private void makeGui()
          display = new FullScreenDisplay(this);
     public void run()
          try {
               Thread.sleep(1000);
          } catch (InterruptedException e) {/*Nothing to do*/}
          run();
     public void quit()
          System.exit(0);
package gui;
import game.Runner;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class FullScreenDisplay extends JFrame {
     private static final long serialVersionUID = 1L;
     private Runner master;
     private JPanel mainPanel;
     private Image tempImage;
     public FullScreenDisplay(Runner master)
          super();
          //Remove this eventually.
          try {
               tempImage = ImageIO.read(new File("test_image.jpg"));
          } catch (IOException e) {
               System.out.println("image get error");
               e.printStackTrace();
          this.master = master;
          makeFrame();
          makePanel();
          makeKeyBindings();
          //setFullScreen(chooseBufferStrategy());
          setFullScreen();
          requestFocus();
          setVisible(true);
          //Remove this eventually.
          Graphics g = tempImage.getGraphics();
          mainPanel.paint(g);
          this.update(g);
     private void makeFrame()
          setUndecorated(true);
          setResizable(false);
          setFocusable(true);
     private void makePanel()
          mainPanel = new JPanel(){
               public void paintComponent(Graphics g) {
                    if(tempImage == null){
                         System.out.println("Balls");
                    g.drawRect(10, 10, 10, 10);
                    g.drawImage(tempImage,0,0,null);
                    super.paintComponent(g);
                    System.out.println("Did it work");
          add(mainPanel);
     private void makeKeyBindings()
          mainPanel.setFocusable(true);
          //Key bindings... don't like using a string to index, find out if there's a better way.
          mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit");
          mainPanel.getActionMap().put("exit", new AbstractAction(){
               public void actionPerformed(ActionEvent e)
                    master.quit();
     /*private void chooseBufferSrategy()
     private void setFullScreen()
          GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
}

super.paintComponent(g);should be the first line in paintComponent()
also, you don't really need the keyBindings to quit,
just set the default close to exit and Alt-F4 will close/quit

Similar Messages

  • How to put images on JPanel one at a time?

    Hi, guys. I am trying to put 7 images to a panel one by one,each takes one second so that users can see the process each images being shown on the panel.
    But the fact is the system freeze for 7 seconds and then display them all once. Altho the "one image has been dealt" showsin the console every one second.
    Thank you
    Here is the code :
    for (int i=0;i<7 ;i++ )
                   try
                   thisTile[i] = new tiles(BBV.string,"BlackBack",100,100);
                   thisTile.setBounds(x1,y1,27,49);               
                   JP1.add(thisTile[i]);                              
                   y1=y1+54;
                   System.out.println("one image has been dealt");
                   Thread.sleep(1000);
                   catch (InterruptedException e)
                        System.out.println("this should not be printed");

    for example, I want to print "all 7 images has been dealt" after the images have been put on the JPanel, by using swing timer it prints the "all 7 images has been dealt" during/before the images being put. Here is the code example(thisTile is a array of 7 images):
    public void showImages()
    t = new javax.swing.Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
                   thisTile[j] = new tiles(BBV.string,"BlackBack",100,100);
                   thisTile[j].setBounds(x1,y1,27,49);               
                   JP1.add(thisTile[j]);
                   JP1.validate();
                   JP1.repaint();
                   y1=y1+54;
                   j++;
                   if (j==7)
                        t.stop();
              //AI1 gets his tiles
              for (int i=0;i<7 ;i++ )
                   t.start();               
              System.out.println("all images has been dealt");
              }

  • Put an Image to a JPanel

    Is there an easy way to put a Image to a JPanel (or as a background for the JPanel). Or is there some kind of JImagePanel class that I can use.

    Hi,
    put JLabel into JPanel and an image put into JLabel. And next time try to search the forum first.
    L.P.

  • Putting an image into a JFrame or a JPanel

    Hello everybody,
    I'd like to know how to put an image (a real one, not a small icon we can't see it!) into a JFrame, or a JPanel, or included into a miscellanea element (label, textfield or somethiong else).
    Thanks,
    Regards
    Cedric

    Hi Cedric,
    If the image format is Jpg or gif, then the best way to insert the image
    into the Frame or panel is to use JLabel. You can pass the argument to the file as parameter to JLabel construction.
    For other formats, you have the option of using drawimage in paint function of applets.
    Hope this helps.
    Best Regards,
    Chandru

  • Background image  for JPanel using UI Properties

    Is there any way to add background image for JPanel using UI Properties,
    code is
    if (property.equals("img")) {
    System.out.println("call image file in css"+comp);
    //set the background color for Jpanel
    comp.setBackground(Color.decode("#db7093"));
    here the comp is JPanel and we are setting the background color,
    Is there any way to put the Background image for the JPanel ????

    KrishnaveniB wrote:
    Is there any way to put the Background image for the JPanel ????Override the paintComponent(...) method of JPanel.
    e.g.
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    public class ImagePanel {
        public void createAndShowUI() {
            try {
                JFrame frame = new JFrame("Background Image Demo");
                final Image image = ImageIO.read(new File("/home/oje/Desktop/icons/yannix.gif"));
                JPanel panel = new JPanel() {
                    protected void paintComponent(Graphics g) {
                        g.drawImage(image, 0, 0, null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(new Dimension(400, 400));
                frame.setContentPane(panel);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            } catch (IOException ex) {
                ex.printStackTrace();
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ImagePanel().createAndShowUI();
    }

  • Display a transparent image in JPanel

    i just start using Java Graphics Programming fews month ago. there's some problem i facing recently. i doing a simple gif file viewer. first i get the file using the Toolkit and put it in a Image object and use the Gif Decoder to decoded each frame of the Gif File to BufferedImage object and display each of the frame in a JPanel inside a JFrame.My porblem is :-
    How to display a transparent image in JPanel? my image source in BufferedImage and how to i know the image is transparent or not?

    I simply use ImageIcon object to display the image (*.gif,*.jpg)
    JLabel l=new JLabel(new ImageIcon("file path"));
    add the label to a panel or frame or dialog
    this object no need to use the ImageBuffered Object
    It can display any animate gif whether the background is transparent or not.

  • CapturingCanvas3d not showing Image in JPanel

    Hello guys
    I m having problem showing image on a panel which has capturingCanvas3d in it. If I add this canvas to a JFrame then it shows otherwise it displays just blank.
    I hope you people to solve my problem.
    Regards
    Qasim Ali

    eggguns wrote:
    Ok I'm trying to put an image in a full screen JPanel, why isn't it working?This is not the Swing forum. Please repost your question into the Swing forum.
    And this is probably too much code as well.

  • Showing image in JPanel

    Ok I'm trying to put an image in a full screen JPanel, why isn't it working?
    package game;
    import gui.FullScreenDisplay;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.lang.Runnable;
    import java.lang.Thread;
    public class Runner implements Runnable
         private static final long serialVersionUID = 1L;
         private FullScreenDisplay display;
         public static void main(String args[])
              Thread t = new Thread(new Runner());
              t.start();
         public Runner()
              makeGui();
         private void makeGui()
              display = new FullScreenDisplay(this);
         public void run()
              try {
                   Thread.sleep(1000);
              } catch (InterruptedException e) {/*Nothing to do*/}
              run();
         public void quit()
              System.exit(0);
    package gui;
    import game.Runner;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.KeyEvent;
    import javax.swing.KeyStroke;
    import javax.swing.AbstractAction;
    import java.awt.event.ActionEvent;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.io.IOException;
    public class FullScreenDisplay extends JFrame {
         private static final long serialVersionUID = 1L;
         private Runner master;
         private JPanel mainPanel;
         private Image tempImage;
         public FullScreenDisplay(Runner master)
              super();
              //Remove this eventually.
              try {
                   tempImage = ImageIO.read(new File("test_image.jpg"));
              } catch (IOException e) {
                   System.out.println("image get error");
                   e.printStackTrace();
              this.master = master;
              makeFrame();
              makePanel();
              makeKeyBindings();
              //setFullScreen(chooseBufferStrategy());
              setFullScreen();
              requestFocus();
              setVisible(true);
              //Remove this eventually.
              Graphics g = tempImage.getGraphics();
              mainPanel.paint(g);
              this.update(g);
         private void makeFrame()
              setUndecorated(true);
              setResizable(false);
              setFocusable(true);
         private void makePanel()
              mainPanel = new JPanel(){
                   public void paintComponent(Graphics g) {
                        if(tempImage == null){
                             System.out.println("Balls");
                        g.drawRect(10, 10, 10, 10);
                        g.drawImage(tempImage,0,0,null);
                        super.paintComponent(g);
                        System.out.println("Did it work");
              add(mainPanel);
         private void makeKeyBindings()
              mainPanel.setFocusable(true);
              //Key bindings... don't like using a string to index, find out if there's a better way.
              mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit");
              mainPanel.getActionMap().put("exit", new AbstractAction(){
                   public void actionPerformed(ActionEvent e)
                        master.quit();
         /*private void chooseBufferSrategy()
         private void setFullScreen()
              GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
    }

    eggguns wrote:
    Ok I'm trying to put an image in a full screen JPanel, why isn't it working?This is not the Swing forum. Please repost your question into the Swing forum.
    And this is probably too much code as well.

  • Putting an Image in my CoordinateArea

    Hey,
    I would like to be able to place an image within my CoordinateArea, and I also want this image to be able to respond to being clicked on when the mouse is clicked between certain co-ordinates. Here is my code so far...
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import javax.swing.event.MouseInputListener;
    public class tourWelby extends JFrame {
    JLabel label = new JLabel();
    JPanel panel = new JPanel();
    Point clickPoint = new Point();
    Point cursorPoint = new Point();
    private static void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
              JFrame frame = new JFrame("TourWelby");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              tourWelby controller = new tourWelby();
              controller.buildUI(frame.getContentPane());
              //Create and set up the content pane.
              frame.getContentPane().setBackground(Color.BLACK);
              // Create the menu bar
              JMenuBar menu = new JMenuBar();
              menu.setBackground(Color.BLACK);
              //Build the menus.
              JMenu file = new JMenu("File");
              file.setBackground(Color.BLACK);
              file.setForeground(Color.WHITE);
              JMenu test = new JMenu("Test");
              test.setBackground(Color.BLACK);
              test.setForeground(Color.WHITE);
              // Install the menu bar in the frame
              frame.setJMenuBar(menu);
              menu.add(file);
              menu.add(test);
              //Display the window.
              frame.pack();
              frame.setVisible(true);
              frame.setResizable(false);
              frame.setSize(800,700);
         private void buildUI(Container container) {
    container.setLayout(new BoxLayout(container,
    BoxLayout.PAGE_AXIS));
    CoordinateArea theArea = new CoordinateArea(this);
    container.add(theArea);
    label = new JLabel();
              label.setBackground(Color.BLACK);
              label.setForeground(Color.WHITE);
    resetLabel();
    container.add(label);
    public void updateCursorLocation(int x, int y) {
    if (x < 0 || y < 0) {
    cursorPoint = null;
    updateLabel();
    return;
    if (cursorPoint == null) {
    cursorPoint = new Point();
    cursorPoint.x = x;
    cursorPoint.y = y;
    updateLabel();
    public void updateClickPoint(Point p) {
    clickPoint = p;
    updateLabel();
    public void resetLabel() {
    cursorPoint = null;
    updateLabel();
    protected void updateLabel() {
    String text = "The cursor is at ";
                   if (cursorPoint != null) {
    text += "("
    + cursorPoint.x + ", " + cursorPoint.y + ") ";
    label.setText(text);
         public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    public static class CoordinateArea extends JComponent implements MouseInputListener {
    Point point = null;
    tourWelby controller;
    Dimension preferredSize = new Dimension(800,700);
    public CoordinateArea(tourWelby controller) {
    this.controller = controller;
                   setBorder(BorderFactory.createMatteBorder(3,3,3,3, Color.RED));
    addMouseListener(this);
    addMouseMotionListener(this);
    public Dimension getPreferredSize() {
    return preferredSize;
    //Methods required by the MouseInputListener interface.
    public void mouseClicked(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    if (point == null) {
    point = new Point(x, y);
    } else {
    point.x = x;
    point.y = y;
    controller.updateClickPoint(point);
    repaint();
    public void mouseMoved(MouseEvent e) {
    controller.updateCursorLocation(e.getX(), e.getY());
    public void mouseExited(MouseEvent e) {
    controller.resetLabel();
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) { }
    public void mousePressed(MouseEvent e) { }
    public void mouseDragged(MouseEvent e) { }
    How do I go about putting an image in the CoordinateArea and making it clickable? Do I need to use an ImageIcon?
    Thanks in advance.

    Hi again everyone. I've gone and rethought my code and started again from scratch. Here is what I have come up with..
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.*;
    public class tourWelby extends JFrame  implements MouseListener {
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) { }
    public void mousePressed(MouseEvent e) { }
    public void mouseDragged(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    public void mouseClicked(MouseEvent e) { }
    public static void main (String args[]) {
         new tourWelby();
         public tourWelby() {
         super("Test");
        Container content = getContentPane();
         Point clickPoint = new Point();
         Point cursorPoint = new Point();
         JLabel label = new JLabel();
         label.setText("Test");
         label.setBackground(Color.WHITE);
         label.setForeground(Color.WHITE);
         JPanel theArea = new JPanel();
         theArea.setPreferredSize(new Dimension(800, 600));
         theArea.setBackground(Color.BLACK);
         theArea.add(label);
         theArea.addMouseListener(this);
         addMouseListener(this);
         content.add(theArea);
             pack();
             setVisible(true);
         setResizable(false);
    }Basiclly now, I think I need to add the mouseListener to the JPanel. However I'm not sure where I need to put the "public void updateCursorLocation" code and such that I had before (before, I used some samples of code from another program, and I wasn't entirely sure how it worked). If someone could help me (again!) with getting the MouseListener to change the label on the JPanel, to reflect where the cursor is (as in my previous code) I would again, be most appreciative. Thanks.

  • How can I put an image/s in Java Application?

    Hi to all.
    How can I put an image/s in Java Application?
    I can put some images in Java applet but when i try to put it in Java application there was an error.

    hey u can easily do it with JLabels...i found a Code Snippet for u
    public class MyPanel extends JPanel
    {private JLabel imageLabel; public MyPanel() {     Image image = Toolkit.getDefaultToolkit().getImage("myimage.gif"); // Could be jpg or png too     imageLabel = new JLabel(new ImageIcon(image));     this.add(imageLabel);  }}
    hope it helps
    Cheers,
    Manja

  • Putting an image in a JFrame

    I'm trying to paint an image onto a JFrame. Im making a monopoly game and need to paint the board, and repaint it after every turn. I dont know how to put an image onto a JFrame and was wondering if anyone could give me a basic algorithm or code example of how to do this. Thanks.

    Here is the basic algorithm to paint to a JFrame:
    Don't do it!
    Set the preferred size of a JPanel and add the JPanel to the JFrame. Paint to the JPanel by extending the paintComponent method. When you're ready to display, then remember to pack the JFrame and setVisible appropriately.
    You can read about doing this in the Java Tutorial.
    public void paintComponent(Graphics g){ //override in JPanel
      super.paintComponent(g);
      //do what ever you need to do here to draw your graphics.
    }I prefer to do all of my drawing to a BufferedImage and then use drawImage to show the image (off screen rendering):
    public void paintComponent(Graphics g){ //override in JPanel
      g.drawImage(myImage, 0, 0, this);
    }

  • I can't figure out the Keynote title option. I'm a newbie so forgive my ignorance. I want to put an image in the swing arm, but I can't figure it out.

    I even tried opening in Motion and inserting it there, but when I bring it back to fc it's not there. I also want to change the background to an image or at least solid white. When I put an image in the Dropzone it doesn't show up. Any help would be GREATLY appreciated!

    To change the title background to use a clip in the drop zone, do just as Jim said.
    Also, place the clip that want to appear in the "swing arm" right below the title in the timeline.

  • I had all photos on a Windows desktop.  Got a mac laptop, installed LR5, and put all images from Windows onto an external hard drive.  When I go to import them into LR on the Mac, they are all locked.  Help, please.  Thanks

    I had all photos on a Windows desktop.  Got a mac laptop, installed LR5, and put all images from Windows onto an external hard drive.  When I go to import them into LR on the mac, they are all locked.  Help, please!

    "I plugged the external into the Mac and moved a folder of images onto the desktop.  It looks like it went from read only to not locked.  Is this possible?"
    Yes, that's exactly what happened. If you buy another drive, you could copy from your existing drive to the new and your files will be read/write. The new drive will have to be formatted as an HFS drive (that's the Mac's format). If you need to format it, you use Disk Utility which is in the Utilities folder on your Mac. Be careful with that -- it wipes out whatever is currently on the drive. Make sure you format the right drive.
    I keep all my images on an external drive, too. In fact, I have two matching drives and sync them so I always have a backup.

  • How to put an image in a data grid in Flex Builder 2

    Hi All,
    I need to populate a data grid with some text data received
    from a web service and, in a particular column of the datagrid, I
    have to put an image depending of a specific data returned me by
    the web service.
    It seems that there is the possibility to add an image in
    data grid column with the cellRenderer properties, but this
    property is available only for ActionScript 3.
    I'm developing an application in Flex Builder 2 that run
    ActionScript 2 and cellRenderer properties is not available. Is it
    right?
    If no, I will can use this cellRenderer properties in my
    application. Please, can you show me an example?
    If yes, there is a way to insert an image in datagridcolumn
    with ActionScript 2?
    Thank you very much
    Regards

    Flex Builder 2 uses Actionscript 3.
    You will need to write a renderer for for this column.
    There are a lot of examples of datagrids with images in them.
    here is one from the livedocs
    http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Live Docs_Book_Parts&file=cellrenderer_072_28.html

  • How to put an image in the popup help of a page item?

    I am using APEX 2.2. I want to put an image in the help text of a page input item.
    This is the help text:
    Some help text....
    <p><img src="&APP_IMAGES.auto_lov.jpg" /></p>This is rendered as:
    <p><img src="wwv_flow_file_mgr.get_file?p_security_group_id=0&p_flow_id=103&p_fname=auto_lov.jpg" /></p>As you can see, the application id is not returned.
    If I put exactly the same text in a html region, the image is correctly displayed:
    <p><img src="wwv_flow_file_mgr.get_file?p_security_group_id=844129086577364&p_flow_id=103&p_fname=auto_lov.jpg" />So, is it possible at all to put in image in the popup help text of a page item?

    I solved it by putting the jpg file in an images directory on the web server instead of in the database. Not really what I want, but it works.
    <p><img src="&IMAGE_PREFIX./apps/auto_lov.jpg" /></p>

Maybe you are looking for