4 Replies - 318 Views - Last Post: 05 June 2014 - 04:30 PM Rate Topic: -----

#1 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Catch Chewbacca game

Posted 05 June 2014 - 10:01 AM

I am creating a game called catch Chewbacca. What will happen is that a picture of Chewbacca will flash up on the screen and you have to click on the icon before it changes to its next position on the screen. I am trying to make it work by using a timer to repaint the image of Chewbacca every second. I am having trouble with the mouse listener part. I am not sure how to make it so that when I click inside the image it will add 1 to the number of times you caught Chewbacca. Here is my unfinished code.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CatchChewyPanel extends JPanel
{
	private final int WIDTH = 400, HEIGTH = 100;
	private final int DELAY = 1000, IMAGE_SIZE = 35;
	public static int NumberOfTimes;
	private ImageIcon image;
	private Timer timer;
	private int x,y,xPos,yPos;
	private Point point1;
	public CatchChewyPanel()
	{
		timer = new Timer(DELAY, new ReboundListener());
		addMouseListener(new CatchListener());
		 
		image = new ImageIcon("Chewbacca.gif");
		
		x = 0;
		y = 40;
		
		setPreferredSize (new Dimension(WIDTH,HEIGHT));
		setBackground(Color.black);
		timer.start();
	}
	
	
	public void paintComponent (Graphics page)
	{
		super.paintComponent(page);
		image.paintIcon(this, page, x, y);
		page.drawString("Number of times caught" + NumberOfTimes,5,15);
		
	}
	private class ReboundListener implements ActionListener
	{
		public void actionPerformed (ActionEvent event)
		{
			x = (int)(Math.random()*400)+1;;
			y = (int)(Math.random()*100)+1;
		
			repaint();
		}
	
	}
	
	private class CatchListener implements MouseListener
	{
		
		public void MousePressed(MouseEvent event)
		{	point1 = event.getPoint();
			if (point1 == x)
				NumberOfTimes+=1;
			
			
			
			
		}
		
		
		
		


Is This A Good Question/Topic? 0
  • +

Replies To: Catch Chewbacca game

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Catch Chewbacca game

Posted 05 June 2014 - 10:25 AM

You need to determine whether or not the mouse click coordinates are inside the bounds of the image that is being drawn, not just whether or not they equal the coordinates.

You have all you need to do this - the coordinates of the mouse click in event.getPoint() (use the getX() and getY() methods of it). You then have the x and y coordinates of where the image is currently drawn to along with the size of the image. Imagine a box at this location that is IMAGE_SIZE large. You need to determine if the x and y of the mouse click are inside the box of the image.

The contains method of the Rectangle class may also come in handy if you want to pursue that route (create a Rectangle representing the image at the current position (there is a constructor to do that) and call the contains method on it passing in the point from the mouse click).
Was This Post Helpful? 1
  • +
  • -

#3 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Catch Chewbacca game

Posted 05 June 2014 - 01:27 PM

View PostRyano121, on 05 June 2014 - 10:25 AM, said:

You need to determine whether or not the mouse click coordinates are inside the bounds of the image that is being drawn, not just whether or not they equal the coordinates.

You have all you need to do this - the coordinates of the mouse click in event.getPoint() (use the getX() and getY() methods of it). You then have the x and y coordinates of where the image is currently drawn to along with the size of the image. Imagine a box at this location that is IMAGE_SIZE large. You need to determine if the x and y of the mouse click are inside the box of the image.

The contains method of the Rectangle class may also come in handy if you want to pursue that route (create a Rectangle representing the image at the current position (there is a constructor to do that) and call the contains method on it passing in the point from the mouse click).


What is the syntax for creating a rectangle in java and what is the syntax for the contains method?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9502
  • View blog
  • Posts: 35,850
  • Joined: 12-June 08

Re: Catch Chewbacca game

Posted 05 June 2014 - 01:31 PM

I hate to be "that guy", but a bit of leg work is okay now and then.

bing/google: "java rectangle docs.oracle"
Was This Post Helpful? 1
  • +
  • -

#5 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Catch Chewbacca game

Posted 05 June 2014 - 04:30 PM

Ok thanks guys! I got it working!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1