- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是图形和 japplet 的新手,我制作了一个横跨屏幕的矩形。但出于某种原因,它只是画了一条线,并没有在矩形移动后删除它的旧实例。
主要内容:
public class Main extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;
private static int width = 900;
private static int height = 600;
public static int fps = 60;
public Thread thread = new Thread(this);
public static Ailoid ailoid = new Ailoid();
public void init() {
setSize(width, height);
setBackground(Color.white);
ailoid.setLocation(new Location(100, 100));
AlienManager.registerAlien(ailoid);
}
public void paint(Graphics g) {
g.setColor(Color.green);
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
g.fillRect(x, y, 10, 20);
}
}
// Thread start
@Override
public void start() {
thread.start();
}
// Thread stop
@SuppressWarnings("deprecation")
@Override
public void destroy() {
thread.stop();
}
@Override
public void run() {
while (true) {
Updater.run();
repaint();
try {
// 1000 divided by fps to get frames per millisecond
Thread.sleep(1000 / fps);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
更新者:
public class Updater {
public static void run() {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
alien.setLocation(new Location(x, y));
}
}
}
为什么它不删除旧图形?谢谢!
最佳答案
您的主要问题是您的paint(...)
方法没有调用super 方法,即允许组件重绘其内容的方法:
public void paint(Graphics g) {
super.paint(g);
//....
话虽如此,您远远最好不要在顶层窗口中绘制,而是在小程序显示的 JPanel 的 paintComponent
方法中绘制。如果您执行此更正,则执行相同的操作——调用 super 方法。
class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//....
顺便说一句,这段代码:
public class Updater {
public static void run() {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
alien.setLocation(new Location(x, y));
}
}
}
看起来它并没有那么大的影响力。事实上,根据此代码,您的外星人应该保持完全静止。
编辑
此外,这是永远不要的代码,您不应该在您的应用程序中使用的代码:
@SuppressWarnings("deprecation")
@Override
public void destroy() {
thread.stop();
}
线程的 stop()
方法被弃用并且永远不应该被调用是有原因的。如果您对原因感到好奇,请查看 API。
关于移动后,Java JApplet 不会删除旧矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24706812/
我正在尝试在 上收听 Java 中的 MouseEvents。小程序 在一个 JFrame 中。我的程序实际上做了更多的事情,但我已经为你简化了: import java.awt.*; import
所以我正在创建一个 JApplet 游戏,并将用户信息保存到 APPDATA 中的 .txt 文件中。有没有更安全的方法来保存他们的信息,不允许他们编辑信息进行作弊? 最佳答案 基本上,防止作弊的唯一
我完全不知道我的时钟程序出了什么问题。 我的程序应该接受用户输入几小时和几分钟。然后,用户的输入会将时针和分针设置到弹出窗口中绘制的圆形时钟上的适当位置。 我的程序似乎没有接受用户输入,而是从Cloc
所以我正在尝试研究简单的动画和虚拟物理等等。我正在尝试制作一个球的动画,使其随着时间的推移慢慢变大。我这里的代码与我在《Java For Dummies》书中的代码几乎完全相同,除了以下几件事:去掉小
我目前正在开发一个学校项目。我们必须制作一个Applet,我选择了JApplet。由于某种原因,我用来显示特定字符串的面板将不会显示。这里可能有什么问题?请指出我正确的方向。另外,我看了一些教程,
我知道这个问题之前已经被问过很多次了,但是每次我访问问题线程并执行提出的解决方案时,它仍然对我不起作用。 所以在我造成任何困惑之前,我遇到的问题是我绘制的屏幕不断闪烁,这是因为每次我绘制屏幕时,我都会
我最近一直在摆弄 awt/swing,试图学习如何做小程序。 我写了以下简短的JApplet example code ,这从表面上看似乎有效。 尝试退出时出现的问题: 按 ESC 不会执行任何操作,
我使用 JApplet 创建了一个简单的 Applet,一切似乎都工作正常,但是当我在小程序中创建名为 ChatUser 的用户定义类的对象时,我收到此错误:- SEVERE: java.lang.C
我花了很多时间试图理解这一点。我创建了一个 JApplet,它在循环中使用 Thread.sleep() 来为小程序设置动画。但当我尝试运行小程序时,屏幕保持空白。当我在单独的线程中绘制动画并调用重绘
我是 Japplets 新手..我想知道我是否有以下内容... classA A = new classA(); //launch Japplet here 并且classA扩展了Japp
我对 JApplet 非常陌生,这就是我现在所做的。 package myform; import java.sql.Connection; import java.sql.DriverManager
我有一个动画类,它只是绘制两个圆和一条线(没有主方法),在另一个类中,我有一个主方法,该方法将动画类作为对象传递,并且应该显示我绘制的两个圆,但是它不显示,它只显示窗口,没有我所做的圆圈或线条,如果我
第一次海报和明显的 n00b java 学生。我一直在研究这个问题,过去两天在互联网上搜索,但我尝试的任何方法都没有结果。这本来是一个使用JFrame的程序,我需要将其转换为Applet。该代码编译良
Java Applet嵌入到JSP中时是否会运行类中的所有代码?这可能听起来很愚蠢,但是当我运行独立小程序时,我可以设法将图像写入路径。但是当我将小程序嵌入到 JSP 页面时,它出现了,但似乎编写图像
我在使用 JApplet 时遇到问题。代码工作得很好,但是当我将其从 JFrame 转换为 JApplet 时,渲染部分停止正常工作。基本上我想做的是简单化的绘图应用程序。当启动小程序时,有一半的时间
我已经制作了一个 Applet 的蓝图,其功能尚未插入。因此,我在类中实例化了各种类型的数组,然后在 init() 函数中进一步实例化了它们的类型。但后来,我在构造函数 GPACalculator 中
import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public
我想构建一个 JApplet 应用程序,包含 5 个页面。一页中的给定数据将被输入到下一页。根据给定的数据,我必须在下一页显示不同的选项。 我完全不熟悉 applet,之前使用过 Web 应用程序。我
我尝试从一个类中创建两个移动对象。我想控制它们,但 JApplet 不打开线程。 我的代码: import java.awt.Graphics; import javax.swing.JApplet;
Applet 以前可以用,现在不行了。我对 GUI 的小程序不是很熟悉。这是 GUI 代码: import javax.swing.*; import java.awt.*; import java.
我是一名优秀的程序员,十分优秀!