单击后如何重新绘制图像?

时间:2014-01-25 13:41:48

标签: java image random mouseevent repaint

我正在制作一个你必须点击一个对象的游戏,如果你点击了右边你得到1分,依此类推。 如果你错过了点击对象,你得到-1分。

现在我想在点击它后重新绘制图像,现在只需点击一下,只有分数会改变。

这是我的代码:

(注意:我把导入器留下了,因为它会比OP中的一团糟)

public class Gamevenster extends JPanel implements Runnable {
        public String Gamestatus = "active";
        private Thread thread;
        //public Main game;

    public int random(int min, int max) {
         int range = (max - min) + 1;     
        return (int)(Math.random() * range) + min;
    }

    public class TestComponent extends JComponent implements MouseListener {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public TestComponent() {
            this.addMouseListener(this);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int clickedX = e.getX();
            int clickedY = e.getY();

            System.out.println("User Clicked: " + clickedX + ", " + clickedY);
        }

        @Override
        public void mousePressed(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {}

        @Override
        public void mouseEntered(MouseEvent e) {}

        @Override
        public void mouseExited(MouseEvent e) {}
    }

    Random rand = new Random();
    private int x = 0;
    private int y = 0;

    Timer timer = new Timer(850, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            x = rand.nextInt(400);
            y = rand.nextInt(330);
            repaint();
        }
    });

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(achtergrond, 0, 0, this.getWidth(), this.getHeight(), null);
        //g.drawImage(muisje, 10, 10, null);
        g.drawImage(muisje, x, y, 100, 100, this);
    }

    private static final long serialVersionUID = 1L;

    private JLabel scoreLabel = new JLabel("SCORE: " + "0");
    private int score = 0;

        Image achtergrond, muisje;
        //JTextField invoer;
        JButton raden;
        JButton menu;
        JButton restartBtn;


        Gamevenster() { // CONSTRUCTOR

        timer.start();

        setLayout(null);

        scoreLabel.setFont(new Font("Open Sans", Font.PLAIN, 30));
        scoreLabel.setForeground(Color.WHITE);
        scoreLabel.setBounds(20, 455, 200, 100);
        add(scoreLabel);

        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e) {
                Point p = e.getPoint();
                int clickX = (int)p.getX();
                int clickY = (int)p.getY();

                if(clickX > x && clickX < x + 100 && clickY > y && clickY  < y + 100) {
                    score++;
                    scoreLabel.setText("SCORE: " + score);
                    repaint();  
                }else{
                    score = Math.max(0, score -1);
                    scoreLabel.setText("SCORE: " + score);
                }

            }
        });

        ImageIcon icon = new ImageIcon(this.getClass().getResource("assets/achtergrondspel.png"));
        achtergrond = icon.getImage();      

        ImageIcon icon2 = new ImageIcon(this.getClass().getResource("assets/muisje.png"));
        muisje = icon2.getImage();   

        //Get the default toolkit  
        Toolkit toolkit = Toolkit.getDefaultToolkit();  

        //Load an image for the cursor  
        Image image = toolkit.getImage("src/assets/hand.png");  

        //Create the hotspot for the cursor  
        Point hotSpot = new Point(0,0);

        //Create the custom cursor  
        Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Hand");

        //Use the custom cursor  
        setCursor(cursor);

        // setLayout( null );

        // Invoer feld
        //invoer = new JTextField(10);
        //invoer.setLayout(null);
        //invoer.setBounds(150, 474, 290, 60); // Verander positie onder aan scherm is int 1

        // Button voor raden
        raden = new JButton("Raden");
        raden.setLayout(null);
        raden.setBounds(10, 474, 130, 60);
        raden.setFont(new Font("Dialog", 1, 20));
        raden.setForeground(Color.white);
        raden.setBackground(new Color(46, 204, 113));
        raden.setPreferredSize(new Dimension(130, 60));

        // Menu knop
        menu = new JButton("Menu");
        menu.setLayout(null);
        menu.setBounds(450, 474, 130, 60);
        menu.setFont(new Font("Dialog", 1, 20));
        menu.setForeground(Color.white);
        menu.setBackground(new Color(46, 204, 113));
        menu.setPreferredSize(new Dimension(130, 60));

        // Toevoegen aan screen
        //add(invoer);
        //add(raden);

        add(menu);

        menu.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
        String i = menu.getText();
        System.out.println("Er is gedrukt! " + i);
                }
            });
        }

        public void start(){
            thread = new Thread(this,"spelloop");
            thread.start();
        }

        public void run() {
            while(Gamestatus=="active"){
                System.out.println("Gameloop werkt");
            }
        }
}

0 个答案:

没有答案