Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2011, 04:06 PM   PM User | #1
bumblefoot89
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bumblefoot89 is an unknown quantity at this point
Java MouseEvents

Hello, i am having to study Java at the moment, i am actually a digital artist but i have to take a java course aswell to get some programming knowledge. I have been given an assignment to do which consists of 10 tasks. I have completed tasks 1-6 and 8-10. I am having trouble however with the 7th. I understand what i have to do i just cant seem to get it to work.

The program is the beginnings of a connect 4 game and i have code so that when i click in the grid i have created it will return a number (1,2 or 3) to say which mouse button was used to click. At the moment, any click will place a red counter in any of the squares, but i want to make it so that a yellow circle will be placed when a right click is made. My code is below, any help will be greatly appreciated, thanks.

Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.*;


class GridPanel extends JPanel implements MouseListener
{
	
	
	public void mouseReleased(MouseEvent event)
	{
		System.out.println("Mouse Released");
	}
	public void mousePressed(MouseEvent event)
	{
		System.out.println("Mouse Pressed");
	}
	public void mouseClicked(MouseEvent event)
	{
			int x =	event.getX();
			int y =	event.getY();
			int w = getWidth();
			int h = getHeight();
			int click = event.getClickCount();
			
			
			
			col_index = x*numCols/w;
			row_index = y*numRows/h;
			

			System.out.println("the x position is " + x);
			System.out.println("the y position is " + y);
			System.out.println("Column clicked" + col_index);
			System.out.println("Row Clicked" + row_index);
			
			System.out.println("The button clicked was " + event.getButton());
			System.out.println("Amount of times button was clicked: " + event.getClickCount());
			System.out.println("Shift or Control key hold, True or False: " + event.isControlDown());
			
			repaint();
			
	}
	public void mouseEntered(MouseEvent event)
	{
		System.out.println("Mouse Entered");
	}
	public void mouseExited(MouseEvent event)
	{
		System.out.println("Mouse Exited");
	}
	
	int numCols;
	int numRows;
	
	int col_index=0;
	int row_index=0;

	public GridPanel(int nc, int nr)
	{
		numCols = nc;
		numRows = nr;
		addMouseListener(this);
	}

	Rectangle getRect(int thisCol, int thisRow)
	{
		// if input is out of range, return "null"
		if(thisCol <0 || thisRow < 0)
			return null;
		if(thisCol>=numCols || thisRow>=numRows)
			return null;

		// otherwise, make and return the Rectangle
		int w = getWidth()/numCols;
		int h = getHeight()/numRows;

		int x = thisCol*w;
		int y = thisRow*h;

		Rectangle myRect = new Rectangle(x,y,w,h);
		return myRect;
	}

	public void paint(Graphics g)
	{
		g.setColor(Color.gray);
		g.fillRect(0,0,getWidth(), getHeight());
		g.setColor(Color.black);


		Graphics2D g2 = (Graphics2D)g;
		// we'll use Graphics2D for it's "draw" method -
		// neater than the Graphics "drawRect" suppled
		// (which you could also use)

		for (int i = 0;i<numCols;i++)
		{
			for(int j = 0;j<numRows;j++)
			{
				Rectangle r = getRect(i,j);
				g2.draw(r);
			}
			
			if (col_index>-1)
			{
				Rectangle r = getRect(col_index, row_index);
				g.setColor(Color.red);
				g.fillOval(r.x,r.y,r.width, r.height);
				g.setColor(Color.black);
				g.drawOval(r.x,r.y,r.width, r.height);
			}
		
		}
	}


	// copied from the W2MouseEvents for convenience
	// (so we can run the program from GridPanel class too!)
	public static void main(String[] args)
	{
		W2MouseEvents w = new W2MouseEvents();
		w.setVisible(true);
	}

}
bumblefoot89 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:17 AM.


Advertisement
Log in to turn off these ads.