简体   繁体   中英

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. 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.

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. 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. Then, inside your keyReleased method you put in a call to the child widget's repaint method. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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