Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When doing anything based on a timer in Swing, the <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html" rel="nofollow"><code>javax.swing.Timer</code></a> class is your best friend.</p> <p>It's JavaDocs explains its usage pretty well, so here's an example.</p> <pre><code>public class GTest extends JFrame implements ActionListener { private Color textColor = Color.BLACK; private Random random = new Random(); @Override public void paint(Graphics g) { super.paint(g); g.setColor(textColor); g.drawString("[Game goes here]", 100, 150); } @Override public void actionPerformed(ActionEvent e) { textColor = new Color(random.nextInt(0x00ffffff)); repaint(); } public GTest() { setSize(Toolkit.getDefaultToolkit().getScreenSize().width / 3, Toolkit.getDefaultToolkit().getScreenSize().width / 3 + 50); setDefaultCloseOperation(EXIT_ON_CLOSE); Timer timer = new Timer(500, this); timer.setInitialDelay(0); timer.start(); setVisible(true); } public static void main(String[] args) { GTest myWindow = new GTest(); } } </code></pre> <p>It's your <code>GTest</code> class once again, but this time the text color changes automatically every 0.5 seconds.</p> <p>Notice two main changes:</p> <ol> <li>The constructor now sets up a new instance of <code>Timer</code> with no initial delay, period of 500 ms and its listener set up to <code>this</code>. That means that this constructed instance will listen to that timer's ticks.</li> <li>To do that, we needed to implement the <code>ActionListener</code> interface. This forces us to write the <code>actionPerformed()</code> method which gets called every time the Timer ticks. In this method, we change the textcolor to a random one and call <code>repaint()</code> which subsequently calls our <code>paint()</code> method and gets the text written.</li> </ol> <hr> <p>P.S. You don't have to <code>dispose()</code> your <code>Graphics</code> object every time. It's actually said in its JavaDoc, too (emphasize mine):</p> <blockquote> <p>Graphics objects which are provided as arguments to the <code>paint()</code> and <code>update()</code> methods of components are automatically released by the system when those methods return. For efficiency, programmers should call <code>dispose()</code> when finished using a <code>Graphics</code> object <em>only if it was created directly from a component or another <code>Graphics</code> object</em>.</p> </blockquote>
    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. 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.
 

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