gpt4 book ai didi

java - libsdl 类消耗所有 JVM(阻止代码的执行)

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:48 27 4
gpt4 key购买 nike

我使用图形库 libsdl(针对 Java 的 C SDL 绑定(bind))以及多代理库 Jade。

我有一个代理,它创建一个使用libsdl创建的GUI类,创建后,代理的代码不再执行。事实上,唯一运行的是 GUI 类。

为什么?

有人知道如何解决这个问题吗?我希望 GUI 与程序的其他代理并行运行,但一旦启动,它就会占用所有 JVM。

以下是我的代码的相关部分(首先是代理):

package main;

import java.util.HashMap;
import java.util.Map;

import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import sdljava.SDLException;

public class Display extends Agent
{
private static final long serialVersionUID = 1L;
Map<String, Position> m_drones = new HashMap<String, Position>();
GUI m_gui = null;

protected void setup()
{
for(int i = 0 ; i < Constants.numberDrones ; i++)
{
String name = "Drone" + Integer.toString(i);
Position position = getFreePosition();
Object[] arguments = {i, position};

try
{
this.getContainerController().createNewAgent(name, "main.Drone", arguments).start();
}
catch(Exception exception)
{
System.out.println("Erreur : " + exception.getMessage());
System.exit(-1);
}

m_drones.put(name, position);
}

try
{
m_gui = new GUI(m_drones);
}
catch (SDLException | InterruptedException e)
{
e.printStackTrace();
}

addBehaviour(new RetrievePositions(this, Constants.retrievePositionsPeriod));
addBehaviour(new UpdateGUI(this, Constants.updateGUIPeriod));
}

Map<String, Position> getDrones()
{
return m_drones;
}

Position getFreePosition()
{
int x, y;
Position position = new Position(0, 0);

do
{
x = (int) Math.floor(Math.random() * Constants.environmentWidth) * Constants.dotSize;
y = (int) Math.floor(Math.random() * Constants.environmentHeight) * Constants.dotSize;

position.setPosition(x, y);
}
while(m_drones.containsValue(position));

return position;
}
}

class RetrievePositions extends TickerBehaviour
{
private static final long serialVersionUID = 1L;

Display display = (Display) this.myAgent;
Map<String, Position> drones = display.getDrones();

public RetrievePositions(Agent agent, long period)
{
super(agent, period);
}

public void onTick()
{
System.out.println("retrievePositions");
}
}

class UpdateGUI extends TickerBehaviour
{
private static final long serialVersionUID = 1L;

Display display = (Display) this.myAgent;

public UpdateGUI(Agent agent, long period)
{
super(agent, period);
}

protected void onTick()
{
System.out.println("updateGUI");
}
}

和图形用户界面:

    package main;

import java.util.HashMap;
import java.util.Map;

import sdljava.SDLException;
import sdljava.SDLMain;
import sdljava.event.SDLEvent;
import sdljava.video.SDLRect;
import sdljava.video.SDLSurface;
import sdljava.video.SDLVideo;

public class GUI
{
SDLSurface m_screen = null;
Map<String, SDLSurface> m_surfaces = new HashMap<String, SDLSurface>();
boolean m_running = true;

void initSurfaces(Map<String, Position> drones) throws SDLException
{
for(Map.Entry<String, Position> entry : drones.entrySet())
{
SDLSurface surface = SDLVideo.createRGBSurface(SDLVideo.SDL_HWSURFACE, Constants.dotSize, Constants.dotSize, 32, 0, 0, 0, 0);
SDLRect rect = new SDLRect(entry.getValue().getX(), entry.getValue().getY());
surface.fillRect(surface.mapRGB(Constants.droneRed, Constants.droneGreen, Constants.droneBlue));
surface.blitSurface(m_screen, rect);
m_surfaces.put(entry.getKey(), surface);
}
}

public GUI(Map<String, Position> drones) throws SDLException, InterruptedException
{
SDLMain.init(SDLMain.SDL_INIT_VIDEO);
m_screen = SDLVideo.setVideoMode(Constants.environmentWidth * Constants.dotSize,
Constants.environmentHeight * Constants.dotSize, 32, SDLVideo.SDL_DOUBLEBUF | SDLVideo.SDL_HWSURFACE);
SDLVideo.wmSetCaption("Flotte de drones en 2D", null);

initSurfaces(drones);

while(m_running)
{
SDLEvent event = SDLEvent.pollEvent();

if(event instanceof SDLEvent)
{
switch (event.getType())
{
case SDLEvent.SDL_QUIT:
m_running = false;
break;
}
}

m_screen.flip();
}

freeSurfaces();
SDLMain.quit();
}

public void updateGUI(Map<String, Position> drones) throws SDLException
{
for(Map.Entry<String, Position> entry : drones.entrySet())
{
SDLRect rect = new SDLRect(entry.getValue().getX(), entry.getValue().getY());
m_surfaces.get(entry.getKey()).updateRect(rect);
m_surfaces.get(entry.getKey()).blitSurface(m_screen, rect);
}
}

void freeSurfaces() throws SDLException
{
for(Map.Entry<String, SDLSurface> entry : m_surfaces.entrySet())
{
m_surfaces.get(entry.getKey()).freeSurface();
}

m_screen.freeSurface();
}
}

最佳答案

我想我找到了答案。对于那些感兴趣的人:Jade 中的每个代理都在自己的线程中运行。因此,为了让 Display(创建 GUI 的代理)继续执行,我必须将 GUI 与他分离。我的方法是让 GUI 也成为代理。因此,从我通过调用其构造函数创建 GUI 的那一刻起,它就会启动一个新线程并允许 Display 在该指令之后继续运行。

所以答案是:让 GUI 成为代理。

public class GUI extends Agent
{
...
}

关于java - libsdl 类消耗所有 JVM(阻止代码的执行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37244021/

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