Please help me figure out how to use a listener.

import java.awt.Point;

import java.awt.event.*;

public class LineListener implements MouseListener{

private Point current;

public void mouseClicked(MouseEvent event){

current=event.getPoint();

}

public void mousePressed(MouseEvent event) {

}

public void mouseReleased(MouseEvent event) {

}

public void mouseEntered(MouseEvent event) {

}

public void mouseExited(MouseEvent event) {

}

public int getX(){

return current.x;

}

public int getY(){

return current..y;

}

public class tictactoeboard extends JPanel{

public tictactoeboard(){

LineListener listener=new LineListener();

simpleshapes s=new simpleshapes("tictactoe");

drawSquare(50,50,50);

addMouseListener(listener);

if(listener.getX()>25&&listener.getX()<=50&&listener.getY()>=25&&listener.getY()<=50){

s.drawLine(25,25,75,75);

s.drawLine(75,25,25,75);

}

}

public void drawSquare(int x, int y, int sf){

simpleshapes s=new simpleshapes("tictactoe");

s.drawLine(x,y,x+sf,y);

s.drawLine(x,y,x,y+sf);

s.drawLine(x+sf,y,x+sf,y+sf);

s.drawLine(x,y+sf,x+sf,y+sf);

}

}

__

public void paintComponent(Graphics g){

super.paintComponent(g);

for(int i = 0; i < numLines;++i){

g.drawLine(lines[0],lines[1],lines[2],lines[3]);

}

for(int i = 0; i < numOvals;++i){

g.drawOval(ovals[0],ovals[1],ovals[2],ovals[3]);

}

}

public void addLine(short x1, short y1, short x2, short y2){

lines[numLines][0] = x1;

lines[numLines][1] = y1;

lines[numLines][2] = x2;

lines[numLines][3] = y2;

++numLines;

}

public void addOval(short x, short y, short w, short h){

ovals[numOvals][0] = x;

ovals[numOvals][1] = y;

ovals[numOvals][2] = w;

ovals[numOvals][3] = h;

++numOvals;

}

}

}

__

import javax.swing.JFrame;

public class Main {

public static void main(String[] args) {

tictactoeboard t=new tictactoeboard();

JFrame frame=new JFrame("OK");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new tictactoeboard());

}

}

here is my code, I don't know what to add Mouse Listener to, to get it to return a point. THanks in advance

[2586 byte] By [PirateXinga] at [2007-10-2 12:04:57]
# 1

I'm not quite sure what you are trying to do, but I *think* you mean you want to call listener.getX() and listener.getY() to return the last clicked position of the mouse?

If so, add a member variable (private LineListener listener) and then in your tictactoeboard constructor initialize that variable. Then whenever you need to access the getX() and getY() methods, just call listener.getX() and listener.getY().

Make since? If that is not what you are trying to do, sorry for completely misunderstanding your problem.

Alex

aRyan316a at 2007-7-13 8:30:08 > top of Java-index,Other Topics,Java Game Development...
# 2

1. Use the "code" button the next time.

2. This piece of code:

addMouseListener(listener);

if(listener.getX()>25&&listener.getX()<=50&&listener.getY()>=25&&listener.getY()<=50){

s.drawLine(25,25,75,75);

s.drawLine(75,25,25,75);

}

}

is bogus. This is not how events work in Java. For a basic understanding of events, you can refer to the Java Tutorial at http://java.sun.com/docs/books/tutorial

DaanSa at 2007-7-13 8:30:08 > top of Java-index,Other Topics,Java Game Development...