透明面板上的动画
Animation on a Transparent Panel
我想为透明面板上的对象设置动画(在本例中为 upperPanel
),但是一旦将动画对象添加到 upperPanel
,它的背景就不再透明了。
upperPanel
在 bottomPanel
中。 bottomPanel
的背景是红色的,可以看看upperPanel
是不是透明的
框架代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewFrame extends JFrame{
public static void main(String args[]){
JFrame f = new JFrame();
AnimatedObject a = new AnimatedObject();
JPanel bottomPanel = new JPanel();
JPanel upperPanel = new JPanel();
bottomPanel.setBackground(Color.red);
bottomPanel.setSize(300, 300);
bottomPanel.setLayout(new BorderLayout());
upperPanel.setSize(300, 300);
upperPanel.setOpaque(false);
upperPanel.setLayout(new BorderLayout());
f.add(bottomPanel);
bottomPanel.add(upperPanel);
upperPanel.add(a);
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
动画对象代码:
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class AnimatedObject extends javax.swing.JPanel implements ActionListener{
private int x = 0;
private int y = 0;
private boolean direction = true;
Timer tm = new Timer(50, this);
public AnimatedObject() {
initComponents();
tm.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new java.awt.Color(102, 102, 102));
g.fillRect(x, y, 30, 30);
System.out.println("Test gesetzt("+x+"|"+y+")");
}
public void moveForward() {
x = x + 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void moveBackwards() {
x = x - 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void moveDown() {
y = y + 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void moveUp() {
y = y - 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void actionPerformed(ActionEvent e) {
if (direction){
moveDown();
}
else {
moveUp();
}
if (((y >= 270) && (direction)) || ((y <= 0) && (!direction))) {
moveForward();
direction = !direction;
}
}
}
因为你的面板正在使用BorderLayout
s,这将强制子组件填充父组件的整个可用space,因为AnimatedObject
本身不是透明的,它正在填充它的 space 具有不透明填充颜色的父项。
您可以通过更改 AnimatedObject
的背景颜色来进行测试
public AnimatedObject() {
setBackground(Color.BLUE);
tm.start();
}
一个简单的解决方法是使 AnimatedObject
也透明。
public AnimatedObject() {
setOpaque(false);
tm.start();
}
我想为透明面板上的对象设置动画(在本例中为 upperPanel
),但是一旦将动画对象添加到 upperPanel
,它的背景就不再透明了。
upperPanel
在 bottomPanel
中。 bottomPanel
的背景是红色的,可以看看upperPanel
是不是透明的
框架代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewFrame extends JFrame{
public static void main(String args[]){
JFrame f = new JFrame();
AnimatedObject a = new AnimatedObject();
JPanel bottomPanel = new JPanel();
JPanel upperPanel = new JPanel();
bottomPanel.setBackground(Color.red);
bottomPanel.setSize(300, 300);
bottomPanel.setLayout(new BorderLayout());
upperPanel.setSize(300, 300);
upperPanel.setOpaque(false);
upperPanel.setLayout(new BorderLayout());
f.add(bottomPanel);
bottomPanel.add(upperPanel);
upperPanel.add(a);
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
动画对象代码:
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class AnimatedObject extends javax.swing.JPanel implements ActionListener{
private int x = 0;
private int y = 0;
private boolean direction = true;
Timer tm = new Timer(50, this);
public AnimatedObject() {
initComponents();
tm.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new java.awt.Color(102, 102, 102));
g.fillRect(x, y, 30, 30);
System.out.println("Test gesetzt("+x+"|"+y+")");
}
public void moveForward() {
x = x + 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void moveBackwards() {
x = x - 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void moveDown() {
y = y + 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void moveUp() {
y = y - 30;
repaint();
System.out.println("(" + x + "|" + y + ")");
}
public void actionPerformed(ActionEvent e) {
if (direction){
moveDown();
}
else {
moveUp();
}
if (((y >= 270) && (direction)) || ((y <= 0) && (!direction))) {
moveForward();
direction = !direction;
}
}
}
因为你的面板正在使用BorderLayout
s,这将强制子组件填充父组件的整个可用space,因为AnimatedObject
本身不是透明的,它正在填充它的 space 具有不透明填充颜色的父项。
您可以通过更改 AnimatedObject
public AnimatedObject() {
setBackground(Color.BLUE);
tm.start();
}
一个简单的解决方法是使 AnimatedObject
也透明。
public AnimatedObject() {
setOpaque(false);
tm.start();
}