i need help~
i make this code for move the playerimage falling down and when it touch the rectimage it stop and also the playerimage not on the rectimage it falling down again.But here is the problem the playerimage have stop when touch but it don't falling down again when the playerimage not on the image rect
i have just one day to do this . tomorrow i have to give this program to my teacher.thank all come to see
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;
class Playscreenextends JPanelimplements Runnable{
ImageIcon playbg;
ImageIcon playerIcon;
ImageIcon[] stair;
Point loc =new Point(0,0);
int dx = 3;
int dy = 3;
Rectangle[] rects;
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");
// 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(), loc.x, loc.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 +10);
p.y = seed.nextInt(getHeight() - r.height + 10);
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(loc.x > 0 ){
loc.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(loc.x + playerIcon.getIconWidth() < 500){
loc.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 (loc.y + playerIcon.getIconHeight() > 550 || loc.y + playerIcon.getIconHeight() == rects[j].y && loc.x > rects[j].x && loc.x + playerIcon.getIconWidth() < rects[j].x + stair[j].getIconWidth()){
th.stop();
}
}
loc.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);
}
}
}
}
Message was edited by:
nickgoldgodl

