Trying_To_Understand
Trying_To_Understand

Reputation: 161

Trying to draw an image icon in graphics but it wont work

I'm trying to draw an image icon in graphics but for some reason I can't draw the image. I think I've done everything correct, I don't know why it doesn't work.

I also tried to change from ImageIcon to BufferedImage and it also doesn't work.

Any suggestions?
this is my code:

package game;

import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) 
  {

        JFrame frame = new JFrame ("Space Ship Game");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DirectionPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
  }

}

And

package game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class DirectionPanel extends JPanel
{

    private final int WIDTH = 1300, HEIGHT = 900;
    private final int JUMP = 10; 
    private final int IMAGE_SIZE = 31;
    private ImageIcon spaceShipImage;
    private int x, y;

    public DirectionPanel()
    {
        addKeyListener (new DirectionListener());

        x = WIDTH /2;
        y = HEIGHT /2;

        try
        {
            spaceShipImage = new ImageIcon(getClass().getResource("/2.gif"));
        }
        catch (Exception e)
        {
            System.out.println("sss");
        }
        setBackground(Color.BLACK);
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setFocusable(true);
    }

    public void paintComponenet (Graphics g)
    {
        super.paintComponent(g);
        spaceShipImage.paintIcon(this, g, x, y);
    }

    private class DirectionListener extends KeyAdapter
    {

      public void keyPressed (KeyEvent event)
      {

         switch (event.getKeyCode())
         {

            case KeyEvent.VK_UP:
                    System.out.println("Dsfdsf");
                    y -= JUMP;
               break;
            case KeyEvent.VK_DOWN:              
               y += JUMP;
               break;
            case KeyEvent.VK_LEFT:
               x -= JUMP;
               break;
            case KeyEvent.VK_RIGHT:
               x += JUMP;
               break;
         }

         repaint();
      }

    }
}

Upvotes: 0

Views: 1399

Answers (1)

STaefi
STaefi

Reputation: 4377

First of all you need to rename your paintComponenet method to paintComponent in order to override the paintComponent method of the super class. Names are very important in overriding methods. As Andrew Thompson suggested adding @Override on top of the method you are overriding is very handy to check the signature and typos. A method in child class must have the same signature of that method in the super class in order to override it. Only the access modifier can be increased.

After that if your image 2.gif is in the same package as your DirectionPanel class is, you should remove the / before the name of your 2.gif.

Good Luck.

Upvotes: 3

Related Questions