2

I'm trying to draw a sprite onto a Jpanel using the paintComponent method, for a game I'm creating. I'm using Toolkit to get the image. The image is not showing up and I don't know what the problem is. The sprite is in an array because I will be using different sprites for the same function.

Here is my class:

import javax.swing.SwingUtilities;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;


public class Game {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public Game() {
        createAndShowGUI();
    }

    private static void createAndShowGUI() {
        System.out.println("Game start: " + SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Black Circle");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }

}

class MyPanel extends JPanel {

    private int circleX = 500;
    private int circleY = 250;
    private int circleW = 20;
    private int circleH = 20;
    private int hiltX1 = 530;
    private int hiltY1 = 250;
    private int hiltX2 = 536;
    private int hiltY2 = 250;
    private int bladeX1 = 533;
    private int bladeY1 = 255;
    private int bladeX2 = 533;
    private int bladeY2 = 210;
    Image [] weapon = new Image [10];


    public int OFFSET = 15;


    public MyPanel() {

        setFocusable(true);
        requestFocusInWindow();

        setBorder(BorderFactory.createLineBorder(Color.black));
        weapon[0] = 
                   Toolkit.getDefaultToolkit().
                       getImage("knightsword.jpg");

        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                int keyCode = e.getKeyCode();

                if (keyCode == KeyEvent.VK_D) {

                    movecircleRight();
                } 
                else {
                    e.consume();
                }
                 if (keyCode == KeyEvent.VK_A) {

                    moveCircleLeft();
                } 
                 else {
                        e.consume();
                    }
             if (keyCode == KeyEvent.VK_W) {

                    moveCircleUp();
                } 
             else {
                    e.consume();
                }
                 if (keyCode == KeyEvent.VK_S) {

                    moveCircleDown();
                } 
                else {
                    e.consume();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                int keyCode = e.getKeyCode();
                e.consume();

            }
        });

    }

    private void movecircleRight() {




                circleX += OFFSET;
                bladeX1 += OFFSET;
                hiltX1 += OFFSET;
                bladeX2 += OFFSET;
                hiltX2 += OFFSET;

                repaint();


        }
    private void moveCircleLeft() {




                circleX -= OFFSET;
                bladeX2 -= OFFSET;
                hiltX2 -= OFFSET;
                bladeX1 -= OFFSET;
                hiltX1 -= OFFSET;
                repaint();


        }
    private void moveCircleDown() {




                circleY +=OFFSET;
                bladeY1 += OFFSET;
                hiltY1 += OFFSET;
                bladeY2 += OFFSET;
                hiltY2 += OFFSET;
                repaint();


        }
    private void moveCircleUp() {





                circleY -= OFFSET;
                bladeY2 -= OFFSET;
                hiltY2 -= OFFSET;
                bladeY1 -= OFFSET;
                hiltY1 -= OFFSET;
                repaint();


        }



    public Dimension getPreferredSize() {
        return new Dimension(1000, 500);
    }
//ImageIcon sword = new ImageIcon( ("knightsword.jpg"));
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.BLACK);
        g.fillOval(circleX, circleY, circleW, circleH);
        g.setColor(Color.BLACK);
        g.drawOval(circleX, circleY, circleW, circleH);
        g.drawLine(hiltX1, hiltY1, hiltX2, hiltY2); // hilt
        g.drawLine(bladeX1, bladeY1, bladeX2, bladeY2); // blade
        g.drawImage(weapon[0], 200, 250, this);
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Milo
  • 43
  • 4
  • 3
    A single blank line of white space is always enough. – Andrew Thompson Jun 13 '13 at 14:14
  • 2
    Did you check using debugger or console output that your `paintComponent` called? – Mikita Belahlazau Jun 13 '13 at 14:31
  • @NikitaBeloglazov While I don't think that is the exact problem here, it is still *excellent* advice. – Andrew Thompson Jun 13 '13 at 14:46
  • 1
    I am too sure, you will love to walk through this post by @camickr, [Motion Using the Keyboard](http://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/), since KeyListeners are not meant for Swing, [KeyBinding](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) is the way to go with Swing :-) – nIcE cOw Jun 13 '13 at 17:11
  • 1
    Thanks for the advice guys. The console unfortunately, doesn't output any errors. >.< That post will probably help me in the future. Might tweak my code to use KeyBinding once I finish this project. – Milo Jun 13 '13 at 18:01
  • 1
    @Milo : Please have a look at this post regarding how to [Add Images to your Project](http://stackoverflow.com/a/9866659/1057230), hopefully this might be able to help somewhat :-) – nIcE cOw Jun 13 '13 at 18:09
  • 1
    Thanks the links helped, I got my image working by using a bit of everyone's advice, haha! – Milo Jun 13 '13 at 18:57
  • @Milo : You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Jun 14 '13 at 02:01

1 Answers1

4

Your example:

  • Works If I replace "knightsword.jpg" with URL("https://i.stack.imgur.com/gJmeJ.png") (some tweaks to the code are necessary).
  • Fails silently if it remains as "knightsword.jpg".

Conclusion: The image is not being found.

This is one of the reasons I prefer to use ImageIO.read(..) for loading images. When it fails, it provides lots of lovely stack trace to warn us. :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433