els*_*ooo 5 java graphics resize window minimize
请注意我还没有在Mac机器上的Windows机器上测试过这个.我不太确定这是否也出现在Windows机器上......
当我调整Java应用程序的大小时,内容是不可见的.我已经找到一种方法来解决它后调整其大小,但不能同时在用户调整窗口的大小.
我没有使用Swing或其他东西,因为它使我的二进制文件变得如此缓慢(在我看来).
结构是这样的:
Frame 我的主窗口Container内容视图main-windowContainer基于paint(Graphics g)-method 的子视图我已经添加了所有监听器My main-window,现在我可以在调整窗口大小后重绘Content-view .
public void componentResized(ComponentEvent e) {
this.contentView.paint(this.contentView.getGraphics());
}
Run Code Online (Sandbox Code Playgroud)
我谨慎使用paint(getGraphics())-method并不是一个非常好的方法,但是因为repaint()-method根本没有做任何事情,所以它是唯一可行的方法.
调整大小时,所有绘制的内容都变得不可见.但是,当我向我添加Button-instance Content-view并调整我Main-window的大小时,该按钮不会被隐藏.
我是能够追踪"live'调整大小事件:
public void componentMoved(ComponentEvent e) {
System.out.println("Live-resize");
}
Run Code Online (Sandbox Code Playgroud)
当我将repaint-method(或官方重绘方法)添加到像这样的'live'-resize事件时,我仍然得到输出,但是,它没有重新绘制或者某些东西
public void componentMoved(ComponentEvent e) {
System.out.println("Live-resize");
this.contentView.paint(this.contentView.getGraphics());
}
Run Code Online (Sandbox Code Playgroud)
要么
public void componentMoved(ComponentEvent e) {
System.out.println("Live-resize");
this.contentView.repaint();
}
Run Code Online (Sandbox Code Playgroud)
当我最小化我的应用程序到Dock并再次最大化应用程序时,同样的事情发生,我想需要相同的代码来解决这个问题.
我没有使用Graphics2D什么东西,只是Graphics.
能否请您解释一下我如何重新表达观点?
蒂姆,提前谢谢
作为参考,这是使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我对Swing比较熟悉,但文章" 在AWT和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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,我终于修好了。
您不需要在paint(Graphics g)- 方法中每次都重绘它,而是需要缓冲输出并仅重绘该图像(我有点希望 Java 已经这样做了,就像 Obj-C 一样)。
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);
}
Run Code Online (Sandbox Code Playgroud)
现在,当您最小化窗口并再次最大化它时,画作仍然存在。只是,窗口现在闪烁了 0.1 秒左右,但我并不关心这一点。
| 归档时间: |
|
| 查看次数: |
4861 次 |
| 最近记录: |