- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是新来的。希望您能提供帮助。
问题:在 JFrame 上显示动画时出现问题。似乎我想念/不太了解 Java 的图形是如何工作的。
全局理念:假设我想制作游戏/电影/剪辑。为此,我需要这个(不是)简单的动画开始工作。
这个问题的一个例子:我得到了 Screen 类,它具有屏幕内容 - JFrame 的声明,设置其配置(大小,关闭操作等),然后创建 Box 类的对象,以显示在框架上。请检查这张图片/类图(希望我写得正确):ClassesDiagram
现在,Box 类扩展了 JPanel。我从 JPanel 继承了 Paint() 方法并覆盖它,绘制框。
在 Screen 类中,在我创建了两个 Boxes 之后,我将它们添加到 JFrame。接下来,启动一个循环 while(true),并通过让线程 hibernate 该时间来每 200 毫秒更新一次框的位置。(在这种情况下,只是简单的 x++ 或 y++ 取决于哪个框,box1 或 box2)。
主要问题 1): 程序运行并显示 JFrame,但在 JFrame 上它仅显示最后添加的对象/组件- Box。它没有显示另一个。为什么?
我找到了一个话题,How to add multiple components to a JFrame? ,并尝试了 jjnguy 2010 年 11 月 15 日 17:02 投票最多的帖子给出的提示。出于某种奇怪的原因,第一个和第二个提示都不适合我。
主要问题 2): 据我了解,我需要布局管理器。如果我只想在框架上的特定 X、Y 处绘制 (),为什么我需要它?
找到其他帖子(找不到了)+Oracle关于布局的指南,建议我需要考虑使用setLayout(null);我尝试这样做,但又出现了问题。
主要问题 3): JFrame 出现,它只显示 1 个框(绿色框不会出现,无论你做什么。不知道为什么),当它移动时 -它从另一侧被删除。这里:Box Movement
提前感谢您提供的任何帮助、提示和解释!希望帖子清晰、有条理且美观。
public class Screen {
public static void main(String[] args) {
new Screen();
}
private JFrame window;
public Screen() {
window = new JFrame("Multiply components panel");
//window.setLayout(null);
window.setSize(200, 200);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Box b1 = new Box(10,10, "box1");
//b1.setBounds(10, 10, 50, 50);
Box b2 = new Box(100,100, "box2");
//b2.setBounds(100, 100, 50, 50);
window.add(b1);
//window.add(b2);
window.setVisible(true);
while (true){
b1.update();
b2.update();
try {
Thread.sleep(200);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
public class Box extends JPanel{
int x, y, w, h;
private String name;
public Box(int x, int y, String name) {
this.x = x;
this.y = y;
this.w = 100;
this.h = 100;
this.name=name;
}
public void paint(Graphics g){
System.out.println(this.name.equalsIgnoreCase("box1"));
if(this.name.equalsIgnoreCase("box1")){
g.setColor(Color.BLACK);
g.fillRect(x, y, w, h);
}else{
g.setColor(Color.GREEN);
g.fillRect(x, y, w, h);
}
}
public void update() {
if(this.name.equalsIgnoreCase("box1"))
x++;
else
y++;
//this.setBounds(x, y, w, h);
System.out.println("Current "+name+": X: "+x+", Y: "+y+", W: "+w+", H: "+h);
repaint();
}
}
最佳答案
Main Problem 1): The program runs, and shows the JFrame, but on the JFrame it shows only the last added object/component- Box. It doesn't show the other one. Why?
你做的 window.add(b1);
,默认
window.add(b2);JFrame
有BorderLayout
因此,当您执行 add(..)
时,您将替换最后添加的框.
Main Problem 2): As far as I understand, i need layout manager. Why do I need it, if I just want to paint() at specific X,Y on the frame?
如果您使用 JPanel
作为游戏中的对象,这将是唯一一次使用 setLayout(null)
因为我们想要完全控制 JPanel
放置。
Main Problem 3): The JFrame shows up, it shows only 1 box(the green one wont show up, whatever you will do. Don't know why) and when it DOES move- it gets erased from the other side. Here: Box Movement
因为你你这样做g.fillRect(x,y,w,h)
,应该是g.fillRect(0,0,w,h)
其他问题:
1) 我认为你遇到的一个主要问题是:
public class Box extends JPanel{
...
public void paint(Graphics g){
...
}
}
你应该覆盖 paintComponent
的 JPanel
,记得调用super.paintComponent(g)
作为覆盖方法中的第一次调用。
3) 你应该覆盖 getPreferredSize
的 JPanel
并返回与 JPanel
的图像或内容匹配的正确尺寸
4) 不要调用 setSize
在 JFrame
使用正确 Layoutmanager
和/或覆盖 getPreferredSize
必要的容器比简单地调用 pack()
在 JFrame
在将其设置为可见之前。
5) 正如@MadProgrammer 所说,阅读 Concurrency in Swing但基本上所有 Swing 组件都应该通过 SwingUtilities.inokeXXX
在 EDT 上创建/操作 block 。
6) 这样做绝对不好:
while (true){
b1.update();
b2.update();
try {
Thread.sleep(200);
} catch (Exception e) {
// TODO: handle exception
}
}
因为您不仅要创建连续循环,还要调用 Thread.sleep
在您创建 GUI 的线程上,因此似乎会卡住。看看How to use Swing Timers , 另见 this关于上述主题的类似问题。
这是实现了上述修复的代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Screen {
private JFrame window;
public static void main(String[] args) {
//creat UI on EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Screen();
}
});
}
public Screen() {
window = new JFrame("Multiply components panel") {
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
window.setLayout(null);//only reason this is warrented is because its a gmae using JPanels as game objects thus we need full control over its placing
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//changed from DISPOSE to EXIT so Timers will be exited too
final Box b1 = new Box(10, 10, 50, 50, "box1");
final Box b2 = new Box(100, 100, 50, 50, "box2");
window.add(b1);
window.add(b2);
window.pack();
window.setVisible(true);
Timer timer = new Timer(200, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
b1.update();
b2.update();
}
});
timer.setInitialDelay(0);
timer.start();
}
}
class Box extends JPanel {
int x, y, w, h;
private String name;
public Box(int x, int y, int w, int h, String name) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.name = name;
setBounds(x, y, w, h);//let the Box class handle setting the bounds more elegant OOP
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(this.name.equalsIgnoreCase("box1"));
if (this.name.equalsIgnoreCase("box1")) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, w, h);
} else {
g.setColor(Color.GREEN);
g.fillRect(0, 0, w, h);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(w, h);
}
public void update() {
if (this.name.equalsIgnoreCase("box1")) {
x++;
} else {
y++;
}
this.setBounds(x, y, w, h);//set the new bounds so box may be updated
System.out.println("Current " + name + ": X: " + x + ", Y: " + y + ", W: " + w + ", H: " + h);
revalidate();
repaint();
}
}
最后看看我的教程/代码片段 Game Development Loop, Logic and Collision detection Java Swing 2D .
它拥有开始一个简单的 2D 游戏所需的一切,例如游戏循环、逻辑、像素碰撞检测、动画(即在多个 Sprite 之间交换以创建 Sprite 集的动画)等等,但根本的区别在于使用对象作为游戏实体,即一个类将保存要绘制的对象所需的所有信息,IMO 这就是游戏应该如何完成的,事情可能会变得图形化,并且有许多 JPanel 想知道屏幕肯定会降低整体 FPS
关于Java 游戏开发 : Graphics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17373979/
我需要一些说明。我可以直接写入 /dev/port 以直接访问并行端口并且它工作正常(我可以打开插入端口连接器的 LED)。但是,我想我可以用 /dev/mem 做同样的事情? (http://tld
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我使用 Visual C++ 和 Win32 API 学习了 Windows 编程。如今,似乎大多数应用程序都是使用 C# 在 .NET 中开发的。我知道大多数时候 native 代码和托管代码之间没
请耐心等待。我正在制作一个 java 控制台,类似于此处找到的 DragonConsole https://code.google.com/p/dragonconsole/ 。一切都按计划进行,但我想
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭5年前。 Improve this que
Django 的开发服务器表现得很奇怪。访问它的浏览器在加载时卡住,任何退出它的尝试都不起作用。当我点击 control c看似相当,但实际上仍在运行。让它退出的唯一方法是重新启动我的电脑,这很令人沮
我正在使用 Flash Develop,并且创建了一个 ActionScript 3.0 项目。它启动并读取一个 xml 文件,其中包含图像的 url。我已将 url 保留在与 swf 相同的文件夹中
是否可以根据其 website 上提供的规范开发 AUTOSAR BSW 堆栈(例如用于 CAN 通信)?不购买任何昂贵的供应商工具?可以遵循哪些步骤?我被要求探索这种可能性。 最佳答案 是和否。工具
有人知道如何用音频文件的内容覆盖 iPhone 麦克风吗? 想象一个场景,您正在通话,并且想要播放一些简短的音频让其他人听到。 因此,有必要将麦克风(硬件)置于保持状态,并使用委托(delegate)
我遇到了这个问题,我的应用程序出现 EXC_BAD_ACCESS 错误并卡住/停止。我使用模拟器的“向左旋转”和“向右旋转”选项来模拟方向变化行为。导致此错误的可能原因有哪些?由于我没有获得有关错误的
我有超过 1 台 Mac,我想在所有这些 Mac 上进行开发。我知道我需要在每台机器上同步我的手机,但这是我遇到的最小的问题。看起来我无法在手机上运行应用程序,除了在其中之一上开发的应用程序。 是否有
在手机上测试时,我的应用程序在特定点崩溃。控制台显示此消息 Tue Jan 27 15:47:14 unknown SpringBoard[22] : Application com.myprof.
我有一个案例,我从服务器获取信息。我的应用程序有一个选项卡栏和导航按钮。我希望应用程序显示进度指示器并禁用所有其他控件,以便用户在从服务器提取数据时无法跳转。我怎样才能实现这个目标? 我想到的一种方法
有时,当我尝试“构建”/编译下载的源代码时,我会收到以下警告: ld: warning: directory '/Volumes/Skiiing2/CD/ViewBased/Unknown Path/
我无法在 Apple 文档中找到关于开发和分发配置之间差异的明确解释。我目前正在使用开发配置在我的 iPhone 上进行开发和测试。我打算将该应用程序分发到我的 Beta 测试中,我想知道: 我需要使
我在使用 SharePoint 时遇到的最大挑战之一是它不能很好地适应典型的项目环境,其中至少包含开发和生产环境。我遇到的最多的问题是内容和列表是如此紧密地耦合在一起,以至于如果不在生产环境中执行内容
我失败了fist step让 Eclipse(对我来说是全新的)为 ARM 开发做好准备。 我在 Windows 10 中安装了 Eclipse。我想我应该安装 xpm,但我不知道在哪里输入此命令:
首先,我告诉你-我是编码新手 我正在使用vs代码来学习c++,它不会产生像dev c++或codeblocks这样的调试器。我看了一些视频,其中我们必须编辑json文件,这对于初学者来说非常复杂。有人
我失败了fist step让 Eclipse(对我来说是全新的)为 ARM 开发做好准备。 我在 Windows 10 中安装了 Eclipse。我想我应该安装 xpm,但我不知道在哪里输入此命令:
我开发了一个 Ionic 应用程序(iOS 和 Android 的混合)。我有 Xcode 8.3.3 并购买了一年的 Apple Developer Program 订阅。 我不想测试我的应用并将其
我是一名优秀的程序员,十分优秀!