Results 1 to 5 of 5
Thread: mouseClicked and arrays
- 03-08-2013, 04:57 AM #1
Member
- Join Date
- Mar 2013
- Posts
- 3
- Rep Power
- 0
mouseClicked and arrays
Hey guys!
I have coded an applet that will show something like this (mines a different color):

The code below does WORK. The mouseListener part doesn't.
What I want to happen is when I click on one of the squares in the applet, I want to change the color of the array element corresponding to that square to RED.Java Code:public class ColoredSquares extends JApplet implements MouseListener { Random rand = new Random(); private MouseEvent evt = null; private boolean isClicked; // declare instance variables // initialize instance variable with an array of Color objects Color [][] a; Color randC; int x, y; int myClickX; int myClickY; // generates a color public Color Color(Color randC2) { // omitted } public void init () { // sets window size of the applet viewer setSize(800,800); // fill entire applet with background color setBackground(Color.BLACK); // fill array with 8x8 color objects a = new Color[8][8]; for (int x = 0; x < a.length; x++) for (int y = 0; y < a.length; y++) a[x][y] = Color(randC); // implement MouseListener interface this.addMouseListener(this); <--- I think this line is WRONG } // end init method public void paint(Graphics g) { // rectangle size is determined by applet size // leave space at the bottom to draw a string int rowHeight = (getHeight() / 8) - 5; int colWidth = (getWidth() / 8); // loop through each array object and paint a rectangle for (int row = 0; row < a.length; row++) { for (int col = 0; col < a.length; col++) { g.setColor(a[row][col]); g.fillRect(col*colWidth, row*rowHeight, colWidth, rowHeight ); } } // if mouseClicked is true, then change the color of the square that was clicked on if (evt != null && isClicked == true) { // if a square is clicked on // the square turns red g.setColor(Color.red); int col = 0; int row = 0; g.fillRect(col*colWidth, row*rowHeight, colWidth, rowHeight ); } } // end paint method public void mouseClicked(MouseEvent evt) { // save the evt pointer in my global variable this.evt = evt; // stores the current click of the mouse, X and Y position myClickX = evt.getX(); myClickY = evt.getY(); NOT sure what to do HERE V V below V V // if an array element is clicked // return isClicked = true // else // return isClicked = false repaint(); } // end mouseClicked method public void mouseExited(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseReleased(MouseEvent evt) {} public void mousePressed(MouseEvent evt) {} } // end class
I don't believe any other method is to be added-- use the methods already stated.
How do I do that? @_@ I'm really confused with mouse listeners. I know I need to do something like a[row][col].addmouseListener???
Any help would be much appreciated.Last edited by IcedWings; 03-08-2013 at 05:00 AM.
- 03-08-2013, 05:55 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: mouseClicked and arrays
Custom painting should NOT be done on the applet itself.
Create a JPanel and overrid the paintComponent(...) method with your painting logic. Then you add the panel to the applet.
Then you add the MouseListener to the panel.
In your mouseClicked logic you then need to determine which sqare was clicked. Basically the row is given by taking the mouse clicked Y position and dividing this number by the height of each square. Do the same for the column. Then you can update your Color array with the appropriate color and repaint the panel.
Don't do this. Instead you Create a JLabel and add it your your applet, probably at BorderLayout.SOUTH.// leave space at the bottom to draw a string
- 03-08-2013, 06:39 AM #3
Member
- Join Date
- Mar 2013
- Posts
- 3
- Rep Power
- 0
Re: mouseClicked and arrays
- 03-08-2013, 07:46 AM #4
- 03-08-2013, 01:13 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
Re: mouseClicked and arrays
Did you try and put print statements in your mouseClicked routine to see if was picking up events?
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Menu Events actionPerformed vs mouseClicked
By alfredlok in forum New To JavaReplies: 2Last Post: 02-18-2012, 05:46 PM -
Two Different MouseListener Actions on mouseClicked()
By 5myl in forum New To JavaReplies: 4Last Post: 01-01-2012, 09:55 PM -
Java MouseClicked problem.
By Love Like Rockets. in forum AWT / SwingReplies: 12Last Post: 10-30-2011, 01:58 PM -
MouseClicked Stop Animation
By xpngamer in forum New To JavaReplies: 1Last Post: 04-14-2009, 09:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks