gpt4 book ai didi

java - SwingerWorker 在小程序中用于角色动画?

转载 作者:行者123 更新时间:2023-12-01 23:51:20 25 4
gpt4 key购买 nike

使用我当前的代码,我有一个角色可以加载到动画的 Sprite 表中。当我运行代码时出现我的问题。游戏运行良好,只是玩家加载/循环 Sprite 的速度太快。我相信这源于整体游戏时间/更新。我应该声明该游戏是通过小程序运行的。我收集了一些有关 SwingerWorkers 的信息,但我不知道如何根据我的需要操纵它。如果有人可以看一下我的代码并让我知道我能做什么,我将不胜感激。

玩家构造函数:

public Player(Image pic, ImageObserver IO){

positionX=200;
positionY=200;
destinationX=(int)positionX;
destinationY=(int)positionY;
speed = 2.3;

girlImage=pic;
this.io=IO;
width=120;
height=100;
//this.playerImage=playerImage;

// Gets each sprite
BufferedImage img = createImage();

for(int j=0;j<4;j++)
{
for(int i = 0; i < frameCount; i++) {
girlAll[j][i] = girl[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
}//end of for
}//end of for
}

// Image loader
private BufferedImage createImage(){
//BufferedImage bufferedImage;
try {
girl[0] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northeast_walk.png"));
girl[1] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northwest.png"));
girl[2] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southeast.png"));
girl[3] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southwest.png"));

} catch (IOException e) {
e.printStackTrace();
}
return null;
}

绘制方法:

public void draw(Graphics g){
//g.drawImage(girlImage, (int)positionX,(int) positionY,io);

picNum = (picNum + 1) % frameCount;
pics = girlAll[x];
if (x==0)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
if (x==1)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
if (x==2)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
if (x==3)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
}

SwingWorker 尝试:

SwingWorker worker = new SwingWorker<BufferedImage[], Void>() {

public BufferedImage[] doInBackground(){
//BufferedImage bufferedImage;
try {
girl[0] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northeast_walk.png"));
girl[1] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northwest.png"));
girl[2] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southeast.png"));
girl[3] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southwest.png"));

} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public void loadImg() {

doInBackground();

for(int j=0;j<4;j++)
{
for(int i = 0; i < frameCount; i++) {
girlAll[j][i] = girl[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
}//end of for
}//end of for

}

};

}

运行游戏(来自“Game”类):

public void init(){
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}

}

线程处理:

private void gameStuff(){
try {Thread.sleep(sleepTime);}
catch (InterruptedException e) {}

mouseClicked=false;
}

游戏渲染:

public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();

}

// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

// draw elements in background
//dbg.setColor (getForeground());
paint (dbg);

// draw image on the screen
g.drawImage (dbImage, 0, 0, this);

}

另一种渲染方法:

private void hud(Graphics g){
g.setColor(Color.orange);
g.fillRect(300, 0, 500, 80);
g.fillRect(200, 0, 100, 80);

//SHOVEL IMAGE AND BUTTON
g.drawImage(shovelImage, 520,0,this);
g.setColor(Color.black);
g.drawRect(520, 0, 60, 80);//show boundaries
if(mouseClicked && new Rectangle(520, 0, 60, 80).contains(mx,my))
toolSelected=1; //sets tool selected to shovel

//ROCKS IMAGE AND BUTTON
g.drawImage(rocksImage, 600,0, this);
g.drawRect(600, 0, 60, 80);//show boundaries
if(mouseClicked && new Rectangle(600, 0, 60, 80).contains(mx,my))
toolSelected=2; //sets tool selected to shovel

//JACKHAMMER IMAGE AND BUTTON
g.drawImage(jackHammerImage, 680,0, this);
g.drawRect(680, 0, 60, 80);//show boundaries
if(mouseClicked && new Rectangle(680, 0, 60, 80).contains(mx,my))
toolSelected=3; //sets tool selected to shovel

if(timer.getAngle()<360){
timer.draw(g);
}
if(timer.getAngle()==360){
raining=true;
timer.reset();
addDrops();
roundNum++;
puddles();

}
drawProgressBar(g);

if(toolSelected == 1)
{
Message = "Native Delaware plants";
Message2 = "are good";
}
else if(toolSelected == 2)
{
Message = "Rocks with Runoff";
Message2 = " ";
}
else if(toolSelected == 3)
{
Message = "Permeated Driveways";
Message2 = "help with excesse water";
}
else{
Message = "Play On";
Message2 = " ";
}
g.drawString("Round "+roundNum, 350, 30);
g.drawString(Message, 205, 40);
g.drawString(Message2, 205, 55);

drawTiles(g);

}

最佳答案

  • 在使用 SwingWorker 之前,如果您想做的只是一个时间控制的简单游戏循环,请使用 Swing Timer相反。
  • 如果我要交换 Sprite 图像,我会考虑交换 JLabel 中的图标作为我尝试的最简单的第一件事。
  • 不要覆盖 Swing GUI 的 update(Graphics g)。这是用于 AWT 的,但对于 Swing 来说并不合适。图形教程会告诉您这一点。
  • 不要直接调用 paint(...)paintComponent(...)。欲了解更多详情,请查看basic Swing drawing tutorial还有a more advanced article关于 AWT 和 Swing 中的绘画。

例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class SpriteAnimationApplet extends JApplet {
private static final String SPRITE_SHEET_SPEC = "http://www.funorb.com/img/images/game/"
+ "central/dev_diary/sprite_sheet_full.gif";
private static final int SPRITE_ROWS = 8; // an 8 x 8 sprite sheet

@Override
public void init() {
try {
final Icon[] icons = SpriteIO.getSprites(SPRITE_SHEET_SPEC, SPRITE_ROWS);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
SpriteAnimationPanel spritePanel = new SpriteAnimationPanel(icons);
getContentPane().add(spritePanel);
spritePanel.startAnimation();
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
System.exit(-1);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(-1);
} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}

}

class SpriteIO {
public static Icon[] getSprites(String spriteSheetSpec, int spriteRows)
throws MalformedURLException, IOException {
Icon[] icons = new Icon[spriteRows * spriteRows];
URL spriteSheetUrl = new URL(spriteSheetSpec);
BufferedImage spriteSheet = ImageIO.read(spriteSheetUrl);
double wD = (double) spriteSheet.getWidth() / spriteRows;
double hD = (double) spriteSheet.getHeight() / spriteRows;
int w = (int) wD;
int h = (int) hD;
for (int i = 0; i < spriteRows; i++) {
for (int j = 0; j < spriteRows; j++) {
int x = (int) (i * wD);
int y = (int) (j * hD);
BufferedImage img = spriteSheet.getSubimage(x, y, w, h);

icons[j * spriteRows + i] = new ImageIcon(img);
}
}
return icons;
}
}

@SuppressWarnings("serial")
class SpriteAnimationPanel extends JPanel {
private static final int TIMER_DELAY = 200;
private Icon[] icons;
private JLabel animationLabel = new JLabel();

public SpriteAnimationPanel(Icon[] icons) {
this.icons = icons;
setLayout(new BorderLayout());
add(animationLabel );
}

public void startAnimation() {
Timer spriteTimer = new Timer(TIMER_DELAY, new ActionListener() {
private int iconIndex = 0;

@Override
public void actionPerformed(ActionEvent arg0) {
animationLabel.setIcon(icons[iconIndex]);
iconIndex++;
iconIndex %= icons.length;
}
});
spriteTimer.start();
}
}

关于java - SwingerWorker 在小程序中用于角色动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com