Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><strong>Never</strong> call Thread.sleep from within a paint type of method. Doing this will make your GUI completely unresponsive.</li> <li>Yes, do call the super painting method from within your painting method (as per muhammad's answer).</li> <li>You should not call <code>Thread.sleep(...)</code> from the event thread either as this too will freeze your application.</li> <li>You should skip doing AWT and move to Swing. </li> <li>When you do so, do your drawing in the <code>paintComponent(Graphics g)</code> method of a JComponent or JPanel object.</li> <li>Call the super's <code>paintComponent(g)</code> within your <code>paintComponent</code> method override.</li> <li>Use a Swing Timer to do any delay or animation.</li> </ol> <hr> <p>e.g.,</p> <pre><code>import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Stroke; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class DrawFoo extends JPanel { private static final int PREF_W = 400; private static final int PREF_H = PREF_W; private static final Stroke BASIC_STROKE = new BasicStroke(3f); private static final Color RECT_COLOR = Color.blue; private static final Color OVAL_COLOR = Color.red; private boolean drawCircle = false; private int rectX = 20; private int rectY = 150; private int rectWidth = 100; public DrawFoo() { int delay = 1000; Timer timer = new Timer(delay, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { drawCircle = true; repaint(); } }); timer.setRepeats(false); timer.start(); } @Override public Dimension getPreferredSize() { return new Dimension(PREF_W, PREF_H); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(BASIC_STROKE); g2.setColor(RECT_COLOR); g.fillRect(rectX, rectY, rectWidth, rectWidth); if (drawCircle) { g2.setColor(OVAL_COLOR); g.fillOval(rectY, rectX, rectWidth, rectWidth); } } private static void createAndShowGui() { JFrame frame = new JFrame("DrawFoo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawFoo()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } </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.
    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