- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这个问题之前已经被问过很多次了,但是每次我访问问题线程并执行提出的解决方案时,它仍然对我不起作用。
所以在我造成任何困惑之前,我遇到的问题是我绘制的屏幕不断闪烁,这是因为每次我绘制屏幕时,我都会用一个完全白色填充的矩形清除屏幕,我绘制的东西矩形将会闪烁。
这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import javax.swing.Timer.*;
/**
*
* beschrijving
*
* @version 1.0 van 16-6-2016
* @author
*/
public class shadows3 extends JApplet implements ActionListener{
// Begin variabelen
int[] loc = new int[2];
int[][] wall = new int[10][8];
// Einde variabelen
public void init() {
Container cp = getContentPane();
cp.setLayout(null);
cp.setBounds(0, 0, 600, 600);
// Begin componenten
for (int a=0; a<10; a++) {
int tempx = (int) (Math.random()*20)*30;
int tempy = (int) (Math.random()*20)*30;
wall[a][0] = tempx;
wall[a][1] = tempy;
wall[a][2] = tempx+30;
wall[a][3] = tempy;
wall[a][4] = tempx+30;
wall[a][5] = tempy+30;
wall[a][6] = tempx;
wall[a][7] = tempy+30;
} // end of for
loc[0] = 300;
loc[1] = 300;
Timer step = new Timer(20, this);
step.start();
// Einde componenten
} // end of init
private int length(int x1, int y1, int x2, int y2) {
double distance = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
return (int) distance;
}
// Begin eventmethoden
public void paint (Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0,0,600,600);
int[] xpoints = new int[8];
int[] ypoints = new int[8];
int[] list = new int[3];
for (int a=0; a<5; a++) {
for (int b=0; b<4; b++) {
if (length(wall[a][b*2],wall[a][b*2+1],loc[0],loc[1])==Math.max(Math.max(length(wall[a][0],wall[a][1],loc[0],loc[1]),
length(wall[a][2],wall[a][3],loc[0],loc[1])),
Math.max(length(wall[a][4],wall[a][5],loc[0],loc[1]),
length(wall[a][6],wall[a][7],loc[0],loc[1])))) {
int temp = b;
for (int c=0; c<3; c++) {
temp += 1;
if (temp == 4) {
temp = 0;
} // end of if
list[c] = temp;
} // end of for
} // end of if
} // end of for
xpoints[0] = wall[a][list[0]*2 ];
ypoints[0] = wall[a][list[0]*2+1];
xpoints[1] = wall[a][list[1]*2 ];
ypoints[1] = wall[a][list[1]*2+1];
xpoints[2] = wall[a][list[2]*2 ];
ypoints[2] = wall[a][list[2]*2+1];
xpoints[3] = wall[a][list[2]*2 ]+(wall[a][list[2]*2 ]-loc[0])*10000;
ypoints[3] = wall[a][list[2]*2+1]+(wall[a][list[2]*2+1]-loc[1])*10000;
xpoints[4] = wall[a][list[0]*2 ]+(wall[a][list[0]*2 ]-loc[0])*10000;
ypoints[4] = wall[a][list[0]*2+1]+(wall[a][list[0]*2+1]-loc[1])*10000;
g.setColor(Color.BLACK);
g.fillPolygon(xpoints, ypoints, 5);
//g.fillRect(wall[a][0],wall[a][1],30,30);
} // end of for
}
//@Override
public void actionPerformed(ActionEvent e){
loc[0] += 4;
loc[1] += 2;
repaint();
}
// Einde eventmethoden
} // end of class shadows3
如果你想知道我正在尝试创建什么,它是某种实时阴影引擎,它相当慢,但它只是一个有趣的项目,尽管对于我的许多项目来说,屏幕闪烁是一个真正的问题。
非常感谢!
最佳答案
因为你应该是migrating对于无插件环境,请考虑 hybrid通过 java-web-start 部署。下面的示例在 JPanel
中显示您的内容,默认情况下是双缓冲的。特别是,
调用super.paintComponent()
以避免渲染伪影。
覆盖getPreferredSize()
确定初始大小。
实例化Random
使用恒定的种子以便于调试。
注释中的applet
标签可以轻松测试,如图here .
测试:
$ javac ShadowApplet.java ; appletviewer ShadowApplet.java
代码:
//<applet code="ShadowApplet.class" width=600 height=600></applet>
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* @see https://stackoverflow.com/a/37958844/230513
*/
public class ShadowApplet extends JApplet {
private static final Random R = new Random(42);
@Override
public void init() {
EventQueue.invokeLater(() -> {
add(new ShadowPanel());
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new ShadowApplet()::display);
}
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ShadowPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class ShadowPanel extends JPanel implements ActionListener {
int[] loc = new int[2];
int[][] wall = new int[10][8];
public ShadowPanel() {
this.setLayout(null);
this.setBackground(Color.WHITE);
for (int a = 0; a < 10; a++) {
int tempx = R.nextInt(20) * 30;
int tempy = R.nextInt(20) * 30;
wall[a][0] = tempx;
wall[a][1] = tempy;
wall[a][2] = tempx + 30;
wall[a][3] = tempy;
wall[a][4] = tempx + 30;
wall[a][5] = tempy + 30;
wall[a][6] = tempx;
wall[a][7] = tempy + 30;
}
loc[0] = 300;
loc[1] = 300;
Timer step = new Timer(20, this);
step.start();
}
private int length(int x1, int y1, int x2, int y2) {
double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
return (int) distance;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int[] xpoints = new int[8];
int[] ypoints = new int[8];
int[] list = new int[3];
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 4; b++) {
if (length(wall[a][b * 2], wall[a][b * 2 + 1], loc[0],
loc[1]) == Math.max(Math.max(length(wall[a][0], wall[a][1], loc[0], loc[1]),
length(wall[a][2], wall[a][3], loc[0], loc[1])),
Math.max(length(wall[a][4], wall[a][5], loc[0], loc[1]),
length(wall[a][6], wall[a][7], loc[0], loc[1])))) {
int temp = b;
for (int c = 0; c < 3; c++) {
temp += 1;
if (temp == 4) {
temp = 0;
}
list[c] = temp;
}
}
}
xpoints[0] = wall[a][list[0] * 2];
ypoints[0] = wall[a][list[0] * 2 + 1];
xpoints[1] = wall[a][list[1] * 2];
ypoints[1] = wall[a][list[1] * 2 + 1];
xpoints[2] = wall[a][list[2] * 2];
ypoints[2] = wall[a][list[2] * 2 + 1];
xpoints[3] = wall[a][list[2] * 2] + (wall[a][list[2] * 2] - loc[0]) * 10000;
ypoints[3] = wall[a][list[2] * 2 + 1] + (wall[a][list[2] * 2 + 1] - loc[1]) * 10000;
xpoints[4] = wall[a][list[0] * 2] + (wall[a][list[0] * 2] - loc[0]) * 10000;
ypoints[4] = wall[a][list[0] * 2 + 1] + (wall[a][list[0] * 2 + 1] - loc[1]) * 10000;
g.setColor(Color.BLACK);
g.fillPolygon(xpoints, ypoints, 5);
g.fillRect(wall[a][0], wall[a][1], 30, 30);
}
}
@Override
public void actionPerformed(ActionEvent e) {
loc[0] += 4;
loc[1] += 2;
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
}
}
关于java - JApplet 中图形闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954011/
我正在尝试在 上收听 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.
我是一名优秀的程序员,十分优秀!