0

I'm trying to make a Paint program using java , I have three events in the jPanel to draw my line. my problem is that when I am drawing the new line , the first one removed (I think the problem in the dragged event!) .. and so on. Note that while the mouse is dragged the line will be stucked to the mouse here is my events code:

  private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {                                     
    g1=(Graphics2D) jPanel1.getGraphics(); 
    p1=jPanel1.getMousePosition();
}                                    
    JLayer lpane;

    private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {                                     
     if(p1!=null){ 
         lpane = new JLayer();
         jPanel1.add(lpane, BorderLayout.CENTER);
         lpane.setBounds(0, 0, 328, 257);

         g2=(Graphics2D) lpane.getGraphics();
    l=new Line(p1.x,p1.y,jPanel1.getMousePosition().x,jPanel1.getMousePosition().y);
    l.draw(g2); 
    //lpane.repaint();
    lpane.setVisible(false);
    lpane.removeAll();
    lpane.disable(); jPanel1.remove(lpane);
    }
}                                    

private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {                                      
    if(p1!=null)
   {  
       g1=(Graphics2D) jPanel1.getGraphics();
        p2=jPanel1.getMousePosition();
        l=new Line(p1.x,p1.y,p2.x,p2.y);
        g1.setColor(Color.red);
      l.draw(g1);
     p1=null;
   }
}                                     
Graphics2D g1,g2; Point p1=null,p2=null; Line l;
EsraaEid
  • 25
  • 1
  • 6

2 Answers2

3

getGraphics is not how painting should be done in Swing, instead override the panels paintComponent and paint your components state there.

The paintComponent method needs to know what to paint whenever it is called, as it may be called any number of times, many times without your interaction or knowledge.

One approach is to build a List of shapes or Points, which can then be looped through and painted each time paintComponent is called. The benefit of this is you can remove these shapes/points should you wish.

See Pinting in AWT and Swing and Performing Custom Painting for more detals

Also take a look at this example for an idea

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

The usual way of doing this is to create a (Buffered)Image the size of your Component, fill the background color, and then draw each new line on the Image as well. In your paintComponent method, all you call is g.drawImage(...);

In your panel:

public void paintComponent(Graphics g) {
    if (mSizeChanged) {
        handleResize();
    }
    g.drawImage(mImg, 0, 0, null);
}

In your MouseMotionListener:

    public void mouseDragged(MouseEvent me) {
        Graphics g = mImg.getGraphics();
        Point p = me.getPoint();
        g.drawLine(mLastPoint.x, mLastPoint.y, p.x, p.y); }
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • 2
    The first line of a paintComponent method should *always* be `super.paintComponent(g);`. – VGR Dec 04 '14 at 20:33
  • If using a `BufferedImage` we can display it in a `JLabel` and simply call `repaint()` when the image is updated. No need to extend `JPanel`. If **using** `paintComponent(Graphics)` then `g.drawImage(mImg, 0, 0, null);` would better be `g.drawImage(mImg, 0, 0, this);` – Andrew Thompson Dec 05 '14 at 00:44