这是个构造函数?
public runThread(Thread t) {
if(t==null){
t= new Thread(this);
t.start();
}
}
还是应该为
public void runThread(Thread t){
...
}
public runThread(Thread t) {
if(t==null){
t= new Thread(this);
t.start();
}
}
还是应该为
public void runThread(Thread t){
...
}
解决方案 »
- 7个8如何(加,减,乘,除)算出等于2000
- could not find the main class
- 关于方法覆盖的一个概念问题
- java 实例缓存
- 一个关于swing控件刷新的问题,请高手解答??在线等,谢谢。来者有分,或多或少!!!
- 本人初学JAVA,想知道哪里有JCreator编辑器方面的资料以及Java Swing方面的资料、电子文档?
- 类的设计问题
- 有谁需要考java程序员
- 希望把值加密后存储,在java中怎样加密,有这方面资料吗?
- About JDBC2.0 & SQL3
- 你能解释这句吗?Thread thread2=new Thread(new ThreadUseRunnable(),"SecondThread");
- 大家进来看看,这段代码应做何解释???
写一段程序贴出来看看?
public runThread(Thread t) ===> public void runThread(Thread t)
import java.awt.*;
import javax.swing.*;/**wo
*
* alin 2003-5-22
*/
public class GamePanel extends JPanel implements Runnable {
private Map map;
private Image[] tileImage;
private Image[] agentImage;
private Image[] goodImage;
int agentX;
int agentRowNumber;
int agentY;
int agentLineNumber;
int focusX;
int focusY; private int mapWidth;
private int mapHigh; private final int UP = 1;
private final int BELOW = 2;
private final int LEFT = 3;
private final int RIGHT = 4;
private int previousMoveDirection = 0;
private int agentCurrentPic = 0; Thread runner;
Thread paintThread;
private GamePanelMouseEvent mouseEvent; public GamePanel(
Image[] tI,
Image[] aI,
Image[] gI,
Map mp,
int agentLN,
int agentRN) { mouseEvent = new GamePanelMouseEvent(this);
addMouseListener(mouseEvent); tileImage = tI;
agentImage = aI;
goodImage = gI;
map = mp;
agentLineNumber = agentLN;
agentY = agentLN * 32;
agentRowNumber = agentRN;
agentX = agentRN * 32; mapHigh = map.tiles.length;
mapWidth = map.tiles[0].length; MediaTracker track = new MediaTracker(this); for (int k = 0; k < tileImage.length; k++)
track.addImage(tileImage[k], k); for (int j = 0; j < agentImage.length; j++)
track.addImage(agentImage[j], tileImage.length + j); for (int m = 0; m < goodImage.length; m++)
track.addImage(
goodImage[m],
tileImage.length + agentImage.length + m);
try {
track.waitForAll();
} catch (InterruptedException e) {
}
if(paintThread==null){
paintThread = new Thread(this);
paintThread.start();
}
} public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D) comp; //»­µØƤ:)...
for (int i = 0; i < mapHigh; i++) {
for (int j = 0; j < mapWidth; j++) {
comp2D.drawImage(tileImage[map.tiles[i][j].tile],
//Ìرð×¢ÒâÕâÀïj¸úiµÄ˳Ðò!!!-_-
j * 32, i * 32, this);
}
} //»­agentËùÔÚÐÐÉÏÃæµÄÎïÆ·²ã(°üÀ¨ÕâÐÐ)
for (int i = 0; i <= agentLineNumber; i++) {
for (int j = 0; j < mapWidth; j++) {
if (map.tiles[i][j].good1 != -1)
comp2D.drawImage(
goodImage[map.tiles[i][j].good1],
j * 32,
i * 32,
this);
}
}
//»­agent
comp2D.drawImage(
agentImage[agentCurrentPic],
agentX,
agentY - 16,
this); //»­agentËùÔÚÐÐÏÂÃæµÄÎïÆ·²ã
if (agentLineNumber != mapHigh - 1) {
for (int i = agentLineNumber + 1; i < mapHigh; i++) {
for (int j = 0; j < mapWidth; j++)
if (map.tiles[i][j].good1 != -1)
comp2D.drawImage(
goodImage[map.tiles[i][j].good1],
j * 32,
i * 32,
this);
}
}
//»­Ìì¿Õ²ã^_^
for (int i = 0; i < mapHigh; i++) {
for (int j = 0; j < mapWidth; j++) {
if (map.tiles[i][j].good2 != -1)
comp2D.drawImage(
goodImage[map.tiles[i][j].good2],
j * 32,
i * 32,
this);
}
}
} public void run() {
while(paintThread == Thread.currentThread()){
repaint();
pause(20);
}
while(runner == Thread.currentThread()) {
moveAgent();
//ÏÂÃæÓï¾ä°ÑÈËÎï½Å·ÅÏÂ:)
if (previousMoveDirection == LEFT && agentCurrentPic != 6) {
agentCurrentPic = 6;
} else if (
previousMoveDirection == RIGHT && agentCurrentPic != 9) {
agentCurrentPic = 9;
} else if (previousMoveDirection == UP && agentCurrentPic != 3) {
agentCurrentPic = 3;
} else if (
previousMoveDirection == BELOW && agentCurrentPic != 0) {
agentCurrentPic = 0;
}
previousMoveDirection = 0;
stopThread();
}
} public void runThread() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
} private void stopThread() {
if (runner != null) {
runner = null;
}
}
private void pause(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
}
} public void moveAgent() {
//ͼƬƽÒƵľàÀë
int step = 5;
//Òƶ¯Ò»´Î¼ä¸ôʱ¼ä
int time = 40;
//´æ·Å¼È¶¨²½·¥×ßÕâÒ»²½Î»ÖÃ
int stemp;
int stempTo; if (focusX < agentX) {
stemp = agentX - step;
//¼ÙÈç°´¼È¶¨²½·¥×ßÕâ²½³¬¹ýÄ¿±êx×ø±êÁË,¾Í°ÑÕâ²½¶¨ÔÚÄ¿±ê´¦
if (focusX > stemp)
stemp = focusX;
stempTo = stemp / 32;
//¼ÙÈçÕâ²½½øÈë²»¿É½øÈëÇø,ÖÐÖ¹Òƶ¯,×¢Òâ˳Ðò
if (map.tiles[agentLineNumber][stempTo].block == false) {
agentX = agentRowNumber * 32;
changeAgentPic(LEFT);
pause(time);
return;
}
agentX = stemp;
agentRowNumber = stempTo;
changeAgentPic(LEFT);
pause(time);
moveAgent();
} else if (focusX > agentX) {
stemp = agentX + step;
if (focusX < stemp)
stemp = focusX;
stempTo = stemp / 32;
if (map.tiles[agentLineNumber][stempTo].block == false) {
agentX = agentRowNumber * 32;
changeAgentPic(RIGHT);
pause(time);
return;
}
agentX = stemp;
agentRowNumber = stempTo;
changeAgentPic(RIGHT);
pause(time);
moveAgent();
} else {
if (focusY < agentY) {
stemp = agentY - step;
if (focusY > stemp)
stemp = focusY;
stempTo = stemp/32;
if (map.tiles[stempTo][agentRowNumber].block == false){
agentY = agentLineNumber*32;
changeAgentPic(UP);
pause(time);
return;
}
agentY = stemp;
agentLineNumber = stempTo;
changeAgentPic(UP);
pause(time);
moveAgent();
} else if (focusY > agentY) {
stemp = agentY + step;
if (focusY < stemp)
stemp = focusY;
stempTo = stemp/32;
if (map.tiles[stempTo][agentRowNumber].block == false){
agentY = agentLineNumber*32;
changeAgentPic(BELOW);
pause(time);
return;
}
agentY = stemp;
agentLineNumber = stempTo;
changeAgentPic(BELOW);
pause(time);
moveAgent();
} else {
return;
}
}
} private void changeAgentPic(int previousDirection) {
int firstPic = 0;
int thirdPic = 0;
if (previousDirection == LEFT) {
firstPic = 6;
} else if (previousDirection == RIGHT) {
firstPic = 9;
} else if (previousDirection == UP) {
firstPic = 3;
} else {
firstPic = 0;
}
thirdPic = firstPic + 2;
if (previousMoveDirection == previousDirection) {
agentCurrentPic++;
if (agentCurrentPic > thirdPic)
agentCurrentPic = firstPic;
} else {
agentCurrentPic = firstPic;
previousMoveDirection = previousDirection;
}
}}
能不能用个简单的main...来试试. 很多东西混在一起就不容易看了.(我不是说你的代码不好或有问题,只是难以看出问题,手头没有编译器...)
package gui;import map.*;
import java.awt.*;
import javax.swing.*;/**wo
*
* alin 2003-5-22
*/
public class GamePanel extends JPanel implements Runnable {
private Map map;
private Image[] tileImage;
private Image[] agentImage;
private Image[] goodImage;
int agentX;
int agentRowNumber;
int agentY;
int agentLineNumber;
int focusX;
int focusY; private int mapWidth;
private int mapHigh; private final int UP = 1;
private final int BELOW = 2;
private final int LEFT = 3;
private final int RIGHT = 4;
private int previousMoveDirection = 0;
private int agentCurrentPic = 0; Thread runner;
Thread paintThread;
private GamePanelMouseEvent mouseEvent; public GamePanel(
Image[] tI,
Image[] aI,
Image[] gI,
Map mp,
int agentLN,
int agentRN) { mouseEvent = new GamePanelMouseEvent(this);
addMouseListener(mouseEvent); tileImage = tI;
agentImage = aI;
goodImage = gI;
map = mp;
agentLineNumber = agentLN;
agentY = agentLN * 32;
agentRowNumber = agentRN;
agentX = agentRN * 32; mapHigh = map.tiles.length;
mapWidth = map.tiles[0].length; MediaTracker track = new MediaTracker(this); for (int k = 0; k < tileImage.length; k++)
track.addImage(tileImage[k], k); for (int j = 0; j < agentImage.length; j++)
track.addImage(agentImage[j], tileImage.length + j); for (int m = 0; m < goodImage.length; m++)
track.addImage(
goodImage[m],
tileImage.length + agentImage.length + m);
try {
track.waitForAll();
} catch (InterruptedException e) {
}
if(paintThread==null){
paintThread = new Thread(this);
paintThread.start();
}
} public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D) comp; //画地皮:)...
for (int i = 0; i < mapHigh; i++) {
for (int j = 0; j < mapWidth; j++) {
comp2D.drawImage(tileImage[map.tiles[i][j].tile],
//特别注意这里j跟i的顺序!!!-_-
j * 32, i * 32, this);
}
} //画agent所在行上面的物品层(包括这行)
for (int i = 0; i <= agentLineNumber; i++) {
for (int j = 0; j < mapWidth; j++) {
if (map.tiles[i][j].good1 != -1)
comp2D.drawImage(
goodImage[map.tiles[i][j].good1],
j * 32,
i * 32,
this);
}
}
//画agent
comp2D.drawImage(
agentImage[agentCurrentPic],
agentX,
agentY - 16,
this); //画agent所在行下面的物品层
if (agentLineNumber != mapHigh - 1) {
for (int i = agentLineNumber + 1; i < mapHigh; i++) {
for (int j = 0; j < mapWidth; j++)
if (map.tiles[i][j].good1 != -1)
comp2D.drawImage(
goodImage[map.tiles[i][j].good1],
j * 32,
i * 32,
this);
}
}
//画天空层^_^
for (int i = 0; i < mapHigh; i++) {
for (int j = 0; j < mapWidth; j++) {
if (map.tiles[i][j].good2 != -1)
comp2D.drawImage(
goodImage[map.tiles[i][j].good2],
j * 32,
i * 32,
this);
}
}
} public void run() {
while(paintThread == Thread.currentThread()){
repaint();
pause(20);
}
while(runner == Thread.currentThread()) {
moveAgent();
//下面语句把人物脚放下:)
if (previousMoveDirection == LEFT && agentCurrentPic != 6) {
agentCurrentPic = 6;
} else if (
previousMoveDirection == RIGHT && agentCurrentPic != 9) {
agentCurrentPic = 9;
} else if (previousMoveDirection == UP && agentCurrentPic != 3) {
agentCurrentPic = 3;
} else if (
previousMoveDirection == BELOW && agentCurrentPic != 0) {
agentCurrentPic = 0;
}
previousMoveDirection = 0;
stopThread();
}
} public void runThread() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
} private void stopThread() {
if (runner != null) {
runner = null;
}
}
private void pause(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
}
} public void moveAgent() {
//图片平移的距离
int step = 5;
//移动一次间隔时间
int time = 40;
//存放既定步伐走这一步位置
int stemp;
int stempTo; if (focusX < agentX) {
stemp = agentX - step;
//假如按既定步伐走这步超过目标x坐标了,就把这步定在目标处
if (focusX > stemp)
stemp = focusX;
stempTo = stemp / 32;
//假如这步进入不可进入区,中止移动,注意顺序
if (map.tiles[agentLineNumber][stempTo].block == false) {
agentX = agentRowNumber * 32;
changeAgentPic(LEFT);
pause(time);
return;
}
agentX = stemp;
agentRowNumber = stempTo;
changeAgentPic(LEFT);
pause(time);
moveAgent();
} else if (focusX > agentX) {
stemp = agentX + step;
if (focusX < stemp)
stemp = focusX;
stempTo = stemp / 32;
if (map.tiles[agentLineNumber][stempTo].block == false) {
agentX = agentRowNumber * 32;
changeAgentPic(RIGHT);
pause(time);
return;
}
agentX = stemp;
agentRowNumber = stempTo;
changeAgentPic(RIGHT);
pause(time);
moveAgent();
} else {
if (focusY < agentY) {
stemp = agentY - step;
if (focusY > stemp)
stemp = focusY;
stempTo = stemp/32;
if (map.tiles[stempTo][agentRowNumber].block == false){
agentY = agentLineNumber*32;
changeAgentPic(UP);
pause(time);
return;
}
agentY = stemp;
agentLineNumber = stempTo;
changeAgentPic(UP);
pause(time);
moveAgent();
} else if (focusY > agentY) {
stemp = agentY + step;
if (focusY < stemp)
stemp = focusY;
stempTo = stemp/32;
if (map.tiles[stempTo][agentRowNumber].block == false){
agentY = agentLineNumber*32;
changeAgentPic(BELOW);
pause(time);
return;
}
agentY = stemp;
agentLineNumber = stempTo;
changeAgentPic(BELOW);
pause(time);
moveAgent();
} else {
return;
}
}
} private void changeAgentPic(int previousDirection) {
int firstPic = 0;
int thirdPic = 0;
if (previousDirection == LEFT) {
firstPic = 6;
} else if (previousDirection == RIGHT) {
firstPic = 9;
} else if (previousDirection == UP) {
firstPic = 3;
} else {
firstPic = 0;
}
thirdPic = firstPic + 2;
if (previousMoveDirection == previousDirection) {
agentCurrentPic++;
if (agentCurrentPic > thirdPic)
agentCurrentPic = firstPic;
} else {
agentCurrentPic = firstPic;
previousMoveDirection = previousDirection;
}
}