Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: run method not working in japplet

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default run method not working in japplet

    i am creating a rect in paint method. and i am trying to move it to its right by 1 px in loop. but the problem is its not going inside the run method. any idea why?




    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
     
    import javax.swing.JApplet;
     
     
    public class main extends JApplet implements Runnable
    {
    	//Double buffering variables
    	Image dbImage; 
    	Graphics dbGraphics;
     
    	//player information variable
    	int x = 10;
    	int y = 50;
    	int width = 30;
    	int height = 30;
    	int dx = 1;
     
    	//###################################################################################################################
    	/*** init method ***/
    	public void init()
    	{
    		setSize(800, 400);
    	}/*** end of init method ***/
     
     
     
    	//#####################################################################################################################
    	/*** stat method ***/
    	public void start()
    	{
     
    	}/*** end of start method ***/
     
     
    	//########################################################################################################################
    	/*** run method ***/
    	public void run()
    	{
     
    		while(true) //main game loop
    		{
    			//move image here
    			x += dx;
     
    			if(x+width >= getWidth())
    			{
    				x -= dx;
    			}
     
     
    			repaint();
    			try
    			{
    				Thread.sleep(17);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
     
    		}//end of while loop
    	}/*** end of run method ***/
     
     
    	//########################################################################################################################
    	/*** update method ***/
    	public void update(Graphics g) 
    	{
    		if(dbImage == null) //if image is empty than create new image
    		{
    			dbImage = createImage(this.getSize().width, this.getSize().height);
    			dbGraphics = dbImage.getGraphics();
    		}
    		dbGraphics.setColor(getBackground());  //set the background color
    		dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
    		dbGraphics.setColor(getForeground());
    		paint(dbGraphics);     //call paint method
    		g.drawImage(dbImage, 0, 0, this);
    	}/*** end of update method ***/
     
     
     
     
    	//########################################################################################################################
    	/*** paint method ***/ 
    	public void paint(Graphics g)
    	{	
    		Graphics2D g2d = (Graphics2D) g; 
     
    		g2d.fillRect(x, y, width, height);   //player	
     
    	}/** end of paint method ***/
     
     
    	//######################################################################################################################
    	/*** stop method ***/
    	public void stop()
    	{
     
    	}/*** end of stop method ***/
     
     
     
    	//#######################################################################################################################
    	/*** destroy method ***/
    	public void destroy()
    	{
     
    	}/*** end of destroy method ***/
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: run method not working in japplet

    I don't see any code that would make the run method, well, run. Where's your Thread object that gets fed the Runnable? Where do you call start() on this Thread?

    Other problems with your code: you should almost never override update(...) in a Swing application, you should not draw directly in a Swing JApplet but rather in the paintComponent(...) method of a JPanel or JComponent that is held by the applet. You shouldn't have a class Graphics or Graphics2D field as that's not how you would use these objects.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    hwoarang69 (November 24th, 2012)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: run method not working in japplet

    i see i fix all the think you told me and now its is almost seem to be working.

    it was redrawing rect over each other so i called super.paint..... to fix that problem.

    but now ithe rect despair for half sec. than moves to right. despair for half sec .. and so on.

    i tried look tutorial on japplet on online but i cant find them. i only saw alot of applet tutorial which is not what i want.

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: run method not working in japplet

    You need to look at the painting with Swing tutorials: Performing Custom Painting and Painting in AWT and Swing

Similar Threads

  1. My while (true) loop method only seems to run once!
    By ajfonty in forum Object Oriented Programming
    Replies: 2
    Last Post: May 18th, 2012, 07:46 PM
  2. Why my void run method cannot be execute?
    By 90th century in forum Threads
    Replies: 2
    Last Post: March 30th, 2012, 12:09 PM
  3. GUI doesn't work when synchronized method is run
    By worwhite in forum AWT / Java Swing
    Replies: 1
    Last Post: August 1st, 2011, 07:59 AM
  4. Re: Can't run the method, Please help!!
    By snowman_heman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 31st, 2011, 08:35 PM
  5. Can't run the method, Please help!!
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2010, 07:13 AM