Results 1 to 10 of 10
  1. #1
    child is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Unhappy Draw Graphics on top of another

    I am currently writing a program to display a movable object on top of a background image. However, the frame displays nothing :(
    Could anyone help me to have a look of the following program...
    thanks

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MyProject extends JFrame{
      private ImageIcon bg, img_player;
      boolean key_left=false, key_right=false, key_up=false, key_down=false;
      int x = 10, y = 10;
      char keyChar = 'A';
      public MyProject(){
        bg = new ImageIcon("background.jpg");
        img_player = new ImageIcon("player1.gif");
        addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
              case KeyEvent.VK_DOWN: key_down=true; break;
              case KeyEvent.VK_UP: key_up=true; break;
              case KeyEvent.VK_LEFT: key_left=true; break;
              case KeyEvent.VK_RIGHT: key_right=true; break;
              default: keyChar = e.getKeyChar();
            }
            repaint();
          }
          public void keyReleased(KeyEvent e){
            switch (e.getKeyCode()) {
              case KeyEvent.VK_DOWN: key_down=false; break;
              case KeyEvent.VK_UP: key_up=false; break;
              case KeyEvent.VK_LEFT: key_left=false; break;
              case KeyEvent.VK_RIGHT: key_right=false; break;
              default: keyChar = e.getKeyChar();
            }
            repaint();
          }
        });
        repaint();
      }
      public void paintComponent(Graphics g){
        // Draws the img to the BackgroundPanel
        super.paint(g);
        g.drawImage(bg.getImage(), 0, 0, null);
        if (key_up) y -= 10;
        if (key_down) y += 10;
        if (key_right) x += 10;
        if (key_left) x -= 10;
        g.drawImage(img_player.getImage(), x, y, null);
      }
      
      public static void main(String[] args){
        MyProject frame = new MyProject();
        frame.setTitle("Game");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);
      }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    As you could've read in the API documentation for the JFrame class, you can't add anything, nor draw anything on that component; you have to use, or set a new ContentPane and use that component for all your drawing operations.

    kind regards,

    Jos

  3. #3
    child is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    how should i change the program?
    i have just tried to simply add a panel to do the drawing and add the panel to the frame...
    but it seems to have some errors

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by child View Post
    how should i change the program?
    i have just tried to simply add a panel to do the drawing and add the panel to the frame...
    but it seems to have some errors
    Well, given the little information you supplied all I can say is: you did something wrong; definitely.

    kind regards,

    Jos

  5. #5
    child is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    could you read my program once again
    many thanks~

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MyProject extends JFrame{
      private Platform platform = new Platform();
      public MyProject(){
        add(platform);
      }
      public class Platform extends JPanel{
        private ImageIcon bg, img_player;
        private boolean key_left=false, key_right=false, key_up=false, key_down=false;
        private int x = 10, y = 10;
        private char keyChar = 'A';
        public Platform(){
          bg = new ImageIcon("background.jpg");
          img_player = new ImageIcon("player1.gif");
          addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
              switch (e.getKeyCode()) {
                case KeyEvent.VK_DOWN: key_down=true; break;
                case KeyEvent.VK_UP: key_up=true; break;
                case KeyEvent.VK_LEFT: key_left=true; break;
                case KeyEvent.VK_RIGHT: key_right=true; break;
                default: keyChar = e.getKeyChar();
              }
              repaint();
            }
            public void keyReleased(KeyEvent e){
              switch (e.getKeyCode()) {
                case KeyEvent.VK_DOWN: key_down=false; break;
                case KeyEvent.VK_UP: key_up=false; break;
                case KeyEvent.VK_LEFT: key_left=false; break;
                case KeyEvent.VK_RIGHT: key_right=false; break;
                default: keyChar = e.getKeyChar();
              }
              repaint();
            }
          });
        }
        public void paintComponent(Graphics g){
          // Draws the img to the BackgroundPanel
          super.paintComponent(g);
          g.drawImage(bg.getImage(), 0, 0, null);
          if (key_up) y -= 10;
          if (key_down) y += 10;
          if (key_right) x += 10;
          if (key_left) x -= 10;
          g.drawImage(img_player.getImage(), x, y, null);
        }
      }
      public static void main(String[] args){
        JFrame frame = new JFrame();
        MyProject p = new MyProject();
        frame.add(p);
        frame.setTitle("Project Game");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);
      }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    MyProject extends JFrame, and then in your main method you create a JFrame and add a MyProject object to the JFrame -- in other words you are trying to add a JFrame to a JFrame which you shouldn't be doing.

    Have you tread the Graphics tutorials on the Sun Java tutorial site? If not, I think that they would help you immensely.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You other issue is that you are using key listeners on a JPanel which does not have focus mainly because its focusable property is false. Without focus, the key listeners will simply not work.

    I know of two possible solutions to this, and the first which is quick and dirty involves just calling setFocusable(true) on your JPanel:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class MyProject extends JFrame {
      private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
      private static final String IMAGE_PATH_PLAYER = "http://duke.kenai.com/iconSized/duke4.gif";
      
      private Platform platform = new Platform();
    
      public MyProject() {
        add(platform);
      }
    
      public class Platform extends JPanel {
        private ImageIcon bg, img_player;
        private boolean key_left = false, key_right = false, key_up = false,
            key_down = false;
        private int x = 10, y = 10;
        private char keyChar = 'A';
    
        public Platform() {
          //!!bg = new ImageIcon("background.jpg");
          setFocusable(true);
          try {
            bg = new ImageIcon(new URL(IMAGE_PATH));
            img_player = new ImageIcon(new URL(IMAGE_PATH_PLAYER));
            int w = bg.getIconWidth();
            int h = bg.getIconHeight();
            setPreferredSize(new Dimension(w, h));
          } catch (MalformedURLException e1) {
            e1.printStackTrace();
          }
          //!!img_player = new ImageIcon("player1.gif");
          addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
              switch (e.getKeyCode()) {
              case KeyEvent.VK_DOWN:
                key_down = true;
                break;
              case KeyEvent.VK_UP:
                key_up = true;
                break;
              case KeyEvent.VK_LEFT:
                key_left = true;
                break;
              case KeyEvent.VK_RIGHT:
                key_right = true;
                break;
              default:
                keyChar = e.getKeyChar();
              }
              repaint();
            }
    
            public void keyReleased(KeyEvent e) {
              switch (e.getKeyCode()) {
              case KeyEvent.VK_DOWN:
                key_down = false;
                break;
              case KeyEvent.VK_UP:
                key_up = false;
                break;
              case KeyEvent.VK_LEFT:
                key_left = false;
                break;
              case KeyEvent.VK_RIGHT:
                key_right = false;
                break;
              default:
                keyChar = e.getKeyChar();
              }
              repaint();
            }
          });
        }
    
        public void paintComponent(Graphics g) {
          // Draws the img to the BackgroundPanel
          super.paintComponent(g);
          g.drawImage(bg.getImage(), 0, 0, null);
          if (key_up)
            y -= 10;
          if (key_down)
            y += 10;
          if (key_right)
            x += 10;
          if (key_left)
            x -= 10;
          g.drawImage(img_player.getImage(), x, y, null);
        }
      }
    
      public static void main(String[] args) {
        MyProject p = new MyProject();
        p.pack();
        p.setLocationRelativeTo(null); // Center the p
        p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //p.setSize(300, 200);
        p.setVisible(true);
      }
    }
    Perhaps a better solution is to use key binding since with this, you don't have to worry if the JPanel goes out of focus.

  8. #8
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    You may want to look at JLayeredPane.

  9. #9
    child is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    thank you everyone :)
    I finally know my problemsssss.....

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's my example that uses key bindings: Java pastebin - collaborative debugging tool

    Note that I could not paste the code in here because the forum software considers key binding in this situation to be malicious code (I'm guessing).
    Last edited by Fubarable; 01-17-2010 at 06:32 PM.

Similar Threads

  1. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 11:48 AM
  2. Trying to use Graphics draw methods
    By Lang in forum SWT / JFace
    Replies: 5
    Last Post: 03-26-2008, 05:49 AM
  3. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •