简体   繁体   English

绘画没有重新粉刷

[英]Drawing not being repainted

I want to move a ball along the x-axis to either the right or the left, depending on which JButton the user clicks. 我想沿着x轴向右或向左移动一个球,具体取决于用户点击的JButton。 The ball is not moving when either of the JButtons is clicked. 单击任一JButton时,球不会移动。

I have used System.out.println() to try and find out what is wrong and the program is detecting clicks on the JButtons and changes the variable (int horizontal) which has to be changed to move the ball as well. 我已经使用System.out.println()来尝试找出错误,程序正在检测JButtons上的点击并更改变量(int horizo​​ntal),必须更改它以移动球。

MoveBallPanel class: MoveBallPanel类:

package test;

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

public class MoveBallPanel extends JPanel implements ActionListener {

// declarations

private JButton moveLeft;
private JButton moveRight;
private int center;
private int horizontal;

public MoveBallPanel() {

    // make elements

    moveLeft = new JButton("To the left");
    moveLeft.addActionListener(this);
    moveRight = new JButton("To the right");
    moveRight.addActionListener(this);

    // add to panel

    add(moveLeft);
    add(moveRight);
}

// to do when JButton is clicked

@Override
public void actionPerformed(ActionEvent e) {

    final int MOVE = 50;

    if (e.getSource() == moveLeft) {
        horizontal -= MOVE;
        // System.out.println("moveLeft JButton has been clicked");
        // System.out.println(horizontal);
    } else {
        horizontal += MOVE;
        // System.out.println("moveRight button has been clicked");
        // System.out.println(horizontal);

    }
    repaint();
}

// draw ball

@Override
public void paintComponent(Graphics g) {

    final int DIAMETER = 100;
    final int VERTICAL = (getHeight() / 2) - (DIAMETER / 2);

    center = getWidth() / 2;
    horizontal = center - DIAMETER / 2;

    super.paintComponents(g);
    g.setColor(Color.ORANGE);
    g.fillOval(horizontal, VERTICAL, DIAMETER, DIAMETER); 
    g.setColor(Color.BLACK);
    g.drawOval(horizontal, VERTICAL, DIAMETER, DIAMETER);
    g.drawOval(horizontal + DIAMETER / 4, VERTICAL, DIAMETER / 2, DIAMETER);

    }

}

MoveBall class: MoveBall类:

package test;

import javax.swing.*;

public class MoveBall extends JFrame {

public MoveBall() {

    // JFrame object

    JFrame frame = new JFrame();

    // JFrame properties

    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Move ball");
    frame.add(new MoveBallPanel());
    frame.setVisible(true);

}

    public static void main(String[] args) {
        new MoveBall();
    }

}

Look at your two pieces of code... 看看你的两段代码......

@Override
public void actionPerformed(ActionEvent e) {
    final int MOVE = 50;
    if (e.getSource() == moveLeft) {
        horizontal -= MOVE;
    } else {
        horizontal += MOVE;

    }
    repaint();
}

In your actionPerformed method, you are updating the horizontal value, that sounds reasonable, but in your paintComponent method you are reassigning the value with something else, overriding what ever the actionPerformed method did... 在你的actionPerformed方法中,你正在更新horizontal值,这听起来很合理,但在你的paintComponent方法中,你用其他东西重新分配值,覆盖actionPerformed方法所做的事情......

public void paintComponent(Graphics g) {
    //...
    horizontal = center - DIAMETER / 2;
    //...
}

Start by taking a look at Performing Custom Painting and Painting in AWT and Swing to understand how painting works in Swing. 首先来看看在AWT和Swing执行自定义绘画绘画,以了解绘画在Swing中的工作原理。

What you need to do is create some kind of flag which can be used to tell the paintComponent method to initialise the horizontal to a default position, once, then let the program update the value from there... 你需要做的是创建某种标志,可用于告诉paintComponent方法将horizontal初始化为默认位置,然后让程序从那里更新值...

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

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