Java2D and mouse events
Hi everybody, i'm having a serious problem adding mouse events to 2D shapes. I have the following:
-- --
/ / /
/ / /
/ / /
-
i.e. I have 2 rectangles of different color (like a tile) and i need to add different events to each of them. Namely, if I am on the first I need to alpha blending the second, and viceversa. I can draw rectangles, it's really simple, but i dont know hot to add mouse events. Can anyone help me please?
Thanks everybody.
Mauro.
P.S.
The image is really confusing. The situation is: i have different rectangles like a puzzle, each colored differently.
Message was edited by:
flx81
[687 byte] By [
flx81a] at [2007-11-14]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseAware extends JPanel {
Rectangle[] rects;
Color[] colors = {
Color.red, Color.green.darker(), Color.blue, Color.orange
};
float alpha = 0.6f;
int rule = AlphaComposite.SRC_OVER;
AlphaComposite ac = AlphaComposite.getInstance(rule, alpha);
int selectedIndex = -1;
final int PAD = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(rects == null) initRects();
Composite orig = g2.getComposite();
for(int j = 0; j < rects.length; j++) {
g2.setPaint(colors[j]);
if(j == selectedIndex)
g2.setComposite(ac);
g2.fill(rects[j]);
if(j == selectedIndex)
g2.setComposite(orig);
}
}
private void initRects() {
int w = getWidth();
int h = getHeight();
int width = (w - 3*PAD)/2;
int height = (h - 3*PAD)/2;
rects = new Rectangle[4];
for(int j = 0; j < rects.length; j++) {
int x = PAD + (j%2)*(width + PAD);
int y = PAD + (j/2)*(height + PAD);
rects[j] = new Rectangle(x, y, width, height);
}
}
public static void main(String[] args) {
MouseAware mouseAware = new MouseAware();
mouseAware.addMouseMotionListener(mouseAware.mouser);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(mouseAware);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
private MouseMotionListener mouser = new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
if(rects == null) return;
Point p = e.getPoint();
boolean hovering = false;
for(int j = 0; j < rects.length; j++) {
if(rects[j].contains(p)) {
hovering = true;
if(selectedIndex == -1 || selectedIndex != j) {
selectedIndex = j;
repaint();
}
break;
}
}
if(!hovering && selectedIndex != -1) {
selectedIndex = -1;
repaint();
}
}
};
}
Hi guys, i really worship you, since I've got my goal. But now i have a really big problem: performance is really low. Namely: when i move my mouse over the rects, i do see repaint operation, which makes my interface really unusable. Have any idea to improve performance? I've yet set RenderingHints to the lowest values (no antialiasing, rendering_speed, no dithering) and i've also tried with the highest ones, but no way out of my problem. I really appreciate your suggestions ;)
Hi and thanks in advance.
ok thank you, i will search for backbuffer, even if i've yet tried VolatileImage but with no results. I don't know if I write something wrong, but it seems hardware acceleration is not available for alpha blending. However, I've minimized the problem using a new variable, lastSelected, which is used to check if i am moving over the same rectangle: in this case i do not repaint(). Now the problem still remains only if i change rectangle, when i do see the repaint() operation. Howevet, i will google backbuffer and let you know. thanks guys.
oh yeah, finally i've solved refresh problems. After looking at lots of guides (not very clear, i have to say), I succeded in using VolatileImage, with performance boost :). You (a possible future newby reader, like me) must follow 4 simple steps:
1. create a VolatileImage vi;
2. create a Graphics2D from that vi (vi.createGraphics()), say vi_graph;
3. draw everything on vi_graph;
4. draw the image on the original Graphics, namely the parameter of paint(Graphics g): g.drawImage().
That's quite all, of course you need some more codes to check vi consistency (code available online).
Follow the same steps if you wanna use a BufferedImage.
Bye and many thanks to everybody.