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

    Help requested with displaying a graphic on the screen

    1012467
      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() {

      super("Floor");

      /////////////////////
      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");
      file.setMnemonic('F');

      JMenuItem exitItem = new JMenuItem("Exit");
      exitItem.setMnemonic('x');
      file.add(exitItem);

      //adding action listener to menu items

      exitItem.addActionListener(
      new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
      // System.out.println("Exit is pressed");
      System.exit(0);
      }
      }
      );
      JMenuBar bar = new JMenuBar();
      bar.add(file);

      //Create and set up the window.
      JFrame frame = new JFrame("Floor");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setJMenuBar(bar);
      paint(g);

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

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

      }

      public static void main(String[] args) {
      Main app = new Main();
      app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }

      }
      ----------------------------------

      Many Thanks
      HtB
        • 1. Re: Help requested with displaying a graphic on the screen
          gimbal2
          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.

          http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html


          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.