簡體   English   中英

如何使用Random()在JPanel中的隨機位置繪制圖像?

[英]How to use Random() to draw images on random place in JPanel?

我正在開發一個游戲,您需要單擊鼠標/鼠,單擊它會得到1分,鼠標/鼠會消失

如何使鼠標圖像出現在不同的位置?

我的游戲畫面如下: 鼠標抓游戲

我在此頁面上的代碼如下所示:

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");
            }
        }
}

您可以使用如下方法在特定范圍內創建隨機數:

public int random(int min, int max) {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM