Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There a number of problems your are facing.</p> <p>Painting in Swing is stateless, that means what ever was previously painting during one cycle, will not appear in the next, unless you physically paint it.</p> <p>Take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/painting/" rel="nofollow">Performing Custom Painting</a> and <a href="http://www.oracle.com/technetwork/java/painting-140037.html" rel="nofollow">Painting in AWT and Swing</a> for more details.</p> <p>Unless you synchronize you threads and paint routines (which is not advisable), you should never change the state of any variable that the paint methods may be reliant on out side of the Event Dispatching Thread.</p> <p>Instead of using a <code>Thread</code> you should be using a <code>javax.swing.Timer</code>.</p> <p>You may find <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow">Concurrency in Swing</a> and <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow">Initial Threads</a> informative</p> <p>You shouldn't be relying on "magic" numbers (such as the character width offset), you should be relying on the underlying system to provide you with useful hints. In this case, you will want to look at <a href="http://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html" rel="nofollow"><code>FontMetrics</code></a></p> <p>You should also avoid using <code>setPreferredSize</code> and override <code>getPreferredSize</code></p> <p>Below is an example of how I might solve the same problem...</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; class TestPaint02 extends JFrame { JPanel panel; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } TestPaint02 obj = new TestPaint02(); obj.makeAnim(); } }); } public void makeAnim() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Animate"); setResizable(false); setSize(400, 400); Animate arr1 = new Animate(); getContentPane().add(arr1); setVisible(true); } public class Animate extends JPanel { int i = 0; int[] a = {1, 2, 3, 5, 6, 7, 2, 1, 10, 99}; public Animate() { Timer timer = new Timer(2000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { i++; repaint(); if (i &gt;= a.length) { ((Timer) e.getSource()).stop(); } } }); timer.setRepeats(true); timer.setInitialDelay(0); timer.start(); } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.setFont(new Font("Courier", Font.PLAIN, 20)); FontMetrics fm = g.getFontMetrics(); int x = 50; int y = 50; for (int loop = 0; loop &lt; i; loop++) { g.drawString(String.valueOf(a[loop]), x, y); x += fm.stringWidth(String.valueOf(a[loop])); } } } } </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.
 

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