在java中更新后调用绘图图形

时间:2016-07-13 11:44:03

标签: java

问题:

每次按下w,a,s或d后,用户x和y应该改变,方形被绘制为x和y,意味着图形没有更新。我的问题是你如何更新它?我尝试过在任何地方以各种可能的方式使用重绘,并开始认为这不是问题。有人能告诉我我做错了什么或如何解决问题。我是Java的新手,请原谅我,如果我犯了一个非常简单的错误。

代码:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class KeyPressDrawing extends JFrame implements KeyListener{
    // define variables
    public static int user_x = 50;
    public static int user_y = 50;

    // init
    public KeyPressDrawing(String s){
        super(s);
        JPanel area = new JPanel();
        add(area);
        setSize(800, 800);
        setVisible(true);
        addKeyListener(this);
        setResizable(false);
    }

    // draw
    public void paint(Graphics g) {
        g.setColor (Color.black);
        // x position, y position, width length, height length
        g.fillRect(KeyPressDrawing.user_x, KeyPressDrawing.user_y, 50, 50);
        repaint();
}

    // check and change

    public void keyTyped(KeyEvent e) {}


    public void keyPressed(KeyEvent e) {}


    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            System.out.println("Up");
            KeyPress.user_y -= 20;

        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            System.out.println("Left");
            KeyPress.user_x -= 20;

        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            System.out.println("Down");
            KeyPress.user_y += 20;

        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            System.out.println("Right");
            KeyPress.user_x += 20;
            }
        }

    public static void main(String[] args){
        new KeyPressDrawing("Control Panel");
    }
}

1 个答案:

答案 0 :(得分:2)

您的代码存在一些问题。首先,您将JPanel作为JFrame中的主要窗口小部件放置,但是您将覆盖JFrame本身的 paint 方法,并且永远不会将调用委托给超类,因此不会呈现JPanel。您可能想要做的是将矩形的绘图移动到JPanel子类中,该子类是框架的主子窗口小部件。然后,在 keyReleased 方法中,您调用了子窗口小部件的重绘方法。每次密钥释放后,子面板将被告知需要重新绘制。删除JFrame的 paint 方法并使用默认值。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;


public class Sample extends JFrame implements KeyListener {
  public int user_x;
  public int user_y;
  public Canvas canvas;

  class Canvas extends JPanel{
    public Canvas() {
      setSize(getPreferredSize());
      Canvas.this.setBackground(Color.white);
      user_x = 10;
      user_y = 10;
    }

    @Override
    public final Dimension getPreferredSize() {
      return new Dimension(600, 600);
    }

    @Override
    public final void paintComponent(final Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;
      Ellipse2D circle = new Ellipse2D.Double(0d, 0d, 100d, 100d);
      g2d.setColor(Color.red);
      g2d.translate(user_x, user_y);
      g2d.draw(circle);
      g2d.fill(circle);
    }
  }

  public Sample() {
    super("Sample");
    setSize(300, 300);
    addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {System.exit(0);}
       public void windowOpened(WindowEvent e) {}
    });
    canvas = new Canvas();
    getContentPane().add(canvas);
    addKeyListener(this);
  }


// Implementation of java.awt.event.KeyListener

  public final void keyTyped(final KeyEvent keyEvent) {

  }

  public final void keyPressed(final KeyEvent keyEvent) {

  }

  public final void keyReleased(final KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W) {
      System.out.println("Up");
      user_y -= 20;
    }
    if (e.getKeyCode() == KeyEvent.VK_A) {
      System.out.println("Left");
      user_x -= 20;
    }
    if (e.getKeyCode() == KeyEvent.VK_S) {
      System.out.println("Down");
      user_y += 20;
    }
    if (e.getKeyCode() == KeyEvent.VK_D) {
      System.out.println("Right");
      user_x += 20;
    }
    //either canvas.repaint or just repaint for the frame will work here
    repaint();
  }

  public static final void main(final String[] args) {

     Sample f = new Sample();
     f.setVisible(true);
  }

} // Sample