简体   繁体   English

在Java中更新后调用绘制图形

[英]calling a paint graphic after update in java

Problem: 问题:

After each key press of w, a, s or d the users x and y should change and the square is drawn to the x and y meaning that the graphic is not updating. 每次按w,a,s或d后,用户x和y都应更改,并且在x和y上绘制正方形,这意味着图形不会更新。 My question is how do you update it ? 我的问题是如何更新它? I have tried using repaint everywhere and in every possible way and am starting to think that is not the issue. 我已经尝试过以各种可能的方式在任何地方使用重绘,并且开始认为这不是问题。 Can someone tell me what i am doing wrong or how to fix the issue. 有人可以告诉我我在做什么错或如何解决此问题。 I am pretty new to Java so excuse me if i did a really simple mistake. 我对Java很陌生,所以如果我犯了一个非常简单的错误,请原谅。

Code: 码:

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");
    }
}

There are a couple of problems with your code. 您的代码有两个问题。 First, you are placing a JPanel as the primary widget in the JFrame but then you override the paint method of the JFrame itself and never delegate the calls to the super class so your JPanel won't be rendered. 首先,您将JPanel放置为JFrame中的主窗口小部件,但是随后您重写了JFrame本身的paint方法,并且从不将调用委派给超类,因此不会呈现JPanel。 What you likely want to do is move the drawing of the rectangle into a JPanel subclass that is your frame's main child widget. 您可能想要做的是将矩形的图形移动到JPanel子类中,该子类是框架的主要子窗口小部件。 Then, inside your keyReleased method you put in a call to the child widget's repaint method. 然后,在keyReleased方法内部,调用子控件的repaint方法。 After each key release then the child panel will be told it needs to repaint. 每次释放键之后,子面板将被告知需要重新粉刷。 Remove the paint method of the JFrame and use the default. 删除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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM