9 Replies - 475 Views - Last Post: 25 November 2012 - 12:16 PM Rate Topic: -----

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

run method not working

Posted 24 November 2012 - 07:17 PM

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 ***/
}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 ***/
}


Is This A Good Question/Topic? 0
  • +

Replies To: run method not working

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: run method not working

Posted 24 November 2012 - 07:20 PM

Swing isn't Thread-safe. You would honestly be better off using a Swing Timer. Also, JPanel by default is double-buffered. If you paint() on that, there is no need to implement double-buffering yourself.
Was This Post Helpful? 1
  • +
  • -

#3 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: run method not working

Posted 24 November 2012 - 07:26 PM

cool so with japplet there is no need for update method? only in applet there is need for update method.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: run method not working

Posted 24 November 2012 - 07:31 PM

You really shouldn't mess with update() at all. The paint() method should handle drawing everything. The Timer's ActionListener will trigger an update to the objects being displayed, then invoke repaint().

Also, lastly, if you have a Runnable, an instance of it is meant to be passed to a Thread object. You then need to invoke start() on the Thread.
Was This Post Helpful? 1
  • +
  • -

#5 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: run method not working

Posted 24 November 2012 - 07:33 PM

thanks, also how do i clear screen? right now what is happening its redrawing rect over each other. it look like a long black line.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: run method not working

Posted 24 November 2012 - 07:34 PM

Make sure to invoke super.paint() initially in your paint() method, as it handles key superclass functionality like clearing the old objects.
Was This Post Helpful? 1
  • +
  • -

#7 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: run method not working

Posted 24 November 2012 - 07:45 PM

i hear that you should use paint method in japplet but use:

public void paintComponent(g) {
   super.paintComponent(g);

}


so it i make this method how and where do i call it?

This post has been edited by hwoarang69: 24 November 2012 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#8 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: run method not working

Posted 24 November 2012 - 08:05 PM

rect despair for half sec. than moves to right. despair for half sec .. and so on.
Was This Post Helpful? 0
  • +
  • -

#9 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: run method not working

Posted 24 November 2012 - 09:11 PM

update verson:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JApplet;


public class main extends JApplet implements Runnable
{
	
	//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()
	{
		Thread thread = new Thread(this); //set up thread
		thread.start();                   //start thread jump inside run method
	}/*** 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 ***/
	
	

	
	
	
	
	//########################################################################################################################
	/*** paint method ***/ 
	public void paint(Graphics g)
	{	
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g; 
		
		
		g.fillRect(x, y, width, height);   //player	
		
	}/** end of paint method ***/
	
	

}


This post has been edited by macosxnerd101: 24 November 2012 - 09:31 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#10 Ahmed92  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 42
  • Joined: 10-August 12

Re: run method not working

Posted 25 November 2012 - 12:16 PM

Even though I don't work with JApplet (I prefer JFrame), here is my code.

Main.java

package game;

import javax.swing.JFrame;

public class Main extends Loop {
	
	private static final long serialVersionUID = 1L;

	public Main() {
		Thread thread = new Thread(this);
		setSize(800, 450);
		setTitle("Untitled");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
		createBufferStrategy(3);
		thread.start();
	}
	
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		Main main = new Main();
	}
}



Loop.java

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Loop extends JFrame implements Runnable {
	
	private static final long serialVersionUID = 1L;
	
	int x = 300;
	int y = 200;

	@Override
	public void run() {
		
		try {
			
		} catch(Exception e){} 
		
		while(true) {
			drawStuff();
			
			x++;
			
			try {
				Thread.sleep(20);
			} catch(Exception e){}
		}
	}
	
	private void drawStuff() {
		BufferStrategy bf = this.getBufferStrategy();
		Graphics g = null;

		try {
			g = bf.getDrawGraphics();

			g.clearRect(0, 0, getWidth(), getHeight());
			g.setColor(Color.black);
			g.fillRect(x, y, 200, 100);

		} finally {
			g.dispose();
		}

		bf.show();
		Toolkit.getDefaultToolkit().sync();
	}
}



Hope this can give you a clue as to what to do, any experts please correct me if i'm doing something wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1