0

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
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by zeroliken
1

you haven't started the thread, add this on your start method

Thread th = new Thread (this);
th.start();
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.