- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个扩展 JPanel 的类(如下),该面板位于 JScrollPane 内。它监听(自身)鼠标事件,并尝试重新定位自身(拖动时)并重新缩放自身(在滚轮上)以模拟鼠标移动和缩放。该面板还负责我的应用程序的主要视觉输出。它存储在 JScrollPane 的可视区域(但在面板的图形上)呈现的 BufferedImage。图像的大小和形状保持与可视区域相匹配。
我的问题就是这样;
1) 在鼠标事件上,我遇到大量闪烁和性能下降2)如果我用自己的绘画方法覆盖paint或paintComponent方法(这对于消除闪烁和其他绘画问题是可取的),我仍然会得到相同的闪烁效果,并且从具有透明区域的加载图像中绘制图形,然后将其着色区域黑色。当我手动调用paint方法而不重写paint和paintComponent方法时,我仍然会出现闪烁,但透明区域显示正常。
我是 Swing 绘画新手,显然做错了什么,有人能给我指出正确的方向来解决这个问题吗?
谢谢
import jSim.simulation.Simulation;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.event.MouseInputListener;
public class SimPanel extends JPanel implements MouseWheelListener, MouseInputListener {
//Simulation
Simulation sim;
//Viewer
JViewport viewport;
Dimension viewSize;
BufferStrategy strat;
//Drawing
Image renderImage;
Graphics2D g2d;
boolean draw = true;
double scale = 1.0;
Object drawLock = new Object();
//Mouse events
int m_XDifference, m_YDifference;
public SimPanel(JViewport viewport) {
this.viewport = viewport;
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addMouseWheelListener(this);
//this.setup();
}
public SimPanel(Simulation sim, JViewport viewport) {
this.sim = sim;
this.viewport = viewport;
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addMouseWheelListener(this);
//this.setup();
}
//Used to initialise the buffered image once drawing begins
private void setup() {
synchronized (drawLock) {
viewSize = viewport.getExtentSize();
renderImage = new BufferedImage(viewSize.width, viewSize.height, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) renderImage.getGraphics();
}
}
// @Override
// public void paint(Graphics g)
// {
// synchronized(drawLock) {
// //super.paintComponent(g);
// paintSimulation();
// }
// }
//Paint the screen for a specific simulation
public void paintSimulation(Simulation sim) {
synchronized (drawLock) {
setSimulation(sim);
paintSimulation();
}
}
//Paint the screen with the panels simulation
public void paintSimulation() {
synchronized (drawLock) {
//if no image, then init
if (renderImage == null) {
setup();
}
//clear the screen
resetScreen();
//draw the simulation if not null, to the image
if (sim != null) {
sim.draw(this);
}
//paint the screen with the image
paintScreen();
}
}
private void resetScreen() {
Dimension newSize = viewport.getExtentSize();
if (viewSize.height != newSize.height || viewSize.width != newSize.width || renderImage == null) {
//System.out.println("Screen Size Changed: " + viewSize + " " + newSize);
viewSize = newSize;
renderImage = new BufferedImage(viewSize.width, viewSize.height, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) renderImage.getGraphics();
} else {
g2d.setBackground(Color.DARK_GRAY);
g2d.clearRect(0, 0, (int) (viewSize.width), (int) (viewSize.height));
}
}
private void paintScreen() {
Graphics g;
Graphics2D g2;
try {
//g = viewport.getGraphics();
g = this.getGraphics();
g2 = (Graphics2D) g;
if ((g != null) && (renderImage != null)) {
g2.drawImage(renderImage, (int) viewport.getViewPosition().getX(), (int) viewport.getViewPosition().getY(), null);
}
Toolkit.getDefaultToolkit().sync(); // sync the display on some systems
g.dispose();
g2.dispose();
this.revalidate();
} catch (Exception e) {
System.out.println("Graphics context error: " + e);
}
}
//Simulation makes calls to this method to draw items on the image
public void draw(BufferedImage image, int x, int y, Color colour) {
synchronized (drawLock) {
Rectangle r = viewport.getViewRect();
if (g2d != null && draw) {
Point p = new Point((int) (x * scale), (int) (y * scale));
if (r.contains(p)) {
if (scale < 1) {
Graphics2D g2 = (Graphics2D) image.getGraphics();
Image test = image.getScaledInstance((int) (image.getWidth(null) * scale), (int) (image.getHeight(null) * scale), Image.SCALE_FAST);
g2d.drawImage(test, (int) ((x * scale - r.x)), (int) ((y * scale - r.y)), null);
} else {
g2d.drawImage(image, x - r.x, y - r.y, null);
}
}
}
}
}
public void setDraw(boolean draw) {
this.draw = draw;
}
public void setSimulation(Simulation sim) {
synchronized (drawLock) {
if (!(this.sim == sim)) {
this.sim = sim;
}
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
synchronized (drawLock) {
updatePreferredSize(e.getWheelRotation(), e.getPoint());
}
}
private void updatePreferredSize(int wheelRotation, Point stablePoint) {
double scaleFactor = findScaleFactor(wheelRotation);
if (scale * scaleFactor < 1 && scale * scaleFactor > 0.05) {
scaleBy(scaleFactor);
Point offset = findOffset(stablePoint, scaleFactor);
offsetBy(offset);
this.getParent().doLayout();
}
}
private double findScaleFactor(int wheelRotation) {
double d = wheelRotation * 1.08;
return (d > 0) ? 1 / d : -d;
}
private void scaleBy(double scaleFactor) {
int w = (int) (this.getWidth() * scaleFactor);
int h = (int) (this.getHeight() * scaleFactor);
this.setPreferredSize(new Dimension(w, h));
this.scale = this.scale * scaleFactor;
}
private Point findOffset(Point stablePoint, double scaleFactor) {
int x = (int) (stablePoint.x * scaleFactor) - stablePoint.x;
int y = (int) (stablePoint.y * scaleFactor) - stablePoint.y;
return new Point(x, y);
}
private void offsetBy(Point offset) {
Point location = viewport.getViewPosition();
//this.setLocation(location.x - offset.x, location.y - offset.y);
viewport.setViewPosition(new Point(location.x + offset.x, location.y + offset.y));
}
public void mouseDragged(MouseEvent e) {
synchronized (drawLock) {
//Point p = this.getLocation();
Point p = viewport.getViewPosition();
int newX = p.x - (e.getX() - m_XDifference);
int newY = p.y - (e.getY() - m_YDifference);
//this.setLocation(newX, newY);
viewport.setViewPosition(new Point(newX, newY));
//this.getParent().doLayout();
}
}
public void mousePressed(MouseEvent e) {
synchronized (drawLock) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
m_XDifference = e.getX();
m_YDifference = e.getY();
}
}
public void mouseReleased(MouseEvent e) {
synchronized (drawLock) {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseMoved(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
最佳答案
简而言之,查找双缓冲。
更长的答案...
重写paintComponent。创建一个离屏图形对象。在那个物体上画画。将其复制到传递给 Paint 方法的图形对象。
哦,并摆脱所有同步。你不需要它。
关于Java Swing绘画和鼠标事件闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6017063/
你好,我试图在java中创建一个网格并绘制每个单元格,但我不知道我做错了什么。每个单元格都是一个 JPanel,我将每个单元格添加到 mazePanel。之后 mazePanel 添加到框架中。但只能
我正在开发一个应用程序(适用于 iOS 和 Android),其中我需要允许用户从图库中选择照片并通过在图像上绘图来圈出某些项目。一旦用户完成绘制,系统就会提示他在弹出窗口中为该项目命名。命名项目后,
当我使用绘画/绘图应用程序时,我从未注意到在按下手指和能够看到显示屏上绘制的内容之间存在延迟。但是,对于我的应用程序,接收到的初始触摸非常缓慢。第二个、第三个和第四个手势每秒接收 60 个,但第一个手
到目前为止,在我的尝试中,我能够在普通图像上画线,比如创建我自己的 CGRect 大小的普通上下文并在其上画线。我看到的所有教程都是如何在创建的 x*y 大小的图像上下文上绘制。但是我想在已经存在的图
我在 android 中开发,我必须为 android 做一个 Paint。 我正在使用下面的代码,当我执行代码时,绘制工作正常,但是,似乎有 2 个表面要绘制,当你绘制一个时,另一个消失了。 我一直
我使用QGraphicsView、QGraphicsScene 和QGraphicsItem 来绘制一些图表。我已经实现了用于绘制文本(图表的值)的 QGraphicsItem::paint 函数,但
Canvas { id: canvas onPaint: { if (personalInfo.count === 0) { return
好的,这就是问题所在:在 C# 表单中,我创建了一个新的私有(private) void: private void NewBtn(string Name, int x, int y) 它的目的是创建
任何人都可以详细建议我如何使用 QDirectPainter 类直接在帧缓冲区上绘制小部件。如果您能提供一个工作示例,我会更有帮助。 最佳答案 QDirectPainter 不会也不能绘制任何东西。它
我正在使用 cardLayout 更改“ View ”(此类有一个 JFrame 变量)。当用户点击新游戏按钮时,会发生这种情况: public class Views extends JFrame
目标: 我想在 pdf 位图上绘制/书写/绘画并将它们保存在一起,以便我可以通过电子邮件发送给他们。 详细信息: 我有多个 Pdf包含 5-20 的文件每个页面,现在我正在从 pdfs 中提取位图并将
我最近开始学习Qt。 我不太清楚如何使用 QPainter 类进行绘画。假设我只想在窗口中放置几个点: class PointDrawer: public QWidget { Q_OBJE
我必须为我的 CS 类(class)期末项目创建一个带有 GUI 的 connect 5 游戏。我在最后一个项目中使用 Graphics2D - 迷宫 - 使用 Graphics2D 是一场噩梦。 c
我已经删除了 CS_HREDRAW、CS_VREDRAW。 消息 WM_PAINT 和 WM_ERASEBKGND 什么都不做,但是一旦窗口重新调整大小,它就会用背景颜色重新绘制它。有什么办法可以完全
首先大家好! 我遇到了一个大问题:我需要构建一个程序,该程序包括构建一个包含 5 个方 block 和一个按钮的 java swing 界面。按钮功能是在方 block 内画一个圆。我有这段代码,但我
我正在尝试从显示幻灯片的 Qt fluidLauncher 演示中运行稍微修改过的代码。代码贴在下面。当处理 paintEvent 时,屏幕上会显示一个黑色矩形,因为导入的图像大小为 0。 单步执行Q
我有以下代码来激活/停用橡皮擦: public PorterDuffXfermode clear = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
我正在绘制一个未被清除的 Canvas ,并使 Canvas 随着时间的推移逐渐变成纯色,或者在 alpha 中逐渐消失以显示后面的图层。 我的第一直觉是简单地用每帧的低 alpha 在绘图上填充一个
我正在尝试在每幅画上检测并绘制一个矩形轮廓,例如这张图片: 我遵循了一些指南并执行了以下操作: 灰度转换 应用中值模糊 锐化图像 应用自适应阈值 应用形态梯度 寻找轮廓 绘制轮廓 得到如下结果: 我知
创建 JFrame、在其上添加 JPanel 并在 JPanel 上绘制矩形的类 class Frame { JFrame frame; myPanel panel; void draw() {
我是一名优秀的程序员,十分优秀!