S
sso
I'm new to Java so the answer to my question may be quite simple.
Basically my Main class is a JFrame. The JFrame contains an instance
of the "board" class. The board class contains several tiles
(Graphics). I need to be able to detect MouseListeners to the
tiles. I've done things with MouseListeners (adapters etc) with
standard swing components and not had trouble. Perhaps there is
something I don't know or I'm forgetting some of the basics.
Suggestions?
Thanks!
/
*********************************************************************************/
public class Main extends JFrame implements MouseListener{
public Main()
{
Container c = getContentPane();
board brd = new board();
c.add(brd);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
Main app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/
*********************************************************************************/
public class board implements MouseListener
{
// private Point points[] = new Point[500];
private Vector<tile> tiles = new Vector<tile>(500);
// HashMap tiles = new HashMap(500);
public board()
{
int xx = 0;
for(int i = 1; i < 35; i++)
{
for(int j = 0; j < 35; j++)
{
tile newtile = new tile(i * 24, j * 24);
tiles.add(newtile);
}
}
System.out.print(tiles.size());
}
@Override
public void paintComponent(Graphics g)
{
for(tile t : tiles)
{
g.drawRect(t.getX(),t.getY(),20,20);
}
}
}
/
*********************************************************************************/
public class tile {
private int x;
private int y;
public tile (int x, int y) {
this.x = x;
this.y = y;
}
}
Basically my Main class is a JFrame. The JFrame contains an instance
of the "board" class. The board class contains several tiles
(Graphics). I need to be able to detect MouseListeners to the
tiles. I've done things with MouseListeners (adapters etc) with
standard swing components and not had trouble. Perhaps there is
something I don't know or I'm forgetting some of the basics.
Suggestions?
Thanks!
/
*********************************************************************************/
public class Main extends JFrame implements MouseListener{
public Main()
{
Container c = getContentPane();
board brd = new board();
c.add(brd);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
Main app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/
*********************************************************************************/
public class board implements MouseListener
{
// private Point points[] = new Point[500];
private Vector<tile> tiles = new Vector<tile>(500);
// HashMap tiles = new HashMap(500);
public board()
{
int xx = 0;
for(int i = 1; i < 35; i++)
{
for(int j = 0; j < 35; j++)
{
tile newtile = new tile(i * 24, j * 24);
tiles.add(newtile);
}
}
System.out.print(tiles.size());
}
@Override
public void paintComponent(Graphics g)
{
for(tile t : tiles)
{
g.drawRect(t.getX(),t.getY(),20,20);
}
}
}
/
*********************************************************************************/
public class tile {
private int x;
private int y;
public tile (int x, int y) {
this.x = x;
this.y = y;
}
}