首页 > 解决方案 > 如何每秒重绘一个组件?

问题描述

我正在尝试创建一个小游戏,用户需要在其中单击正方形 x 秒。方块应该在鼠标点击或 1 秒后消失,最后我应该显示他获得的最终分数。我在每秒重新绘制正方形时遇到了麻烦。

import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;



public class RectangleDemo extends JPanel{
    JLabel label;

    void buildUI(Container container) {
        container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));

        RectangleArea rectangleArea = new RectangleArea(this);
        container.add(rectangleArea);

        label = new JLabel("Click within the framed area.");
        container.add(label);

        //Align the left edges of the components.
        rectangleArea.setAlignmentX(LEFT_ALIGNMENT);
        label.setAlignmentX(LEFT_ALIGNMENT); //unnecessary, but doesn't hurt
    }

    public void updateLabel(Point point) {
        label.setText("Click occurred at coordinate ("
                + point.x + ", " + point.y + ").");
    }

    //Called only when this is run as an application.
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        RectangleDemo controller = new RectangleDemo();
        controller.buildUI(f.getContentPane());
        f.pack();
        f.setVisible(true);
    }
}

class RectangleArea extends JPanel implements ActionListener{
    Random rand = new Random();
    int a = rand.nextInt(400);
    int b = rand.nextInt(400);
    Point point = new Point(a,b);
    RectangleDemo controller;
    Dimension preferredSize = new Dimension(500,500);
    int rectWidth = 50;
    int rectHeight = 50;
    Timer t;

    public void actionPerformed (ActionEvent e)
    {

    }

//final Container panou;

    public RectangleArea(RectangleDemo controller) {
        this.controller = controller;
        t=new Timer(1000, this); t.start();
        Border raisedBevel = BorderFactory.createRaisedBevelBorder();
        Border loweredBevel = BorderFactory.createLoweredBevelBorder();
        Border compound = BorderFactory.createCompoundBorder
                (raisedBevel, loweredBevel);
        setBorder(compound);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                if ((point.x<=x)&&(x<=point.x+50)&&(point.y<=y)&&(y<=point.y+50))
                {point=null;t.stop();}
                repaint();

            }
        });
    }

    public Dimension getPreferredSize() {
        return preferredSize;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);  //paint background

        //Paint a filled rectangle at user's chosen point.
        if (point != null) {
            g.drawRect(point.x, point.y,
                    rectWidth - 1, rectHeight - 1);
            g.setColor(Color.yellow);
            g.fillRect(point.x + 1, point.y + 1,
                    rectWidth - 2, rectHeight - 2);

            controller.updateLabel(point);
        }
    }
}

同样在我单击第一个方块后,我收到以下错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at RectangleArea$1.mousePressed(RectangleDemo.java:82)

标签: javaswing

解决方案


推荐阅读