2014-01-22 3 views
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 = random * width'와'y = random * height'입니다. 여기서'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-- 문자열 - 인 - 자바 비교 및 ​​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 (마우스, 임의 (0, this.getWidth()), random (0, this.getHeight()), null); ' – ADTC

+0

그리고 너도 그 크기를 고려해야한다. – Joschua

+0

아, 맞아. getWidth() - mouse.getWidth())'나는 생각하나요? 대답에 모두 덧붙일 수 있습니까? ':)' – ADTC

관련 문제