JFrame Paint having click-sensitive areas of the screen - java

I was making a program and I wanted to know how to make certain areas of a JFrame activate something when clicked, although without a button, like you clicked in the upper-right quarter of a picture to activate something.

Create an List of Shape objects to represent the areas that you want to click on:
List<Shape> shapes = new ArrayList<Shape>();
Then you can add different shapes to the List:
areas.add( new Rectangle(5, 5, 10, 10) );
Then you add a MouseListener to the frame and in the mousePressed event you would do something like:
for (Shape shape: shapes)
{
if (shape.contains(theMousePointFromTheMouseEvent)
// do something
}

create a JLabel object and set its icon to the image you want to display. Then add a mouse listener to the label object and implement all of its abstract classes especially the mouse clicked method to do what you want to do when clicked. Then when you click your JLabel you will see what you want.
The below code is an example to print "hello" when label is clicked:-
java.awt.event.MouseListener ml = new java.awt.event.MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("hello");
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
};
jLabel1.addMouseListener(ml);

Related

how to impliment mouse listener

Kind of a noob question, but then again, I am a noob. I'm trying to implement a sort of "universal" mouse listener. That is, when I click any of the objects on screen, it runs a specific amount of code. I have the current solution below, but the code I want to run is the same for 10 different objects, so this gets rather tedious.
difference2 = new JLabel(new ImageIcon("transparent.png"));
difference2.setBounds(645,490,10,10); //left, top, width, height
contentPane.add(difference2);
difference2.setVisible(true);
difference2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
//code
}
});
I am aware I can create a separate method such as the following
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"this would be nice");
}
But I can't figure out how to set up a mouse listener on every object for it. The JOptionPane currently does nothing.
I might have misread your question, but if you want the same mouselistener on various objects,you could store the instance of your listener in a variable once and then add it to whatever gui object you want it added to.
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {//code}
#Override
public void mousePressed(MouseEvent e) {//code}
#Override
public void mouseExited(MouseEvent e) {//code}
#Override
public void mouseEntered(MouseEvent e) {//code}
#Override
public void mouseClicked(MouseEvent e) {//code}
};
JLabel j1 = new JLabel("Label1");
j1.addMouseListener(ml);
JLabel j2 = new JLabel("Label2");
j2.addMouseListener(ml);
You can create an instance of an anonymous class that extends MouseAdapter and assign it to a variable that you can reuse (myMouseListener in this case):
MouseListener myMouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"this would be nice");
}
};
difference2.addMouseListener(myMouseListener);
aSecondObject.addMouseListener(myMouseListener);
aThirdObject.addMouseListener(myMouseListener);
...

Change JButton's click color?

I've created some swing applications involving JButtons, and noticed whenever one is clicked, it turns white. Example here.
How would I change it so when, and only when, the button is clicked, it turns RED instead of the usual white, and when it is released, it goes back to its normal look? Is there a method for this?
Example code:
JButton b = new JButton("foo");
b.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
//turn red
}
#Override
public void mouseReleased(MouseEvent e) {
//go back to original state
}
});
change color of button text using setForeground method
like this
#Override
public void mousePressed(MouseEvent e) {
b.setForeground(Color.red); // button text color
// b.setBackground(Color.red); // button background color
}
#Override
public void mouseReleased(MouseEvent e) {
b.setForeground(Color.black); // button text color
}
JButton b = new JButton("foo");
b.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
b.setBackground(Color.red);
}
#Override
public void mouseReleased(MouseEvent e) {
//go back to original state
}
});
For more details look at this example

OpenImaj - Using a MouseListener with JFrame

I'm attempting to add mouse listening capabilities to a JFrame that displays an MBFImage and the mouse events do absolutely nothing. I'm not sure if the events are not firing or if they are and not being caught because I am doing something wrong...
The image shows up in the JFrame just fine, however moving the mouse around over the image, clicking, moving, dragging, etc. does not result in any activity.
NOTE 1 I've discovered that if I add the mouselistener to a JPanel and then (in this specific order) draw the image and then add the the JPanel to the JFrame, that the mouse listener catches the events, but ONLY listens outside of the image. It draws a minimum size window which I need to resize. Any mouse movement over the image does not seem to fire / catch any events.
NOTE 2 If I add the panel to the JFrame and then draw the image, the window size is just fine, however the mouse listener no longer work.
Can anyone shed any light?
Here is the relevant portion of my code:
private JFrame displayImage(final MyAppImage image, final MyAppImage.DetectLevel level, String title) {
MBFImage mbfImg = image.drawDetections(level); //draws face detection boxes
JFrame imgFrame = new JFrame(title);
DisplayUtilities.display(mbfImg, imgFrame);
imgFrame.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println("here");
if (level == MyAppImage.DetectLevel.filtered) {
if (image.checkPoints(e.getX(), e.getY(), level) != null) {
System.out.println("YES");
}
else {
System.out.println("NO!");
}
}
else {
System.out.println("Huh?");
}
}
#Override
public void mouseExited(MouseEvent e) {
}
});
imgFrame.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
System.out.println("hello");
}
#Override
public void mouseMoved(MouseEvent e) {
System.out.println("here");
if (level == MyAppImage.DetectLevel.filtered) {
if (image.checkPoints(e.getX(), e.getY(), level) != null) {
System.out.println("YES");
}
else {
System.out.println("NO!");
}
}
else {
System.out.println("Huh?");
}
}
});
return imgFrame;
}
Exactly what #MadProgrammer commented - when you call DisplayUtilities.display(mbfImg, imgFrame); it creates an ImageComponent inside your JFrame that is itself a MouseListener.
You should be able to add a MouseListener to the ImageComponent directly however.

How to use JPanel mouse listener

I have a JPanel that is painted as a rectangle with a specific color. In my JPanel class in the constructor I accept a color and text. The text is the name of the color.
I am trying to make a mouse listener that will get the color of the jpanel after a person clicks on the jpanel. Any suggestions?
I did store the color in a variable but I have multiple color panels so when I click on one panel for example a yellow one I want to make a check to see if the panel clicked is a certain color and if so then something will happen but I'm having trouble figuring out how to get the JPanel source from the mouse listener.
This is how to get the background Color of the JPanel that was clicked on via a mouse handler (assuming the mouse event handler is attached to the JPanel you want to get the Color of):
private void mouseClicked(java.awt.event.MouseEvent evt) {
JPanel panel = (JPanel)evt.getSource();
Color c = panel.getBackground();
System.out.println("color: " + c.toString());
}
Explanation:
In the mouseClicked method the MouseEvent argument evt is an Object which contains a reference to the "source" of the mouse event (ie. the Object that had the click event handler attached to it). If you know you've only added the event handler to JPanel objects, then you can safely cast the result of getSource() to a JPanel instance as is done in the example code.
Then you can perform operations on the JPanel source of the click event such as getBackground().
here is a complete class showing how to print color name the JPanel is clicked is tested the code
class RectanglePanel extends JPanel implements MouseListener {
String colorName;
Color color;
public RectanglePanel(String text, Color c) {
this.colorName = text;
this.color = c;
super.addMouseListener(this);
}
#Override
public void paint(Graphics g) {
super.paint(g); //To change body of generated methods, choose Tools | Templates.
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
g2.fillRect(50, 50, 100, 100);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(colorName);
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}

Changing the Background of a button after it is clicked (i.e. after Action performed)

I am trying to change the background of a JButton after it is clicked. Currently my buttons are located in a GridLayout (3x3) and look like this:
tiles.add(new JButton(new AbstractAction("") {
#Override
public void actionPerformed(ActionEvent e) {
this.setIcon("foo.png");
}
}));
This does not work. How do I manipulate the background of the image from within the actionperformed?
A JToggleButton might be ideal for this, as shown on Swing JToolbarButton pressing
Note that you would need to add some code to ensure a button was only clickable once.
Alternately you might use a standard JButton and call AbstractButton.setDisabledIcon(Icon). Disable the button when clicked, and it will flip to the alternate icon.
You can create your own listener that implements the MouseListener. This way, you can control when the background of the button changes (when the mouse is released, pressed, etc). Here is an example
//Add the listener to the button
myButton.addMouseListener(new customActionListener());
//Create the listener
class customActionListener implements MouseListener {
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Icon icon = new ImageIcon("icon.gif");
myButton.setIcon(icon);
}
public void mouseClicked(MouseEvent e) {
}
}
At whatever point you want to set the background back to its default, use:
myButton.setIcon(new ImageIcon());

Categories

Resources