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 ***/
}
run method not working
Page 1 of 19 Replies - 376 Views - Last Post: 25 November 2012 - 12:16 PM
#1
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?
Replies To: run method not working
#2
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.
#3
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.
#4
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.
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.
#5
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.
#6
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.
#7
Re: run method not working
Posted 24 November 2012 - 07:45 PM
i hear that you should use paint method in japplet but use:
so it i make this method how and where do i call it?
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
#8
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.
#9
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
#10
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
Loop.java
Hope this can give you a clue as to what to do, any experts please correct me if i'm doing something wrong.
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|