本文介绍了如何在transperant JFrame上使用paintComponent重置图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,当我想制作动画时,我会通过绘制矩形重置所有图形屏幕:
Usually when I want to make animation, I reset all the graphic screen by drawing rectangle:
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,1000,1000);
// now im drawing the animation on empty screen
}
After I drawed the Rect I can draw the animation on empty screen so the animation will move instead of spread. Right now I want to draw animation on Transparent JFrame. How can I empty the component from Precedings drawings, and still keep the JFrame transperent?
推荐答案
- 制作确保您的组件不透明
- 为了使透明性起作用,您不需要在
paintComponent()
(特别是不用WHITE填充矩形) - 每当你想重置所有东西,设置一个标志或一个条件并调用
repaint()
。在paintComponent()
中,检查该标志或条件,如果为true,则不要绘制任何东西。
- Make sure your components are non-opaque
- For the transparency to work, you don't need to do anything in the
paintComponent()
(especially not filling a rectangle with WHITE) - Whenever you want to reset everything, set a flag or a condition and invoke
repaint()
. In thepaintComponent()
, check for that flag or condition and if true, don't paint anything.
以下是一个半透明框架的小型演示,在鼠标移动的任何地方都会绘制一条线。只要你点击框架,它就会删除所有内容。
Here is a small demo of a semi-transparent frame which paints a line everywhere your mouse goes. Whenever you click on the frame, it removes everything.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestTransparentFrame {
private class PaintPanel extends JPanel {
private List<Point> points = new ArrayList<Point>();
public PaintPanel() {
setOpaque(false);
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
points.clear();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
};
addMouseListener(adapter);
addMouseMotionListener(adapter);
setBorder(BorderFactory.createLineBorder(Color.GREEN));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (points.size() > 1) {
g.setColor(Color.RED);
Point p1 = points.get(0);
for (int i = 1; i < points.size(); i++) {
Point p2 = points.get(i);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
p1 = p2;
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(700, 500);
}
}
protected void createAndShowGUI() throws MalformedURLException, IOException {
JFrame frame = new JFrame("Test transparent painting");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 50));
frame.add(new PaintPanel());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new TestTransparentFrame().createAndShowGUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
这篇关于如何在transperant JFrame上使用paintComponent重置图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!