Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating glasspane
    primarykey
    data
    text
    <p>I have a question. Can I create glasspane in body MousePressed ? If yes anyone can write me how? I mean that I press mouse button and glass pane is visible and I can painting on him.</p> <p>EDIT</p> <p>Ok I have now what I want. My glass pane is creating when I click mouse button and disapear when I release this button. Now I have another question. Where I should create my painting method. I want draw rectangle on this glasss pane using mouse dragged. Where I must implement paint method? In other class or in this events? I implement one my try paint function but I don't know if this is good way. This is my code:</p> <pre><code>public class Selection extends JPanel { static Point startPoint; public static void GUI() { final JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Select"); final JPanel glassPane = new JPanel(); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.add(button); glassPane.setOpaque(false); frame.add(panel); frame.setGlassPane(glassPane); glassPane.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); System.out.println("f.getGlassPane() mousePressed"); if(e.getButton() == MouseEvent.BUTTON1) frame.getGlassPane().setVisible(true); startPoint=e.getPoint(); Graphics2D g = null; Graphics2D g2 = (Graphics2D) g; Rectangle2D rect = new Rectangle2D.Double(); rect.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F)); g2.setColor(Color.BLUE); g2.fill(rect); g2.draw(rect); } }); glassPane.addMouseMotionListener(new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } }); frame.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); if(e.getButton() == MouseEvent.BUTTON1) frame.getGlassPane().setVisible(true); } public void mouseReleased(MouseEvent e) { frame.getGlassPane().setVisible(false); } }); frame.setVisible(true); } int x1, x2, y1,y2; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.drawRect(x1,y1, x2, y2); } public static void main(String[] args) { GUI(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload