Java – how to prevent JComponent cleanup?

I'm making a molecular design application I can draw lines and circles, but it clears the old lines every time I click, so basically, you can only design molecules with 2 atoms

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;
public class MoleculeDesigner extends JComponent implements MouseListener {
    private Point op,cp;
    private boolean first = true;
    public static final Color linecolor = new Color(0,255,0);
    private static final long serialVersionUID = 1L;
    private BufferedImage img = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
    public MoleculeDesigner() {
        JFrame f = new JFrame("Molecule Designer");
        f.setBackground(Color.WHITE);
        f.addMouseListener(this);
        f.add(this);
        f.setSize(100,100);
        f.setDefaultCloSEOperation(3);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new MoleculeDesigner();
    }
    @Override
    protected void paintComponent(Graphics g) {
        if(op != null && cp != null) {
            Graphics2D g2 = img.createGraphics();
            super.paintComponent(g2);
            g2.setColor(linecolor);
            g2.drawLine((int) op.getX(),(int) op.getY(),(int) cp.getX(),(int) cp.getY());
            g2.setColor(Color.BLACK);
            g2.fillOval((int) cp.getX(),(int) cp.getY(),10,10);
            op = (Point) cp.clone();
            g2.dispose();
        }
    }
    @Override
    public Dimension getPreferredSize() {
        return getParent().getMaximumSize();
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        if(!first) {
            cp = e.getPoint();
            cp.setLocation(cp.getX(),cp.getY() - 8);
        }
        else {
            op = e.getPoint();
            first = false;
        }
        repaint();
    }
    @Override public void mousePressed(MouseEvent e) {}
    @Override public void mouseReleased(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
}

All help appreciated!

Solution

Either 1) draw bufferedimage and display it inside the paintcomponent overlay, or 2) put the data into ArrayList or other collections, and then traverse the collections in paintcomponent If I use the data for other purposes, I will do the latter Also, never do this:

public void update(Graphics g) {
    paintComponent(g);
}

This is not how swing graphics should be done, and it is potentially dangerous code Please read:

> Basic Swing Graphics Tutorial > Advanced Swing Graphics Information

Edit more details about option 1:

>Create bufferedimage using one of its constructors. > Draw on an image

>When you need to draw, use getgraphics() or creategraphics() (for graphics2d objects) to get the graphics object from bufferedimage > draw with this graphics object > and then dispose() the graphics object

> then invoke repaint () to redraw components in JVM. > Draw an image in the paintcomponent method by calling g.drawimage (...) and pass in the buffered image

Benefit: usually drawing is faster. I often use it to draw background images Disadvantages: data points are not available, so if you need to manipulate or animate data points, this is not a feasible method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>