programming challenge in java-2

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 23, 2024
hello friend's,
check out this code
import java.applet.*;
import java.awt.*;
/*<APPLET CODE="k.class" HEIGHT=400 WIDTH=900></APPLET>*/
public class k extends Applet implements Runnable
{
Image img,im,p;
int x=0,y=0;
Thread t,t1;
public void init()
{
img=getImage(getDocumentBase(),"cc.gif");
}
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
try
{
for(int i=0;i<=20;i++)
{
p=img;
repaint();
t.sleep(50);
x=x+20;
}
}
catch(InterruptedException e)
{
System.out.println("interrupted exception ");
}
}
public void paint(Graphics g)
{
g.drawImage(p,x,20,100,100,this);
}
}
This is a program which move an image and stop at some distance.
You have to edit it in this way that after 1st image stop 2nd image start to move from same position and stop just before 1st one.
.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 24, 2010

    check it out

    Really it was challenge 😀
    import java.applet.*;
    import java.awt.*;
    /*<APPLET CODE="k.java" HEIGHT=400 WIDTH=900></APPLET>*/
    public class k extends Applet implements Runnable
    {
        Image img[]=new Image[2],p;
        int x=0,y=0,limit=20,i,width=60,height=60;
        int ext=(limit*limit)+width;
        Thread t,t1;
        public void init()
        {
            img[0]=getImage(getDocumentBase(),"Desert.jpg");
            img[1]=getImage(getDocumentBase(),"Hydrangeas.jpg");
        }
        public void start()
        {
            t=new Thread(this);
            t.start();
        }
    
        public void run()
        {
        try{
            for(i=0;i<2;i++)
            {        x=0;
    
            
            for(int j=0;j<=limit;j++)
            {
                p=img[i];
                t.sleep(50);
                repaint();
                x=x+20;
            }
            limit=limit-1;
            }
    
    
        
            }catch(Exception e){}
    }
    
    
    public void paint(Graphics g)
        {
            if(i==0)
            {
                g.drawImage(p,x,30,width,height,this);
            }
            if(i>=1)
            {
                g.drawImage(p,x,30,width,height,this);
                g.drawImage(img[0],ext,30,width,height,this);
            }
        }
    
    }
    
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 24, 2010

    Nice find goyal..!!
    So you are the winner of java programming challenge-2... 😁
    .
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 24, 2010

    Well This was the solution what i want to post today on 4PM.
    Check out this.
    import java.applet.*;
    import java.awt.*;
    /*<APPLET CODE="k.class" HEIGHT=400 WIDTH=900></APPLET>*/
    public class k extends Applet implements Runnable
    {
    Image img,im,p;
    int x=0,y=0;
    Thread t,t1;
    public void init()
    {
    img=getImage(getDocumentBase(),"cc.gif");
    im=getImage(getDocumentBase(),"ee.jpg");
    }
    public void start()
    {
    t=new Thread(this);
    t.start();
    }
    public void run()
    {
    try
    {
    for(int i=0;i<=20;i++)
    {
    p=img;
    repaint();
    t.sleep(50);
    x=x+20;
    }
    for(int i=0;i<=15;i++)
    {
    p=im;
    repaint();
    t.sleep(100);
    y=y+20;
    }
    }
    catch(InterruptedException e)
    {
    System.out.println("interrupted exception ");
    }
    }
    public void paint(Graphics g)
    {
    if(p==img)
    {
    g.drawImage(p,x,20,100,100,this);
    }
    else
    {
    g.drawImage(img,x,20,100,100,this);
    g.drawImage(p,y,20,100,100,this);
    }
    }
    }
  • d_vipul

    d_vipul

    @d-vipul-wP6Syg Sep 25, 2010

    nice one guys........