有没有人曾尝试使用Swing构建一个合适的多缓冲渲染环境,在此环境中可以添加Swing用户界面元素?
在这种情况下,我有一个动画红色矩形绘制在背景上.背景不需要每帧更新,因此我将其渲染到BufferedImage上,并仅重绘清除矩形的先前位置所需的部分.请参见下面的完整代码,这扩展了以前的线程通过@trashgod给出的例子,在这里.
到现在为止还挺好; 流畅的动画,低CPU使用率,无闪烁.
然后我将JTextField添加到Jpanel(通过单击屏幕上的任何位置),并通过在文本框内单击来关注它.现在,清除矩形的先前位置会在每个光标闪烁时失败,请参见下图.
我很好奇是否有人知道为什么会发生这种情况(Swing不是线程安全的?图像是异步绘制的?)以及寻找可能解决方案的方向.
这是在Mac OS 10.5,Java 1.6上
JPanel重绘失败了http://www.arttech.nl/javaredrawerror.png
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class NewTest extends JPanel implements
MouseListener,
ActionListener,
ComponentListener,
Runnable
{
JFrame f;
Insets insets;
private Timer t = new Timer(20, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试用 Java 制作一个简单的 2D 游戏。
据我所知,我的游戏应该由两个线程组成:“事件调度线程”(用于 GUI 操作)和“游戏线程”(用于游戏循环)。
我创建了一个大纲,但找不到放置游戏循环的位置。
简而言之,我正在尝试在不冻结 UI 线程的情况下创建游戏循环。
如果您能提供有关我做错的事情的任何信息,我将不胜感激。
这是我的游戏循环(您也可以提供提示以创建更好的游戏循环):
while(true) {
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Forge …
Run Code Online (Sandbox Code Playgroud) 我已经使这个程序能够绘制一个使用这两个类在屏幕上弹跳的小球的实例
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;
public class move extends JPanel implements ActionListener
{
Timer t = new Timer(7, this);
int x = 10, y = 10, velX = 7, velY = 7;
public void paintComponent(Graphics g, Graphics h)
{
super.paintComponent(h);
super.paintComponent(g);
System.out.println(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, 40, 40);
g2.fill(circle);
t.start();
}
public void actionPerformed(ActionEvent e) {
if(x<0 || x > getWidth())
{
velX …
Run Code Online (Sandbox Code Playgroud) 我有这个奇怪的问题,我不确定是什么导致它.有时问题甚至不存在.根据我的猜测,这是一个Java内存问题或某种线程问题.
我有一个Ship
和船射击Bullets
如果我按住Space钥匙,船射击子弹.我的子弹每隔200毫秒开始发射一次.有时他们射击很好,并以相同的速度移动!其他时候,他们射击他们以不同的速度移动.什么可能导致这个?
package JGame.Actions;
import JGame.GameObject.GameObject;
import javax.swing.AbstractAction;
public class MoveAction extends Action implements Runnable{
protected GameObject obj;
protected int endX = 0, endY = 0;
protected int moveAmount = 0;
protected Thread thread;
public void moveToY(GameObject obj, int y, int amount, AbstractAction complete){
this.obj = obj;
this.endY = y;
this.moveAmount = amount;
this.complete = complete;
thread = new Thread(this);
thread.start();
}
public void run(){
try{
boolean run = true;
while(run){
int objY = …
Run Code Online (Sandbox Code Playgroud) 我想用最简单的方法制作pacman开/关嘴动画.这是我最近的代码:问题是,什么都没发生?
package ordner;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PacMan implements ActionListener {
private JFrame frame;
private DrawPanel panel;
private void initGui() {
frame = new JFrame("Pacman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new DrawPanel();
frame.add(panel);
panel.setBackground(Color.BLACK);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
PacMan pm = new PacMan();
pm.initGui();
}
@Override
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的画板:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawPanel extends JPanel …
Run Code Online (Sandbox Code Playgroud)