如何获取Graphics2D对象以使用我为KeyListener提供的代码移动

时间:2019-05-30 13:21:44

标签: java swing jframe keylistener

我无法使Graphics2D对象随WASD移动。此外,应将按下和释放的键输入到控制台中。这种分离效果很好,但是,我无法移动对象

我已经尝试将这段代码移到Background和Game类中。


public class Game extends JFrame implements KeyListener //Canvas is a blank work space to work and then override with Jfame
{
    KeyEvent asd = null;
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;
    public static final int SCALE = 3;

    BufferedImage myImage = null; //BufferedImage is a class used to handle data regarding an image
    Background p = new Background();

    public Game()
    {
        super ("Dungeons of Death");
    }
    public void start()
    {
        try
        {
            this.setLocation(200, 200); 
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
            this.setVisible(true); //sets the visibility of the component. It is by default false.
            this.setFocusable(true);  
            this.setResizable(false); //makes it so that the window size can not be adjusted
            this.pack(); //sizes the frame so that all its contents are at or above their preferred sizes.

            this.add(p);
            addKeyListener(this);

            while (true)
            {
                Thread.sleep(16);
                this.repaint();


            }
        }
        catch(Exception e)
        {
            e.printStackTrace();  //helps determine where the error is if there is one
        }
    }   
    public void keyTyped(KeyEvent evt)
    {

        int key = evt.getKeyCode();
        switch(key)
        {
            case KeyEvent.VK_W:
            {
                p.setyc(-1);
                System.out.println("" + evt.getKeyChar());  //for testing purposes
                break;
            }
            case KeyEvent.VK_A:
            {
                p.setxc(-1);
                System.out.println("" + evt.getKeyChar());
                break;
            }
            case KeyEvent.VK_S:
            {
                //yC++;
                p.setyc(1);
                System.out.println("" + evt.getKeyChar());
                break;
            }
            case KeyEvent.VK_D:
            {
                //xC++;
                p.setxc(1);
                System.out.println("" + evt.getKeyChar());
                break;
            }
            /*default:
            {
                System.out.prwintln("bruhhhh");
                break;
            }*/
        }
        p.drawImage(p.getGuy(),p.getxc(),p.getyc(), p.geth(), p.getw(),  new Color(0,0,0,0), p.gety())  ;
    }
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
    }
    public static void main (String [] args)
    {
        Game x = new Game();
        x.start();
    }

}

public class Background extends JComponent
{
    public Background()
    {
        //addKeyListener(this);
    }

    BufferedImage myImage = null;
    BufferedImage guy = null; 
    ImageObserver y = null;
    Graphics2D x = null;

    Component a = new Button();
    KeyEvent W = new KeyEvent(a,1,1,0, KeyEvent.VK_W, 'w' );
    KeyEvent A = new KeyEvent(a,1,1,0, KeyEvent.VK_A, 'a' );
    KeyEvent S = new KeyEvent(a,1,1,0, KeyEvent.VK_S, 's' );
    KeyEvent D = new KeyEvent(a,1,1,0, KeyEvent.VK_D, 'd' );

    int xC = 700;
    int yC = 350;
    int h = 150;
    int w = 150;

    private static final long serialVersionUID = 1L;

    public void paintComponent(Graphics g)
    {
        try
        {
            myImage = ImageIO.read(new URL("https://www.pennington.com/-/media/images/pennington-na/us/blog/seed/all-you-need-to-know-about-bermudagrass/bermuda-header.jpg")); //used to handle image data
            super.paintComponent(g);
            g.drawImage(myImage, 0, 0, this.getWidth(), this.getHeight()/2 + 300, this); //use this not null bc when method called first time image has not been loaded yet so error if null
            x = (Graphics2D) g;
            x.setColor(Color.GREEN); //just in case background is still there
            //x.fillOval(700, 400, 100, 100);
            guy = ImageIO.read(new URL("https://images-na.ssl-images-amazon.com/images/I/61O3LuvpDML._SY355_.png"));
            x.drawImage(guy,xC,yC, h, w,  new Color(0,0,0,0), y);
            //keyPress(A);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void setyc(int c)
    {
        yC +=c;
    }
    public void setxc(int c)
    {
        xC =+ c;
    }
    public int getxc()
    {
        return xC;
    }
    public int getyc()
    {
        return yC;
    }
    public BufferedImage getGuy()
    {
        return guy;
    }
    public int geth()
    {
        return h;
    }
    public int getw()
    {
        return w;
    }
    public ImageObserver gety()
    {
        return y;
    }
/*  public void keyTyped(KeyEvent evt)
    {

        int key = evt.getKeyCode();
        switch(key)
        {
            case KeyEvent.VK_W:
            {
                yC--;
                System.out.println("" + evt.getKeyChar());  //for testing purposes
                break;
            }
            case KeyEvent.VK_A:
            {
                xC--;
                System.out.println("" + evt.getKeyChar());
                break;
            }
            case KeyEvent.VK_S:
            {
                yC++;
                System.out.println("" + evt.getKeyChar());
                break;
            }
            case KeyEvent.VK_D:
            {
                xC++;
                System.out.println("" + evt.getKeyChar());
                break;
            }
            default:
            {
                System.out.println("bruhhhh");
                break;
            }
        }
        x.drawImage(guy,xC,yC, h, w,  new Color(0,0,0,0), y)    ;
    }
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
    }*/
}

预期结果是表情符号应该弹出并随WASD移动

实际结果是它只是停留在那儿,没有动弹

0 个答案:

没有答案