gpt4 book ai didi

尝试在 OS X Mavericks 中绘制图形时 Java 崩溃

转载 作者:行者123 更新时间:2023-11-30 09:13:44 25 4
gpt4 key购买 nike

我正在学习 Java 游戏开发教程,虽然这似乎适用于 Windows,但 Java 在我的 Macbook Pro 上崩溃并出现以下错误:

C [libGL Image.dylib+0x24221] storeColor+0x5d1

目前项目设置在eclipse中,有以下类:

游戏:

package Main;
/**
* Created by beuden on 1/3/14.
*/

import javax.swing.JFrame;

public class Game {

public static void main(String[] args)
{

JFrame window = new JFrame("Dragon Tale");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new GamePanel());
window.setResizable(false);
window.pack();
window.setVisible(true);
}
}

游戏面板:

package Main;

import GameState.GameStateManager;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;

/**
* Created by beuden on 1/3/14.
*/
public class GamePanel extends JPanel implements Runnable, KeyListener{

// Dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
Dimension d = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);

// game Thread
private Thread thread;
private boolean running;
private int FPS = 60;
private long targetTime = 1000 / FPS;

//image
private BufferedImage image;
private Graphics2D g;

// game state manager
private GameStateManager gsm;

public GamePanel()
{
super();
setPreferredSize(d);
setMaximumSize(d);
setMinimumSize(d);


setFocusable(true);
requestFocus();
}

public void addNotify()
{
super.addNotify();
if(thread == null)
{
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}

public void init()
{
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;

gsm = new GameStateManager();



}

public void run()
{
init();

long start,elapsed,wait;
long lastTime = System.nanoTime();
double nsPerTick = 1000000000/60D;

int frames = 0;
int ticks = 0;

long lastTimer = System.currentTimeMillis();
double delta = 0; // Now many unprocessed nanoseconds have gone by so far.

// Game Loop
while(running)
{


long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;

while(delta >=1)
{
ticks++;
update();
delta -=1;
shouldRender = true;
}
if(shouldRender)
{
frames++;
draw();
drawToScreen();
}

if(System.currentTimeMillis() - lastTimer > 1000)
{
lastTimer += 1000;
System.out.println("Frames: " + frames + "," + "Ticks: " + ticks);
frames = 0;
ticks = 0;
}

}
}

public void update()
{
gsm.update();
}

public void draw()
{
gsm.draw(g);
}

public void drawToScreen()
{
Graphics g2 = getGraphics();
g2.drawImage(image,0,0,(WIDTH * SCALE), (HEIGHT * SCALE), null);

g2.dispose();
}

public void keyTyped(KeyEvent key){}
public void keyPressed(KeyEvent key){
gsm.keyPressed(key.getKeyCode());
}
public void keyReleased(KeyEvent key){
gsm.keyReleased(key.getKeyCode());
}
}

游戏状态:

package GameState;

import java.awt.*;

/**
* Created by beuden on 1/3/14.
*/
public abstract class GameState {

protected GameStateManager gsm;
public abstract void init();
public abstract void update();
public abstract void draw(Graphics2D g);
public abstract void keyPressed(int k);
public abstract void keyReleased(int k);
}


package GameState;

import java.awt.*;
import java.util.ArrayList;

/**
* Created by beuden on 1/3/14.
*/
public class GameStateManager {

private ArrayList<GameState> gameStates;
private int currentState;

public static final int MENUSTATE = 0;
public static final int LEVEL1STATE = 1;

public GameStateManager()
{
gameStates = new ArrayList<GameState>();

currentState = MENUSTATE;
gameStates.add(new MenuState(this));

}

public void setState(int state)
{
currentState = state;
gameStates.get(currentState).init();
}

public void update()
{
gameStates.get(currentState).update();

}

public void draw(Graphics2D g)
{
gameStates.get(currentState).draw(g);
}

public void keyPressed(int k)
{
gameStates.get(currentState).keyPressed(k);
}

public void keyReleased(int k)
{
gameStates.get(currentState).keyReleased(k);
}
}

MenuState - 这是游戏可以处于的单一状态,还会有 1 级状态。

package GameState;

import java.awt.*;
import java.awt.event.KeyEvent;

import TileMap.Background;
/**
* Created by beuden on 1/4/14.
*/
public class MenuState extends GameState {

private Background bg;

private int currentChoice = 0;
private String[] options = {
"Start",
"Options",
"Quit"
};

private Color titleColor;
private Font titleFont;

private Font font;
public MenuState(GameStateManager gsm)
{
this.gsm = gsm;

try{
bg = new Background("/Backgrounds/menubg.gif", 1);
bg.setVector(-0.1, 0);

titleColor = new Color(128,0,0);
titleFont = new Font("Century Gothic", Font.PLAIN, 28);

font = new Font("Ariel", Font.PLAIN, 12);
}
catch(Exception e){

}
}

public void init(){}

public void update(){
bg.update();
}

public void draw(Graphics2D g){
//draw background
bg.draw(g);

//draw font
g.setColor(titleColor);
g.setFont(titleFont);
g.drawString("Dragon Tale", 80, 70);

//draw menu options
g.setFont(font);

for(int i = 0; i < options.length; i++)
{
if(i == currentChoice)
{
g.setColor(Color.BLACK);

}
else{
g.setColor(Color.red);
}
g.drawString(options[i], 145, 140 + i* 15);
}

}

public void select()
{
if(currentChoice == 0)
{
//Start
}
if(currentChoice == 1)
{
//Options
}
if(currentChoice == 2)
{
//Quit
System.exit(0);
}

}
public void keyPressed(int k){
if(k == KeyEvent.VK_ENTER)
{
select();
}

if(k == KeyEvent.VK_UP)
{
currentChoice--;
if(currentChoice < 0)
{
currentChoice = options.length - 1;
}
}
if(k == KeyEvent.VK_DOWN)
{
currentChoice++;
if(currentChoice == options.length)
{
currentChoice = 0;
}
}
}
public void keyReleased(int k){}

}

和背景:

package TileMap;

import Main.GamePanel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
* Created by beuden on 1/4/14.
*/
public class Background {

private BufferedImage image;

private double x, y,dx,dy;

private double moveScale;

public Background(String s, double ms)
{
try{
image = ImageIO.read(getClass().getResourceAsStream(s));
}
catch(Exception e)
{
System.out.println("Unable to read image!!!");
e.printStackTrace();
}

moveScale = ms;
}

public void setPosition(double x, double y)
{
this.x = (x * moveScale)%GamePanel.WIDTH;
this.y = (y * moveScale)%GamePanel.HEIGHT;
}

public void setVector(double dx, double dy)
{
this.dx = dx;
this.dy = dy;
}

public void update()
{
x += dx;
y += dy;
}

public void draw(Graphics2D g)
{
g.drawImage(image, (int)x, (int)y, null);

if(x < 0){
g.drawImage(image, (int)x + GamePanel.WIDTH, (int)y, null);
}
if(x > 0){
g.drawImage(image, (int)x - GamePanel.WIDTH, (int)y, null);
}

}
}

问题出现在 GamePanel 类中。

下一行:

g2.drawImage(image,0,0,(WIDTH * SCALE), (HEIGHT * SCALE), null);

导致崩溃。但将其更改为:

g2.drawImage(image, 0,0,WIDTH,HEIGHT,null);

并且游戏呈现完美,尽管上面的测量值 (320,240) 使其变小但它会绘制。

我不确定为什么缩放会突然导致崩溃。即使是对任何变量的简单 +1 也会导致它崩溃。

如有任何帮助,我们将不胜感激。

最佳答案

我在 OS/X Mavericks 上调用 Graphics.drawImage() 时也遇到了同样的问题,并设法通过手动安装 JDK 1.6 并使用它而不是 1.7 运行我的代码来解决这个问题。

参见 how-do-i-install-jdk-1-6-in-mavericks有关如何执行此操作的说明。

关于尝试在 OS X Mavericks 中绘制图形时 Java 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916107/

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