i have a little problem
my problem is my playerIcon can auto falldown and it touch the rect the playerIcon stop but press left to walk out the rect it do not fall down again.
i use theard do this (playerRect.y += dy) when touch Rect i do this (dy = 0)then it stop.
how to make it falldown again when the playerIcon awlk out the rect
class Playscreenextends JPanelimplements Runnable{
ImageIcon playbg;
ImageIcon playerIcon;
ImageIcon[] stair;
Point loc =new Point(0,0);
int dx = 3;
int dy = 3;
int sy = 2;
Rectangle[] rects;
Rectangle playerRect;
Random seed =new Random();
String[] fileNames;
boolean firstTime =true;
Thread th;
public Playscreen(){
String img ="stair.jpg";
fileNames =new String[10];
playbg =new ImageIcon("image/bgimg.jpg");
playerIcon =new ImageIcon("image/player.gif");
playerRect =new Rectangle(0, 0, playerIcon.getIconWidth(), playerIcon.getIconHeight());
// define a new thread
th =new Thread (this);
setBackground(Color.white);
setOpaque(true);
registerKeys();
setFocusable(true);
for(int f = 0; f < 10; f++){
fileNames[f] = img ;
}
rects =new Rectangle[fileNames.length];
stair =new ImageIcon[fileNames.length];
for(int j = 0; j < rects.length; j++){
stair[j] =new ImageIcon("Image/" + fileNames[j]);
rects[j] =new Rectangle(stair[j].getIconWidth(), stair[j].getIconHeight());
}
}
protectedvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(playbg.getImage(), 0, 0,this);
g.drawImage(playerIcon.getImage(), playerRect.x, playerRect.y,this);
if(firstTime){
setRects();
firstTime =false;
}
for(int j = 0; j < stair.length; j++){
g.drawImage(stair[j].getImage(), rects[j].x, rects[j].y,this);
}
}
privatevoid setRects(){
Point p;
for(int j = 0; j < rects.length; j++){
do{
p = getRandomLocationFor(rects[j]);
rects[j].setLocation(p);
}while(!notTouching(j));
}
}
privateboolean notTouching(int index){
for(int j = 0; j < index; j++){
if(rects[j].intersects(rects[index]))
returnfalse;
}
returntrue;
}
private Point getRandomLocationFor(Rectangle r){
Point p =new Point();
p.x = seed.nextInt(getWidth() - r.width + 50);
p.y = seed.nextInt(getHeight() - r.height + 50);
return p;
}
privatevoid registerKeys(){
int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
getInputMap(c).put(KeyStroke.getKeyStroke("UP"),"UP");
getActionMap().put("UP", upAction);
getInputMap(c).put(KeyStroke.getKeyStroke("LEFT"),"LEFT");
getActionMap().put("LEFT", leftAction);
getInputMap(c).put(KeyStroke.getKeyStroke("DOWN"),"DOWN");
getActionMap().put("DOWN", downAction);
getInputMap(c).put(KeyStroke.getKeyStroke("RIGHT"),"RIGHT");
getActionMap().put("RIGHT", rightAction);
}
private Action upAction =new AbstractAction(){
publicvoid actionPerformed(ActionEvent e){
}
};
private Action leftAction =new AbstractAction(){
publicvoid actionPerformed(ActionEvent e){
if(playerRect.x > 0 ){
playerRect.x -= dx;
repaint();
}
}
};
private Action downAction =new AbstractAction(){
publicvoid actionPerformed(ActionEvent e){
// start this thread
th.start();
}
};
private Action rightAction =new AbstractAction(){
publicvoid actionPerformed(ActionEvent e){
if(playerRect.x + playerIcon.getIconWidth() < 500){
playerRect.x += dx;
repaint();
}
}
};
publicvoid run(){
// lower ThreadPriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// run a long while (true) this means in our case "always"
while (true){
for(int j = 0; j < rects.length; j++){
if (playerRect.intersects(rects[j])){
dy = 0 ;
}
}
for(int j = 0; j< rects.length; j++){
rects[j].y -= sy;
repaint();
if(rects[j].y < 0){
rects[j].y = 600;
}
}
playerRect.y += dy;
// repaint the image
repaint();
try{
// Stop thread for 20 milliseconds
Thread.sleep (50);
}catch (InterruptedException ex){// do nothing }
// set ThreadPriority to maximum value
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
}
}

