import java.util.*;
import java.awt.*;
import javax.swing.*;
class LoadRx extends JFrame
{
Image red;
Image yellow;
int y=329;
int floor=329;
int x=3;
int yLoc = 0;
LoadRx()
{
super("some name");
red=new ImageIcon("red.png").getImage();
//"images/geek/geek--g--.gif").getImage();
yellow=new ImageIcon("yellow.png").getImage();
//"images/geek/geek---h-.gif").getImage();
getContentPane().add(new DisplayPanel());
// setVisible(true);
setSize(175,400);
// setResizable(false);
// This should always be the last call.
setVisible(true);
}
class DisplayPanel extends JPanel implements Runnable
{
DisplayPanel()
{
Thread t=new Thread(this);
// To keep your gui responsive to user input.
t.setPriority(Thread.NORM_PRIORITY);
t.start();
}
// Java wil be easier to learn if you will copy
// these method signatures exactly as shown in
// the javadocs. There is still plenty of room
// to do things your way, eg, giving meaningful,
// descriptive, verbose names to your variables.
protected void paintComponent(Graphics g)
{
// Eliminate artifacts:
super.paintComponent(g);
// This cast not necessary here, but okay.
//Graphics2D g2=(Graphics2D)g;
// Draw fixed-location image:
g.drawImage(red, x, y, this);
// Draw moving image:
// "yLoc" is controlled from within your
// event code, here, the run method
// of your Runnable.
g.drawImage(yellow, x, yLoc, this);
}
// i just want to place the red image fixed at the bottom
// and start new yellow image thread i,e
// This is the implementation of the Runnable interface.
// In java, when you implement an interface you must
// provide an implementation of every method defined
// in the interface. Runnable has a single method: [i]run{/i].
public void run()
{
// This resets your member variable "y" to zero.
// You want it to be left alone so it will show
// "red" at the bottom of the component. You could
// reset the member variable "yLoc" here if you are
// going to do multiple runs through this method.
// y=0;
while(yLoc != floor)
{
// The variable "y" was/is used to position "red".
// If you change it here you will reposition "red".
// y++;
// Solution: use a different variable to locate "yellow".
yLoc++;
// Show the new location of "yellow".
repaint();
try
{
// Draw only in paintComponent.
// Adjust variables here in event code.
// q.drawImage(b,x,y,this);
Thread.sleep(50);
}
catch(InterruptedException e)
{
break; // get out of this while loop
}
}
}
}
public static void main(String[] args)
{
new LoadRx();
}
}