Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could simply draw to a buffered image and display that image.</p> <p>Demo code:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; class TestPaint { private BufferedImage image; private JLabel drawing; private int x = 0; private int y = 0; protected void initUI() { JFrame jFrame = new JFrame(); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); jButton1.setText("Button1"); jButton1.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Button2"); jButton2.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB); image.getGraphics().setColor(Color.WHITE); image.getGraphics().fillRect(0, 0, image.getWidth(), image.getHeight()); drawing = new JLabel(new ImageIcon(image)); JPanel bottomPanel = new JPanel(new FlowLayout()); JPanel buttonPanel = new JPanel(new GridLayout(1, 0)); buttonPanel.add(jButton1); buttonPanel.add(jButton2); bottomPanel.add(buttonPanel); jFrame.add(drawing); jFrame.add(bottomPanel, BorderLayout.SOUTH); jFrame.pack(); jFrame.setVisible(true); } private Graphics getImageGraphics() { return image.getGraphics(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Graphics g = getImageGraphics(); g.setColor(Color.GREEN); g.drawRoundRect(x, y, 40, 40, 100, 200); drawing.repaint(); x += 5; y += 5; } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { Graphics g = getImageGraphics(); g.setColor(Color.BLUE); g.drawRect(x, y, 200, 200); drawing.repaint(); x += 5; y += 5; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestPaint().initUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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