gpt4 book ai didi

Java applet 仅部分绘制

转载 作者:行者123 更新时间:2023-12-01 15:04:11 24 4
gpt4 key购买 nike

我是 Java 小程序编程的新手,所以请原谅,如果这是一个非常基本的问题,但我已经在 google 上进行了广泛的搜索,并且只找到了半相关的问题和解决方案。

我正在编写一些几何算法的简单演示,当我repaint()时,只有一些图形基元被渲染到屏幕上。每次我的小程序重新绘制时,都会绘制看似随机的线条和椭圆子集。唯一的模式是渲染的图元总是从绘图的开头开始。即,有时它会绘制基元 0-2,有时是 0-5,有时是整个批处理。

我想指出的是,据我所知,这不是可以通过双缓冲解决的经典“闪烁”。据我了解,闪烁是指在完成渲染之前您可以在短时间内看到部分渲染的小程序。然而,就我而言,如果它没有完成渲染,它永远不会完成,除非我再次redraw()并且幸运。我尝试过双缓冲:

public void update(Graphics g) {
Graphics offgc;
Image offscreen = null;
Dimension d = size();

// create the offscreen buffer and associated Graphics
offscreen = createImage(d.width, d.height);
offgc = offscreen.getGraphics();
// clear the exposed area
offgc.setColor(getBackground());
offgc.fillRect(0, 0, d.width, d.height);
offgc.setColor(getForeground());
// do normal redraw
paint(offgc);
// transfer offscreen to window
g.drawImage(offscreen, 0, 0, this);
}

但这似乎根本没有帮助。如果有用的话,这里有一些正在发生的事情的图片。它应该是这样的:

full

但大多数时候它看起来像这样:

partial1

或者这个:

partial2

提前致谢!

最佳答案

这并不是双缓冲真正的工作方式,也不是绘制过程的工作方式。

  • 不要覆盖更新
  • 尽可能不要覆盖顶级容器(如 Applet/JApplet/Frame/JFrame)的 paint
  • 使用可以渲染的“绘画”面板,最好是像 JPanel 这样的面板。 Swing组件提供双缓冲支持
  • 双缓冲区应该在绘制周期之外绘制,并且仅在需要时更新,这使得整体绘制过程更快,因为您不必重新渲染内容。
  • 当需要更新缓冲区时,首先渲染到临时缓冲区,这可以确保更新时可能发生的任何重绘不会过早反射回屏幕...

enter image description here

public class TestPaintGeometry {

public static void main(String[] args) {
new TestPaintGeometry();
}

public TestPaintGeometry() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ShowPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class ShowPane extends JPanel {

private GeometryPane geoPane;

public ShowPane() {
setLayout(new BorderLayout());

geoPane = new GeometryPane();
JButton redrew = new JButton("Redraw");
add(geoPane);
add(redrew, BorderLayout.SOUTH);
redrew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
geoPane.redrew();
}
});
}
}

public class GeometryPane extends JPanel {

private BufferedImage buffer;

public void redrew() {
Path2D.Float path = new Path2D.Float();
int width = getWidth();
int height = getHeight();

int points = Math.max(10, (int) Math.round(Math.random() * 100));
for (int index = 0; index < points; index++) {
int x = (int) Math.round(Math.random() * width);
int y = (int) Math.round(Math.random() * height);
if (index > 0) {
path.lineTo(x, y);
} else {
path.moveTo(x, y);
}
}

BufferedImage tmp = createCompatibleImage(width, height);
Graphics2D g2d = tmp.createGraphics();
g2d.setColor(Color.BLACK);
g2d.draw(path);
g2d.dispose();

buffer = tmp;
repaint();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (buffer != null) {
int x = (getWidth() - buffer.getWidth()) / 2;
int y = (getHeight() - buffer.getHeight()) / 2;
g.drawImage(buffer, x, y, this);
}
}
}

public static GraphicsConfiguration getGraphicsConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}

public static BufferedImage createCompatibleImage(int width, int height) {
return createCompatibleImage(width, height, Transparency.TRANSLUCENT);
}

public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
image.coerceData(true);
return image;
}
}

这允许您将GeometryPane部署到JFrameJAppelt,因为它不受其继承遗留的限制...

关于Java applet 仅部分绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com