2014-01-22 43 views
1

我正在做一個遊戲,你需要點擊一個鼠標/鼠標,點擊它後你得到1分,鼠標/鼠標會消失如何使用Random()在JPanel的隨機地方繪製圖像?

如何讓鼠標圖像出現在不同的地方?

我gamescreen看起來是這樣的: 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"); 
      } 
     } 
} 
+0

如果您可以將您使用的某些術語翻譯成英語,那可能會有幫助。但通常,你所做的是'x =隨機*寬度'和'y =隨機*高度',其中'random'是0和1之間的值。 – ADTC

+0

將所有荷蘭語單詞翻譯成英語。 –

+0

'while(Gamestatus ==「active」)' - [HDICSIJ](http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCkQFjAA&url=http%3A%2F %2Fstackoverflow.com%2Fquestions%2F513832%2Fhow-do-i-compare-strings-in-java&ei = qNLfUuXIK6iZiQftxYDYBQ&usg = AFQjCNE-bfxh2nRVFgcvyJyfOtbFkVXyCw&bvm = bv.59568121,d.aGc) –

回答

1

你可以用這樣的方法在特定的範圍內創建一個隨機數:

public int random(int min, int max) { 

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

然後:'g.drawImage(mouse,random 0,this.getWidth()),random(0,this.getHeight()),null);' – ADTC

+0

你必須考慮mose的大小以及 – Joschua

+0

啊,真的..'隨機(0,this。 getWidth() - mouse.getWidth())'我想?你能把所有這些添加到你的答案嗎? ':)' – ADTC

相關問題