在Java中调整大小时绘制的内容不可见
请注意,我没有在Windows计算机上测试此功能,只是在Mac计算机上测试。我不确定这是否也发生在Windows计算机上… 当我调整Java应用程序的大小时,内容是不可见的。我已经找到了一种在调整大小后修复它的方法,但不是在用户调整窗口大小时在Java中调整大小时绘制的内容不可见,java,graphics,resize,window,minimize,Java,Graphics,Resize,Window,Minimize,请注意,我没有在Windows计算机上测试此功能,只是在Mac计算机上测试。我不确定这是否也发生在Windows计算机上… 当我调整Java应用程序的大小时,内容是不可见的。我已经找到了一种在调整大小后修复它的方法,但不是在用户调整窗口大小时 public void componentResized(ComponentEvent e) { this.contentView.paint(this.contentView.getGraphics()); } 我没有使用Swing或其他东西,
public void componentResized(ComponentEvent e) {
this.contentView.paint(this.contentView.getGraphics());
}
我没有使用Swing或其他东西,因为它使我的二进制文件速度非常慢(在我看来)
结构如下:
我的主窗口框架
主窗口的内容视图包含
r
基于容器的子视图,包括
-方法绘制(图形g)
public void componentResized(ComponentEvent e) {
this.contentView.paint(this.contentView.getGraphics());
}
我注意到使用paint(getGraphics())
-方法并不是一个很好的方法,但是由于repaint()
-方法根本不起任何作用,所以它是唯一可行的方法
调整大小时,所有绘制的内容都将不可见。但是,当我将按钮
-实例添加到我的内容视图
并调整我的主窗口的大小
时,按钮不会不可见
我能够跟踪“实时”-调整大小事件:
public void componentMoved(ComponentEvent e) {
System.out.println("Live-resize");
}
public void componentMoved(ComponentEvent e) {
System.out.println("Live-resize");
this.contentView.paint(this.contentView.getGraphics());
}
或
当我将我的应用程序最小化到dock并再次将应用程序最大化时,同样的事情也会发生,我想需要相同的代码来解决这个问题
我没有使用Graphics2D
或其他什么东西,只是Graphics
你能给我解释一下如何重新绘制视图吗
提前感谢,,
Tim我更熟悉Swing,但本文区分了系统触发和应用程序触发的绘制。下面的示例显示了在调整窗口大小时,系统如何调用
paint()
,而应用程序如何调用repaint()
,它调用update()
,以响应鼠标事件。这种行为是跨平台的
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class AWTPaint {
public static void main(String[] args) {
Frame frame = new Frame();
frame.add(new CirclePanel());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
private static class CirclePanel extends Panel {
private static final Random r = new Random();
public CirclePanel() {
this.setPreferredSize(new Dimension(320, 240));
this.setForeground(new Color(r.nextInt()));
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
CirclePanel.this.repaint();
}
});
}
@Override
public void update(Graphics g) {
this.setForeground(new Color(r.nextInt()));
}
@Override
public void paint(Graphics g) {
Dimension size = this.getSize();
int d = Math.min(size.width, size.height) - 10;
int x = (size.width - d) / 2;
int y = (size.height - d) / 2;
g.fillOval(x, y, d, d);
g.setColor(Color.blue);
g.drawOval(x, y, d, d);
}
}
}
这里是使用Swing的同一个程序,仅供参考。因为
JPanel
是双缓冲的,所以在调整大小后释放鼠标时,它不会闪烁
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class SwingPaint {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new CirclePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private static class CirclePanel extends JPanel {
private static final Random r = new Random();
public CirclePanel() {
this.setPreferredSize(new Dimension(320, 240));
this.setForeground(new Color(r.nextInt()));
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
CirclePanel.this.update();
}
});
}
public void update() {
this.setForeground(new Color(r.nextInt()));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = this.getSize();
int d = Math.min(size.width, size.height) - 10;
int x = (size.width - d) / 2;
int y = (size.height - d) / 2;
g.fillOval(x, y, d, d);
g.setColor(Color.blue);
g.drawOval(x, y, d, d);
}
}
}
好吧,我终于修好了 与每次在
paint(Graphics g)
-方法中重新绘制图像不同,您需要缓冲输出并只重新绘制图像(我有点希望Java已经这样做了,就像Obj-C一样)
现在,当你最小化窗口并再次将其最大化时,绘画仍然存在。只是,窗子忽悠了一秒钟左右,但我并不在乎。也见“AWT和Swing中的绘画”,考虑短、自包含、正确(可编译)、示例(SSCCE)。这就说明了问题所在:我建议您调查一下为什么您认为Swing会使应用程序如此缓慢,而不是尝试使用AWT组件来克服这一限制。我认为还有更优雅的解决方案:)另请参阅关于纵横比的内容。另请参阅?另请参阅?
public BufferedImage buffer;
public void redraw() {
buffer = new BufferedImage(
200, // height
300, // width
BufferedImage.TYPE_4BYTE_ABGR); // ABGR = RGBA, 4-byte (r, g, b, a) per pixel
Graphics g = buffer.getGraphics();
// do your drawing here
if (this.getGraphics()) {
// 'this' is already shown, so it needs a redraw
this.paint(this.getGraphics()); // little hack
}
}
public void update(Graphics g) {
this.paint(g);
}
public void paint(Graphics g) {
g.drawImage(buffer, 0, 0, this);
}