1 Reply Latest reply: Jun 3, 2013 9:25 AM by gimbal2 RSS

    Help requested with displaying a graphic on the screen

      I hope that this is very simple to answer, but there is something I am missing in my coding.
      I am developing a simple java game program ( not a web app.) that will display some graphics (jpg files) on the screen.
      Ultimately I plan to make them move around - a timer function and collision detection code -- to be written).
      At this point, I am certain that the graphic, "Person-green.jpg", is found and read into the variable 'PersonGreen".
      The message System.err.println("Failed to load PersonGreen. Caught IOException: " + e.getMessage());
      does not appear.

      BUT - try as I might, I cannot get the graphic to display on the screen. I am at my wits end.
      The solution, I hope, should be simple, but... what am I doing wrong?

      Anyway - below is the code:
      package HtB;

      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Image;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import java.io.File;
      import java.io.IOException;

      import javax.imageio.ImageIO;

      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;

      public class Main extends JFrame {

      int i = 0;

      private int WallHoriz; // Wall is standing at point x,Vert x= horiz, Vert = vert.
      private int WallVert;

      public Image PersonGreen;
      Graphics g;

      public Main() {


      try {

      * Note: Source image / graphic must be in same directory as jpr (executable) file to work
      File sourceimage = new File("Person-green.jpg");
      PersonGreen = ImageIO.read(sourceimage); // works.

      } catch (IOException e) {
      System.err.println("Failed to load PersonGreen. Caught IOException: " + e.getMessage());


      JMenu file = new JMenu("File");

      JMenuItem exitItem = new JMenuItem("Exit");

      //adding action listener to menu items

      new ActionListener(){
      public void actionPerformed(ActionEvent e)
      // System.out.println("Exit is pressed");
      JMenuBar bar = new JMenuBar();

      //Create and set up the window.
      JFrame frame = new JFrame("Floor");

      frame.setSize(1000, 1000);
      // current problem - menu looks OK, but graphic not appearing on screen''
      // graphic is read correctly - proved that.

      public void update(Graphics g){
      public void paint(Graphics g) {
      g.drawImage(PersonGreen, 20, 20, null);


      public static void main(String[] args) {
      Main app = new Main();


      Many Thanks
        • 1. Re: Help requested with displaying a graphic on the screen
          I see you implementing paint(); you should be overriding paintComponent() in stead and don't forget to call super.paintComponent().

          Perhaps you will want to visit the chapter on custom painting before you continue.


          EDIT: further consideration is to not do the custom painting in JFrame directly, do it in a JPanel which is a child of the JFrame.