I'm trying to make a screenshot tool that shows screenshot picture to user in fullscreen. Then user will edit it with a few tools. While creating it, i faced a problem with visualization of some effects. I want to create visual representation of selected area using selection rectangle(don't know the exact term name) that you use to select multiple files on desktop or file explorer. example image link
I tried to do it with drawing rectangle on background picture, but when i tried to move a cursor, image was refreshing ugly. Then I tried to do transparent panel with both setOpaque(false); and setBackground(new Color(0,0,0,0)); and than pin mouse listeners on this panel. It draws rectangle successfully, but background of panel instantly fills with default grey color. How can i make foreground panel transparent and draw something on it, without repainting background image? Here is my Glass class:
private static final int WIDE = 1920;
private static final int HIGH = 1080;
private final Color clean = new Color(0,0,0,0);
private Point mousePt = new Point(WIDE / 2, HIGH / 2);
private Rectangle mouseRect = new Rectangle();
Glass(){
this.setOpaque(false);
this.setBackground(clean);
this.addMouseListener(new MouseHandler());
this.addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g) {
g.setColor(clean);
g.fillRect(0, 0, 1920, 1080 );
g.setColor(Color.darkGray);
g.drawRect(mouseRect.x, mouseRect.y, mouseRect.width, mouseRect.height);
}
private class MouseHandler extends MouseAdapter {
public void mouseReleased(MouseEvent e) {
System.out.println("released");
mouseRect.setBounds(0, 0, 0, 0);
e.getComponent().repaint();
}
public void mousePressed(MouseEvent e) {
mousePt = e.getPoint();
System.out.println("pressed");
e.getComponent().repaint();
}
}
private class MouseMotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e){
mouseRect.setBounds(
Math.min(mousePt.x, e.getX()),
Math.min(mousePt.y, e.getY()),
Math.abs(mousePt.x - e.getX()),
Math.abs(mousePt.y - e.getY()));
e.getComponent().repaint();
}
}
Aucun commentaire:
Enregistrer un commentaire