java - How do I delay the program within a for loop without execptions? -
I have to wait at the end of my loop before starting again, but I have searched and any other methods The program does not work for my program, moving the image from one side to the other loop looks like this:
int xSize = ((int) tk.getScreenSize (). GetWidth ()); Int ySize = ((int) tk.getScreenSize (). GetHeight ()); (Int final = x = xSize - 128; lastx == 0; last x- = 50) {for (int i = 0; i and lt; 11; i + = 1) {int min = 64; Int max = ySize - 200; Int finalY = min + (int) (math.rendum) * ((max - min) + 1)); G.drawImage (Image3, Final X, Final, Image, Image, Image, This); & Lt; B & gt; // Wait here & lt; / B & gt; }}
Yes, I've imported everything I tried try / hold
, exceptions, and more but none of them works. How can I do so that my program will still work and will wait?
Due to this line:
g.drawImage (image3, Final, final, image, image, image, this);
This is a swing or AWT GUI because you are portraying a graphics object (an important statement about your question that you should provide for us), and if so , Then it is well discussed here and here:
- Do not use the loop.
- Do not use
thread. Sleep (...)
- Instead of using a swing timer to delay, and in the ActionListener of Timer, Set a class field that will be used to enlarge the drawing.
- and
ColorCourse (Graphics G)
Overrides of a JPNL or other class that extends from JComponent. Remember to call- and
super.paintComponent (Graphics G)
Override above
The details of your implementation will depend on the details of your problem, and if you want to give us something else, please see the general review of using the swing timer.
Edit
For example ...
Import java.awt.Dimension; Import java.awt.Graphics; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.awt.image.BufferedImage; Import java.io.IOException; Import java.net.URL; Import java.util.Random; Import javax.imageio.ImageIO; Import javax.swing *; @SuppressWarnings ("serial") public-class timerapp extends to JPNL {Personal Static Final End PREF_W = 600; Private Stable Final End PREF_H = PREF_W; Private static final string ICON_PATH = "https://duke.kenai.com/iconsized/duke4.gif"; Personal fixed final difference TIMER_DELAY = 400; Private Buffett Image Choice = Faucet; Private random random = new random (); Private Int Final X; Private int final; Public timerEg () throws IOException {URL imgUrl = new URL (ICONPAT); ImageSprite = ImageIO.read (imgUrl); Int maxX = PREF_W - imageSprite.getWidth (); Integer = PREF_H - imageSprite.getHeight (); New timer (TIMER_DELAY, new timer listener (this, maxx, max)). Start (); } Public Zero Sets per location (int x, int y) {last x = x; Last Y = y; Repaint (); } @ Override Protected Zero Paint Comonant (Graphics G) {SuperPaintConant (G); If (imageSprite! = Null) {g.drawImage (imageSprite, finalX, last, this); }} @ Override Public Dimensions getPreferredSize () {New Dimension Back (PREF_W, PREF_H); } Private Class TimerListener applies ActionListener {Private TimerEg timerEg; Private int xxx; Private integer maximum; Private Timer Listener (Timer EG Timer Egg, Intex Xx, NXxXx) {this.timerEg = Timer Egg; This.maxx = maxX; This.maxY = maxY; } @ Override Public Zero Action Perffered (ActionEvents E) {int presentx = random.nxit (max); Int currentY = random.nextInt (maxY); Timer Egg .Setlight location (current X, current Y); }} Private Static Wide CreateAndShowGui () {TimerEg mainPanel = null; {MainPanel = New TimerEg (try); } Hold (IOException e) {e.printStackTrace (); System.exit (-1); } Jeffre Frame = New Gefram ("Timer Egg"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Add frame.getContentPane () (mainPanel) .; Frame.pack (); Frame.setLocationByPlatform (true); Frame.setVisible (true); } Public static zero main (string [] Args) {SwingUtilities.invokeLater (new runnab) (Public Zero Run) {createAndShowGui ();}}); }}
which rushes randomly on a phantom GUI and looks like this:
Comments
Post a Comment