for loop - Doing an animation in Java -


I'm assigning a Java for the next Saturday.

It's actually going well, though I'm struggling with a block.

Here I want to reveal a number of numbers in a string, one at a time. I 'Thread sleep (1000);'

Trying to slow down the loop, however nothing is being displayed until the thread is finished

The following is a part of the graphics class where the problem is coming I miss

  Public Zero Color (Graphics G) {setSize (550, 300); // It pulls all random numbers, if the user is disclosed to ans (if appears == 0) {g.setColor (Color.BLUE); G.drawString (Random Number, 20, 20); // Draw String ("string", x, y)} // This shows the user from 1 to 1 in the beginning of the game if (not manifest == 1) {for (int x = 0; x & lt; borders ; X ++) {g.setColor (Color.BLUE); G.drawString ( "+ +, 20, 20); // draw to string (" string ", x, y) {Thread. Sleep (1000);} catch (shown predefined) {Systemkoutkprint (" Error ");} Color ();} // Slow the loop to show the user}  

Since your GUI is called thread soth, the whole app will be kept to sleep. Instead, use a swing timer inside the Timer's ActionListener, add another letter to the displayed string, For example,

import java.awt, for example,

.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing. *; public class Srlanukrmnn JPanel {public static final end TIMER_DELAY = 1000; private JTextField textField = new JTextField (10); private Jeelabl Displelebl = new Jeelabel ( "", swing Constnstentsksetr); public Srlanimeshn () {a Rrwai Bitianakshn = new Doaitibitianakshn ( "Do it!", Keavent. VKD); JPNlp TopPenel = new zpn (); TopPanel.add (field); TopPanel.add (new pocket (BTN)); TextField.addActionListener (btnAction); SetLayout (new grid layout (2, 1)); Add (topPanel); Add (displayLabel); } Private class DoItBtnAction AbstractAction {Private String textFieldText = ""; Public DoItBtnAction (name of string, reward memnick) {super (name); PutValue (MNEMONIC_KEY, MONEY); } @ Override Public Zero Action Perfid (Action Event e) {displayLabel.setText (""); SetEnabled (false); TextFieldText = textField.getText (); New Timer (TIMER_DELAY, New Akshnlistnr () {private Int I = 0; @ Override public void Akshnprrfed (Action Event e) {if (i & gt; = textFieldText.length ()) {((timer) EkgetSource () ). stop (); Doitbtnkshnkthisksetenbled (true);} else {Displelbelksettest (Displelbelkgettest () + Tekstfieldtestkcrt (e)); i ++;}}}). Start (); }} Make Private Static Wounds Andshoes () {Jeffrey Frame = New Geoffram ("Simple Animation"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.getContentPane () Add (New SimpleAnimation ()); Frame.pack (); Frame.setLocationRelativeTo (zero); Frame.setVisible (true); } Public static zero main (string [] Args) {SwingUtilities.invokeLater (new runnab) (Public Zero Run) {createAndShowGui ();}}); In addition,

  • If your swing is GUI, then instead of displaying your text in JLabel or JTextField, it's easy to paint it on the GUI.
  • If this swing, then paint (Graphics g) override but paintComponent (Graphics g) method Jepianel or Jekanbonent Of

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -