For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2006 > mouse event









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author mouse event
andrewtitus@alltel.net

2006-02-23, 9:58 pm

I made a square out of a polygon and when I click on one the color should
change for that polygon only. I made the polygon and then transcoded it
whith a loop to make tic tac toe board. The only square that I can get to
change colors is the top right corner one but when it does they all change
colors. How can I get this to read the transcoded polys with the contains
method for that class.

Thank You,
Andy

import javax.swing.JApplet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TicTacToe extends JApplet
{
final int POINTS = 4;
final int SQUARES = 9;
boolean inside = false;
int[] xPoints = {20,140,260,20,20,140,140,260,260};
int[] yPoints = {20,20,20,140,260,140,260,140,260};




//Draws a square using a polygon
Polygon poly = new Polygon(
new int[]{20,20,100,100},
new int[]{20,100,100,20},POINTS);


public void paint (Graphics page)
{

setBackground (Color.black);
setPreferredSize (new Dimension(400,400));

int width = getSize().width;
int height= getSize().height;

addMouseListener( new getPoint() );

page.setColor(Color.white);


for (int i=0; i<SQUARES; i++) {
page.translate(xPoints[i],yPoints[i]);
if(inside == true) {
page.setColor(Color.red);
page.fillPolygon(poly);
}
page.fillPolygon(poly);
page.translate(-xPoints[i],-yPoints[i]);
}

}

private class getPoint implements MouseListener
{

public void mousePressed(MouseEvent mouse) {

if ( poly.contains(mouse.getX(), mouse.getY()) )
{
inside = true;

}
repaint();
System.out.println(mouse.getX()+" "+mouse.getY());
}
public void mouseReleased(MouseEvent mouse) {}
public void mouseClicked(MouseEvent mouse) {}
public void mouseEntered(MouseEvent mouse) {}
public void mouseExited(MouseEvent mouse) {}

}

}
Steve W. Jackson

2006-02-24, 7:01 pm

In article <zsqdndkGMqJIHGPenZ2dnUVZ_v-dnZ2d@giganews.com>,
andrewtitus@alltel.net wrote:

> I made a square out of a polygon and when I click on one the color should
> change for that polygon only. I made the polygon and then transcoded it
> whith a loop to make tic tac toe board. The only square that I can get to
> change colors is the top right corner one but when it does they all change
> colors. How can I get this to read the transcoded polys with the contains
> method for that class.
>
> Thank You,
> Andy
>
> import javax.swing.JApplet;
> import java.awt.*;
> import java.awt.event.*;
> import java.util.*;
>
> public class TicTacToe extends JApplet
> {
> final int POINTS = 4;
> final int SQUARES = 9;
> boolean inside = false;
> int[] xPoints = {20,140,260,20,20,140,140,260,260};
> int[] yPoints = {20,20,20,140,260,140,260,140,260};
>
>
>
>
> //Draws a square using a polygon
> Polygon poly = new Polygon(
> new int[]{20,20,100,100},
> new int[]{20,100,100,20},POINTS);
>
>
> public void paint (Graphics page)
> {
>
> setBackground (Color.black);
> setPreferredSize (new Dimension(400,400));
>
> int width = getSize().width;
> int height= getSize().height;
>
> addMouseListener( new getPoint() );
>
> page.setColor(Color.white);
>
>
> for (int i=0; i<SQUARES; i++) {
> page.translate(xPoints[i],yPoints[i]);
> if(inside == true) {
> page.setColor(Color.red);
> page.fillPolygon(poly);
> }
> page.fillPolygon(poly);
> page.translate(-xPoints[i],-yPoints[i]);
> }
>
> }
>
> private class getPoint implements MouseListener
> {
>
> public void mousePressed(MouseEvent mouse) {
>
> if ( poly.contains(mouse.getX(), mouse.getY()) )
> {
> inside = true;
>
> }
> repaint();
> System.out.println(mouse.getX()+" "+mouse.getY());
> }
> public void mouseReleased(MouseEvent mouse) {}
> public void mouseClicked(MouseEvent mouse) {}
> public void mouseEntered(MouseEvent mouse) {}
> public void mouseExited(MouseEvent mouse) {}
>
> }
>
> }


Think object-oriented. Make each of your squares separate objects in
themselves, rather than drawn squares on a larger palette. Each one can
listen to the relevant mouse actions and respond accordingly. Each one
can change its color without affecting the others. If they should have
need of knowledge of each other, you may have broken the rules of OO.
For that you should consider a controller (thinking in terms of the
model-view-controller design concept). And it could call methods on
each to get or set state (color or other) on them.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com