Java Help MouseListener

I am trying to write a program in Java Swing that draws a grid of 10 x 10 geometric rectangles filled with randoms colors. However, when the user clicks on one of the rectangles in the display window, the rectangle should repaint () and change to a different color.

So far, I have run a rudimentary program, but I can’t understand how to implement a mouseListener for it to change the color of the rectangles when the user clicks inside. At this point, the rectangles only redraw when the display window expands and is minimized. Any advice / help would be greatly appreciated! Thanks!

Here is what I still have ...

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; public class ColorGrid extends JPanel { int w, x, y, z; Color c = new Color((int)(Math.random() * 0xFFFFFF)); public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; setLayout(new GridLayout(10,10)); int w = x = y = z = 0; for(int i=0;i<100;i++){ Color c = new Color((int)(Math.random() * 0xFFFFFF)); w+=10; x+=10; y+=50; z+=15; g2.drawRect(w+10,x+30,y,z); g2.drawRect(w+10,x+30,y,z); g2.fillRect(w+10,x+30,y,z); g2.setPaint(c); } } public static void main(String[] args) { JFrame f= new JFrame(); f.setTitle("ColorGrid Display Window"); f.setSize(200,200); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = f.getContentPane(); contentPane.add(new ColorGrid()); f.show(); } } 
+2
source share
4 answers

Any Component can have a MouseListener . JLabel is good for a color rectangle if you make it opaque.

Addendum: MouseAdapter recommending the MouseAdapter elsewhere, I should mention that one instance is enough.

Addendum: This update adds a mouse listener to the ColorLabel constructor.

Colorlabel picture

 import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.JFrame; import javax.swing.JLabel; /** @see http://stackoverflow.com/questions/5136859 */ public class ColorLabel extends JLabel { private static final int N = 10; private static final Random random = new Random(); private static final MouseAdapter listener = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { ColorLabel label = (ColorLabel) e.getSource(); label.setBackground(new Color(random.nextInt())); } }; public ColorLabel() { this.setOpaque(true); this.setBackground(new Color(random.nextInt())); this.setPreferredSize(new Dimension(32, 32)); this.addMouseListener(listener); } private void displayGrid() { JFrame f = new JFrame("ColorGrid"); f.setLayout(new GridLayout(N, N)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (int i = 0; i < N * N; i++) { final ColorLabel label = new ColorLabel(); f.add(label); } f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ColorLabel().displayGrid(); } }); } } 
+4
source

Instead of having a JPanel on which you draw your color grid, how about a button grid. You redefine the button's drawing mechanism so that it simply displays the current color. Then you have a built-in function to listen for clicks in a specific section of your grid.

+3
source

Here is what I came up with. Note. I am still studying Java at the university, so this may not be the most accurate way to do this, but it worked when I did it.

 public class ColorGrid extends JPanel implements MouseListener { this.addMouseListener(this); addMouseListener(this); 

For the first part, the second part must have these methods in your code.

 public void mouseClicked(MouseEvent arg0) { } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } 

Then, depending on what you want (i.e. mouse clicked or clicked), simply type:

 repaint(); 

Hope this helps.

+1
source

Assuming you have a 2d array of colors, you can simply use the x and y that the mouselistener gives you when you click to calculate the indices of this rectangle. Just divide x and y by the size of the rectangle using integer division. After changing the color, use repaint () to show it.

0
source

Source: https://habr.com/ru/post/893548/


All Articles