- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
继续 previous question ,我一直在寻找将主动渲染与 Java 中的文本字段相结合的最佳方式。我尝试了几种选择,使用 BufferStrategy、VolatileImage 或在标准 AWT 中覆盖 update() 和 paint(),但我最终使用了 Swing。
我在这里发布当前的状态,以防万一有人根据我的代码示例有新的见解,也许其他正在开发类似应用程序的人可能会从我的发现中受益。
目标是完成这三个壮举:
下面是在stackoverflower的大力帮助下开发的演示应用程序的代码trashgod .
两个注意事项:
1) 严格刷新动画中前一步无效的区域似乎很容易出现视觉错误,因此我放弃了。这意味着我现在每帧都重新绘制整个背景缓冲区。
2) 将 BufferedImage 绘制到屏幕上的效率在很大程度上取决于平台。 Mac 实现似乎没有正确支持硬件加速,这使得将背景图像重新绘制到输出窗口成为一项繁琐的任务,当然这取决于窗口的大小。
我在我的 2.93 GHz 双核 iMac 上发现了以下结果:
Mac 操作系统 10.5:
640 x 480:0.9 毫秒,8 - 9%
1920 x 1100:5 毫秒,35 - 40%
Windows XP:
640 x 480:0.05 毫秒,0%
1920 x 1100:0.05 毫秒,0%
图例:
屏幕大小:绘制一帧的平均时间、应用程序的 CPU 使用率。
据我所知,下面的代码是实现我的目标的最有效方式。非常欢迎任何新的见解、优化或测试结果!
问候,马蒂斯
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class SwingTest extends JPanel implements
ActionListener,
Runnable
{
private static final long serialVersionUID = 1L;
private BufferedImage backgroundBuffer;
private boolean repaintbackground = true;
private static final int initWidth = 640;
private static final int initHeight = 480;
private static final int radius = 25;
private final Timer t = new Timer(20, this);
private final Rectangle rect = new Rectangle();
private long totalTime = 0;
private int frames = 0;
private long avgTime = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new SwingTest());
}
public SwingTest() {
super(true);
this.setPreferredSize(new Dimension(initWidth, initHeight));
this.setLayout(null);
this.setOpaque(false);
this.addMouseListener(new MouseHandler());
}
@Override
public void run() {
JFrame f = new JFrame("SwingTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addComponentListener(new ResizeHandler());
/* This extra Panel with GridLayout is necessary to make sure
our content panel is properly resized with the window.*/
JPanel p = new JPanel(new GridLayout());
p.add(this);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
createBuffer();
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
long start = System.nanoTime();
super.paintComponent(g);
if (backgroundBuffer == null) createBuffer();
if (repaintbackground) {
/* Repainting the background may require complex rendering operations,
so we don't want to do this every frame.*/
repaintBackground(backgroundBuffer);
repaintbackground = false;
}
/* Repainting the pre-rendered background buffer every frame
seems unavoidable. Previous attempts to keep track of the
invalidated area and repaint only that part of the background buffer
image have failed. */
g.drawImage(backgroundBuffer, 0, 0, null);
repaintBall(g, backgroundBuffer, this.getWidth(), this.getHeight());
repaintDrawTime(g, System.nanoTime() - start);
}
void repaintBackground(BufferedImage buffer) {
Graphics2D g = buffer.createGraphics();
int width = buffer.getWidth();
int height = buffer.getHeight();
g.clearRect(0, 0, width, height);
for (int i = 0; i < 100; i++) {
g.setColor(new Color(0, 128, 0, 100));
g.drawLine(width, height, (int)(Math.random() * (width - 1)), (int)(Math.random() * (height - 1)));
}
}
void repaintBall(Graphics g, BufferedImage backBuffer, int width, int height) {
double time = 2* Math.PI * (System.currentTimeMillis() % 3300) / 3300.;
rect.setRect((int)(Math.sin(time) * width/3 + width/2 - radius), (int)(Math.cos(time) * height/3 + height/2) - radius, radius * 2, radius * 2);
g.setColor(Color.BLUE);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
void repaintDrawTime(Graphics g, long frameTime) {
if (frames == 32) {avgTime = totalTime/32; totalTime = 0; frames = 0;}
else {totalTime += frameTime; ++frames; }
g.setColor(Color.white);
String s = String.valueOf(avgTime / 1000000d + " ms");
g.drawString(s, 5, 16);
}
void createBuffer() {
int width = this.getWidth();
int height = this.getHeight();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
backgroundBuffer = gc.createCompatibleImage(width, height, Transparency.OPAQUE);
repaintbackground = true;
}
private class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
JTextField field = new JTextField("test");
Dimension d = field.getPreferredSize();
field.setBounds(e.getX(), e.getY(), d.width, d.height);
add(field);
}
}
private class ResizeHandler extends ComponentAdapter {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
System.out.println("Resized to " + getWidth() + " x " + getHeight());
createBuffer();
}
}
}
最佳答案
我有几点看法:
您改进的repaintDrawTime()
可读性很强,但它是micro-benchmark并受制于vagaries主机操作系统。我不禁想知道 XP 结果是否是该系统 limited clock resolution 的产物.我在 Windows 7 和 Ubuntu 10 上看到非常不同的结果。
如果您不使用空布局,则不需要额外的面板; JPanel
的默认布局是 FlowLayout
,f.add(this)
只是将它添加到框架默认的 BorderLayout 的中心
.
重复调用构造函数可能很耗时。
考虑更换
g.setColor(new Color(0, 128, 0, 100));
与
private static final Color color = new Color(0, 128, 0, 100);
...
g.setColor(color);
或者,一个简单的 color lookup table , 可能会有用,例如
private final Queue<Color> clut = new LinkedList<Color>();
关于java - Swing主动渲染效率或如何将主动渲染与gui widgets结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289336/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在试验 jquery-ui 并查看和克隆一些示例。在一个示例(自动完成的组合框)中,我看到一个带有 ui-widget 类的 anchor (a) 元素,它与包含的 css 文件中的 .ui-wi
在我帮助维护的代码中,我发现了多个如下所示的代码示例: Description := IfThen(Assigned(Widget), Widget.Description, 'No Widget')
这个问题在这里已经有了答案: CSS Performance Question (8 个答案) 关闭 9 年前。 这是一个很常见的情况。假设我们有一些标记: 我们可以使用 2 个选项来
这个问题在这里已经有了答案: CSS: Class name selector- name starts with (2 个答案) 关闭 5 年前。
我有一个布局,其中Row小部件中有两个子部件,这两个都是灵活的小部件。左侧的小部件包含红色和蓝色的小部件,我希望根据右侧小部件的高度增加蓝色小部件的高度(这是一个灵活的小部件,其FLEX值为7)。。例
我有一个布局,其中Row小部件中有两个子部件,这两个都是灵活的小部件。左侧的小部件包含红色和蓝色的小部件,我希望根据右侧小部件的高度增加蓝色小部件的高度(这是一个灵活的小部件,其FLEX值为7)。。例
这是一个代码: import 'package:flutter/material.dart'; import 'package:flutterapp/ui/pages/notes_home.dart'
ListView、GridView、SingleChildScrollView 等小部件是可滚动的小部件,而Container、SizedBox, Column 不是。 有没有办法检查 Widget
假设我在 Python 中有这个简单的函数: def f(gender, name): if gender == 'male': return ranking_male(nam
我不想听起来像个糟糕的新手,但是小部件是独立的应用程序吗?例如,我正在为 Android 创建一个新闻阅读器应用程序,我想要一个主屏幕小部件。我是将小部件创建为 Hook 到其他应用程序的单独项目/应
如何告诉 Tk 小部件告诉我它的子级是什么(或谁,视情况而定)?有这个命令吗? 例如,给定一个带有标签、按钮和其他装饰的 Canvas 小部件 .cvs ...如何查询 Canvas ? 最佳答案 w
我为 Android 开发了一个 APP + 主屏幕小部件。现在我更新了应用程序(增加了版本代码/名称)但是当我安装时,它不会自动替换屏幕上现有的小部件。它只是说“问题加载小部件”。 有什么想法吗??
我是小部件开发的新手..我设法构建了一个无法调整大小的小部件..我希望它像 - 用户设法根据自己调整大小......有人可以告诉我如何制作它可能吗? 谢谢 friend 。 最佳答案 AppWidge
我有一个小部件列表: List widgetList = List(); widgetList.add(WidgetA()); widgetList.add(WidgetB()); widgetLis
对不起,我的英语不好! 我开发了一个今日小部件。我需要从小部件启动 map 应用程序(例如),并且在设备锁定时不工作。在这种情况下如何检测锁定的设备并启动解锁屏幕? 问候, 最佳答案 而不是使用 UI
我正在尝试制作我的小部件的免费版本,我想向小部件添加广告,这可能吗?如果是这样怎么办? 最佳答案 小部件应用程序中的 View 仅限于: 模拟时钟 按钮 天文台 图像按钮 ImageView 进度条
我在 Android Studio 中创建了一个新项目,然后添加了一个新的小部件并在我的 Nexus 5 5.1 上运行该应用程序,它可以工作,但是当尝试将小部件添加到主屏幕时,它没有出现在小部件列表
我有一个字符串 'currentMessage' 和一个用于显示它的标签。我有一个 Toplevel 小部件,其中包含一个文本小部件,为“currentMessage”提供新值: from tkint
我尝试使用这个 link 解决我的问题 update - I figured out that there is something wrong with the setter of the pend
我是一名优秀的程序员,十分优秀!