- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用图形库 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/
我有一个简单的 HATEOAS 使用 构建的提供程序 Spring 这为我提供了以下资源: { "_links" : { "self" : { "href" : "http:/
这里是 Clojure 初学者,不确定问题中的术语是否正确。 我正在使用 clj-webdriver 出租车 API 构建网络抓取工具。它需要从多个站点抓取数据。以下实际上不是项目中的代码,但我已经对
我使用pthread lib 2.8,操作系统内核是arm上的Linux 2.6.37。在我的程序中,线程 A 使用 pthread 接口(interface)将调度优先级设置为 sched_get_
我有一个大约 400MB 的二进制文件,我想将其转换为 CSV 格式。输出的 CSV 文件将约为 1GB(根据我的计算)。 我读取二进制文件并将其存储在一个结构数组中(其他处理也需要),当用户想要将其
我在编写我的专业应用程序时遇到一个串口线程问题。我有cpu消耗。当我在我的项目中添加 SerialCtrl.h(来自项目 SerialCtrl http://www.codeproject.com/A
总结:似乎 c 代码的 RAM 消耗取决于变量排序。有没有办法自动优化? 更长的版本:在这里,我粘贴了两个版本的代码,它们仅在变量排序方面有所不同。 版本 1: static unsigned lon
我有一个处理图像编辑(裁剪和调整大小)的 Windows 应用程序项目。不幸的是,这些图像处理会消耗大量内存和 CPU 资源(很容易达到 600MB 或 50% cpu),而且它只是裁剪和调整大小 2
我创建了一个实例化类 10 亿次的循环,并且非常惊讶地看到它在 0 毫秒内运行并且根据 Windows 任务管理器没有消耗 CPU 时间。 正如您从下面的代码中看到的那样,我显然没有对默认构造函数执行
我们有以下用户名验证规则: 用户名可以包含字母数字字符 用户名可以有下划线、连字符或句号 现在假设用户名是 ASCII 用户名不能以句点开头或结尾 用户名不能开始、结束或有任何空格 我们有以下相同的正
如何获取 C# 中所有进程的列表,然后获取每个进程的当前内存和 CPU 消耗? 非常感谢示例代码。 最佳答案 Process class有一个 GetProcesses 方法,可以让您枚举正在运行的进
如何从 linux 2.6.32 机器上的源代码中限制 C 程序的物理内存消耗? 我需要确定系统使用的页面替换算法的类型。 问题是,如果不限制一个进程在内存中可以拥有的页面数量,就很难分析页面错误的模
我正在编写一个 Linux 应用程序,它观察其他应用程序并跟踪资源消耗。我计划使用 Java,但编程语言对我来说并不重要。目标很重要,所以我可以切换到另一种技术或使用模块。我的应用程序将任何选定的第三
我有一个图标,旁边有一个复选框,包含在一个面板中。面板有悬停效果,点击面板时想选中框。 我想使用或阻止复选框的所有事件,仅以编程方式选择它。我希望该框在屏幕上显示为“已启用”,而“在幕后”几乎不起作用
我正在使用服务 REST,它当前有 5025 条记录,但当我使用该服务时,只出现 1,000 条记录。我可以做什么来完全消耗所有记录? 这是我的代码示例: $http({ method:
我已经搜索了几个小时但没有成功,是否可以跟踪 Blob 容器上 SAS 凭据的消耗情况? 我将向几个客户提供 SAS 凭证,并且我希望能够跟踪他们的 SAS 使用情况(操作次数、带宽使用情况...)
我创建了具有 1.75 GB RAM 的 B1 应用服务计划。我还创建了一个应用程序服务并向其部署了 docker 镜像。现在我停止了 docker 镜像,它的状态已停止,并且它是该应用程序服务计划中
与我们的合作伙伴一起为我们的客户(一家售后汽车零部件零售商)开发一个项目,他们利用 AR 和 VR 做了很多很酷的事情。我们的想法是使用 Hololens 并尝试帮助我们的客户在其大型仓库中进行物流操
这是一个简短的 Haskell 程序,可以生成 440 Hz 的声音。它使用pulseaudio作为音频后端。 import GHC.Float import Control.Arrow import
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
tab.setOnCloseRequest(e -> { if (getEditorForTextArea(getSelectedTextArea()) != null
我是一名优秀的程序员,十分优秀!