I was playing around with transparancy in Java and wanted to create a selection (rubber band) box. When I click and drag the initial band works fine... however, the transparent fill in of the box flashes (espessially when the box is big). Does anyone know how to get around this? The flashing is real annoying. (Sorry about all the commented out crap... I was playing around).
Here is the code for the JPanel that I put into a Frame:
package rubberbandtest;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.Rectangle;
public class MyPanel extends javax.swing.JPanel {
/** Creates new form MyPanel */
public MyPanel() {
initComponents();
//System.setProperty("sun.awt.noerasebackground", "true");
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setLayout(null);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
formMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
formMouseReleased(evt);
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Some value");
jLabel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 0, 102)));
jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
add(jLabel1);
jLabel1.setBounds(90, 90, 70, 10);
}
private void formMouseDragged(java.awt.event.MouseEvent evt) {
int X = evt.getX();
int Y = evt.getY();
System.out.println("X: " + X + " Y: " + Y);
c_x2= X;
c_y2= Y;
if (X < 0) {X = 0;};
if (Y < 0) {Y = 0;};
doBox();
return;
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
repaint();
}
private void formMousePressed(java.awt.event.MouseEvent evt) {
int X = evt.getX();
int Y = evt.getY();
System.out.println("X: " + X + " Y: " + Y);
c_x1= X;
c_y1= Y;
if (X < 0) {X = 0;};
if (Y < 0) {Y = 0;};
return;
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
/*
***********************
* doBox
*
* Purpose: Draws rubber band box with Java.
*/
private final void doBox(){
Graphics g = this.getGraphics();
Graphics2D g2d = (Graphics2D)this.getGraphics();
//this.update(g);
super.paint(g); //eliminates flashing a bit better than this.update(g) does.
//this.update(g2d);
//super.paint(g2d);
g.setColor(Color.blue);
g.drawLine(c_x1,c_y1,c_x1,c_y2);
g.drawLine(c_x1,c_y2,c_x2,c_y2);
g.drawLine(c_x2,c_y2,c_x2,c_y1);
g.drawLine(c_x2,c_y1,c_x1,c_y1);
//Color tempColor = g2d.getColor();
g2d.setColor(Color.blue);
float alpha = .2f;
Composite composite = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
//Rectangle rect = getRectangle();
//g2d.fill(rect);
if(c_x1 < c_x2 && c_y1 < c_y2)
g2d.fillRect(c_x1, c_y1, c_x2-c_x1, c_y2-c_y1);
if(c_x1 > c_x2 && c_y1 > c_y2)
g2d.fillRect(c_x2, c_y2, c_x1-c_x2, c_y1-c_y2);
if(c_x1 < c_x2 && c_y1 > c_y2)
g2d.fillRect(c_x1, c_y2, c_x2-c_x1, c_y1-c_y2);
if(c_x1 > c_x2 && c_y1 < c_y2)
g2d.fillRect(c_x2, c_y1, c_x1-c_x2, c_y2-c_y1);
g2d.setComposite(composite);
//g2d.draw(rect);
//g2d.setColor(tempColor);
//this.update(g);
}
int c_xval1=0; // initial x for rubber band box
int c_yval1=0; // initial y for rubber band box
int c_xval2=0; // final x for rubber band box
int c_yval2=0; // final y for rubber band box
int c_x1=0; // initial x java
int c_y1=0; // initial y java
int c_x2=0; // final x java
int c_y2=0; // final y java
}