如何使用Random()在JPanel中的随机位置绘制图像?

时间:2014-01-22 13:47:35

标签: java swing animation random

我正在制作一款游戏,你需要点击一只鼠标/鼠标,点击后你得到1分,鼠标/鼠会消失

如何让鼠标图像出现在不同的地方?

我的游戏屏幕看起来像这样: mouse catch game

此页面的代码如下:

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

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

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), null);
        g.drawImage(mouse, 10, 10, null);
    }

    private static final long serialVersionUID = 1L;

        Image background, muisje;
        JTextField input;
        JButton guess;
        JButton menu;

        Gamescreen() {
        setLayout(null);

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

        ImageIcon icon2 = new ImageIcon(this.getClass().getResource("assets/muisje.png"));
        mouse = 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 );

        // Input field
        input = new JTextField(10);
        input.setLayout(null);
        input.setBounds(150, 474, 290, 60); // change position at bottom of screen is int 1

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

        // Menu button
        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));

        // add to screen
        add(input);
        //add(guess);
        add(menu);

        menu.addActionListener(new ActionListener() {

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

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

        public void run() {
            // TODO Auto-generated method stub
            while(Gamestatus=="active"){
                System.out.println("Gameloop works");
            }
        }
}

1 个答案:

答案 0 :(得分:1)

您可以使用此类方法在特定范围内创建随机数:

public int random(int min, int max) {

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