Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083680
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:20:37+00:00 2026-05-20T18:20:37+00:00

It works for a few seconds and suddenly will be crashed. import java.util.*; import

  • 0

It works for a few seconds and suddenly will be crashed.

import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CanvasUnit extends Canvas {

    private Timer timer; 
    private TimerTaskClass task;  
    private int Top=13 , Left=48;
    private int width = getWidth();
    private int height = getHeight();
    private static int SWidth = 10;
    private static int SHeight = 10;
    private int LStep = 5;
    private int TStep = 5;

    public CanvasUnit() {
       timer = new Timer();
       task = new TimerTaskClass();
       timer.schedule(task,10); 
    }

    public void paint(Graphics g) {

        g.setColor(0,0,0);
        g.fillRect(0, 0, width, height);
        g.setColor(255,255,255);

        g.drawRect(Left, Top, SWidth, SHeight);
    }
   private class TimerTaskClass extends TimerTask{

    public final void run(){ 

      if (Left > (width - SWidth)) { 
        LStep = LStep * (-1);
      } else if (Left < 0) {
        LStep = Math.abs(LStep); 
      }
      //
      if (Top > (height - SHeight)) {
        TStep = TStep * (-1); 
      } else if (Top < 0) {
        TStep = Math.abs(TStep);  
      }

      Left = Left + LStep;
      Top = Top + TStep;

      repaint();

      //Run the timer agian
      timer = new Timer();
      task = new TimerTaskClass();
      timer.schedule(task,10);
    }

   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:20:38+00:00Added an answer on May 20, 2026 at 6:20 pm

    Sorry my previous comments were not correct because I thought you are using LWUIT, don’t know from where I got this idea. You should use something similar to this

    import javax.microedition.lcdui.*;
    
    public class CanvasUnit extends Canvas {
    
        private int Top = 13, Left = 48;
        private int width = getWidth();
        private int height = getHeight();
        private static int SWidth = 10;
        private static int SHeight = 10;
        private int LStep = 5;
        private int TStep = 5;
    
        public CanvasUnit() {
            Thread t = new Thread(new Runnable() {
    
                public void run() {
                    while (true) {
    
                        if (Left > (width - SWidth)) {
                            LStep = LStep * (-1);
                        } else if (Left < 0) {
                            LStep = Math.abs(LStep);
                        }
                        //
                        if (Top > (height - SHeight)) {
                            TStep = TStep * (-1);
                        } else if (Top < 0) {
                            TStep = Math.abs(TStep);
                        }
    
                        Left = Left + LStep;
                        Top = Top + TStep;
    
                        repaint();
    
                        try {
                            Thread.sleep(10);
    
                        } catch (Exception ex) {
                        }
                    }
                }
            });
            t.start();
        }
    
        public void paint(Graphics g) {
    
            g.setColor(0, 0, 0);
            g.fillRect(0, 0, width, height);
            g.setColor(255, 255, 255);
    
            g.drawRect(Left, Top, SWidth, SHeight);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fadeOut the first div every few seconds, this code works once:
The below code works when there are a few element in the To list
Have an ASP.net web app which works fine for a few days, but then
I have a few servers stateside, Django + MySQL. It all works fine but
I'm using the JayData.js library. It works quite well. However, I have a few
I'm trying to do a basic join which would take a few seconds in
I'm trying to load random items into a div every few seconds, with a
I'm using the http://jplayer.org plugin to play a video as Flash. A few seconds
I want to show a div and remove it after few seconds. It's just
I'm working on a Java Selenium-WebDriver. I added driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); and WebElement textbox =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.