为什么这段代码不能绘制和移动矩形?

时间:2014-01-22 01:02:26

标签: java event-handling mouseevent actionlistener

我写了一个关于简单游戏的代码,但它不起作用。我的意思是矩形没有出现因此我无法移动它。以及我从ide中得到的错误信息。我花了好几次试图解决它没有任何暗示。任何提示或建议都非常感谢.Thx。

我写的代码:

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



public class DataGame extends JFrame {
    // Create text fields for balls are left and time elapsed
    private JTextField jtfBallsRemain, jtfTimeElapsed;
    // Button "New game"
    private JButton jbtStartNewGame = new JButton("New game");

    private Game canvas = new Game();

    // the main method
    public static void main(String[] args) {
        JFrame frame = new DataGame();
        frame.setTitle("Test data game ");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(470, 300);
        frame.setVisible(true);
    }

    public DataGame() {
        // Use panel1 to hold text fields, labels and the button
        JPanel panel = new JPanel();

        // Add panel to south
        add(panel, BorderLayout.SOUTH);
        panel.add(new JLabel("Balls remain"));
        panel.add(jtfBallsRemain = new JTextField(4));
        panel.add(new JLabel("Time elapsed"));
        panel.add(jtfTimeElapsed = new JTextField(4));
        panel.add(jbtStartNewGame);

        jtfBallsRemain.setEditable(false);
        jtfTimeElapsed.setEditable(false);

        add(canvas, BorderLayout.CENTER); // Add canvas to centre

        // register listener
        jbtStartNewGame.addActionListener(new StartNewGame());

        canvas.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1)
                    canvas.moveUpRect();
                else if (e.getButton() == MouseEvent.BUTTON3)
                    canvas.moveDownRect();
            }
        });

    }

    class StartNewGame implements ActionListener { // inner class
        @Override
        public void actionPerformed(ActionEvent e) {
            canvas.StartNewGame();
        }
    }

}

class Game extends JPanel {
    //The ball
    private int radius = 10;
    private int x;
    private int y;
    private int dx = 5;
    private int dy = 5;

    //The rectangle
    private int w = 15;
    private int h = 80;
    private int dy1 = 5;
    private int y1 = ((getHeight() / 2) - (h / 2));
    private int x1 = (getWidth() - 50);

    private Timer timer = new Timer(20, new TimerListener());

    public void moveDownRect() {
        y1 += dy1;
        repaint();

    }

    public void moveUpRect() {
        y1 -= dy1;
        repaint();

    }

    public void StartNewGame() {

        timer.start();

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        g.fillOval(x, y, 2 * radius, 2 * radius);
        g.setColor(Color.BLUE);
        g.fillRect(x1, y1, w, h);
    }

    class TimerListener implements ActionListener { /*
                                                     * make the TimerListener an
                                                     * inner class of Ball,
                                                     * which allows it to access
                                                     * the variable and methods
                                                     * of Ball
                                                     */
        @Override
        public void actionPerformed(ActionEvent e) {

            if (x < 0 || x + (2 * radius) > getWidth()) {

                dx *= -1;

            }

            if (y < 0 || y + (2 * radius) > getHeight()) {

                dy *= -1;
            }

            x += dx;
            y += dy;

            repaint();
        }
    }

}

1 个答案:

答案 0 :(得分:2)

评估x1变量时;

private int x1 = (getWidth() - 50);

getWidth将返回0,这意味着x1将等于-50,将其移出屏幕

更好的解决方案可能是在主窗口可见后进行后初始化