Java JlayeredPane透明度设置不透明不工作
我正在创建一个模拟类型的应用程序,我想要一个背景层和另一个层上的所有动画。我目前正在使用JlayeredPanes,但我无法使顶层的背景显示为透明,以便我可以看到背景,非常感谢任何帮助,以下是代码: 背景层Java JlayeredPane透明度设置不透明不工作,java,swing,transparent,jlayeredpane,Java,Swing,Transparent,Jlayeredpane,我正在创建一个模拟类型的应用程序,我想要一个背景层和另一个层上的所有动画。我目前正在使用JlayeredPanes,但我无法使顶层的背景显示为透明,以便我可以看到背景,非常感谢任何帮助,以下是代码: 背景层 public class SimBackground extends JLayeredPane{ private Model theModel; private SimulationArea simulationArea; public SimBackground(Model theMo
public class SimBackground extends JLayeredPane{
private Model theModel;
private SimulationArea simulationArea;
public SimBackground(Model theModel){
this.theModel=theModel;
setBackground(new Color(0, 230, 0));
setOpaque(true);
setPreferredSize(new Dimension(500,500));
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for(int x=0;x<50;x++){
for(int y=0;y<50;y++){
g.drawRect((x*10), (y*10), 10, 10);
}
}
}
public类SimBackground扩展JLayeredPane{
私有模型;
私有模拟EA模拟EA;
公共SimBackground(模型theModel){
this.theModel=theModel;
挫折背景(新颜色(0230,0));
set不透明(true);
设置首选尺寸(新尺寸(500500));
setVisible(真);
}
@凌驾
公共空间涂料(图g){
超级油漆(g);
Graphics2D g2d=(Graphics2D)g;
对于(int x=0;x不要使用JLayeredPane,但如果将来确实需要使用JLayeredPane,您将需要阅读本教程,因为根据我的评论,您根本没有正确使用它们。相反,我建议您在单个JPanel中进行所有绘制,将背景绘制到可能在构造函数中的BuffereImage中,然后然后在JPanel的paintComponent方法中绘制该图像和精灵
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class SimExample extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final Color BKGD_COLOR = new Color(0, 230, 0);
private BufferedImage bkgrnd = new BufferedImage(PREF_W, PREF_H,
BufferedImage.TYPE_INT_ARGB);
public SimExample() {
Graphics2D g = bkgrnd.createGraphics();
g.setBackground(BKGD_COLOR);
g.clearRect(0, 0, PREF_W, PREF_H);
g.setColor(Color.black);
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
g.drawRect((x * 10), (y * 10), 10, 10);
}
}
g.dispose();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (bkgrnd != null) {
g.drawImage(bkgrnd, 0, 0, null);
}
// draw sprites here
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
SimExample mainPanel = new SimExample();
JFrame frame = new JFrame("SimExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入javax.swing.*;
公共类SimExample扩展了JPanel{
专用静态最终整数预值W=500;
私有静态final int PREF_H=PREF_W;
专用静态最终颜色BKGD_颜色=新颜色(0,230,0);
private BufferedImage bkgrnd=新的BufferedImage(PREF_W,PREF_H,
BuffereImage.TYPE_INT_ARGB);
公共SimExample(){
Graphics2D g=bkgrnd.createGraphics();
g、 立根背景(BKGD_颜色);
g、 clearRect(0,0,PREF_W,PREF_H);
g、 设置颜色(颜色为黑色);
对于(int x=0;x<50;x++){
对于(int y=0;y<50;y++){
g、 drawRect((x*10),(y*10),10,10);
}
}
g、 处置();
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
如果(bkgrnd!=null){
g、 drawImage(bkgrnd,0,0,null);
}
//在这里画精灵
}
@凌驾
公共维度getPreferredSize(){
如果(isPreferredSizeSet()){
返回super.getPreferredSize();
}
返回新维度(PREF_W,PREF_H);
}
私有静态void createAndShowGui(){
SimExample mainPanel=新SimExample();
JFrame=新JFrame(“SimExample”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}
等等,顶层扩展了JLayeredPane?很抱歉,这让我非常困惑。您的JLayeredPane不应该是所有层的底部,即容纳所有层的容器吗?并且所有层都不应该扩展JLayeredPane.JPanel或JComponent,是的,但不是JLayeredPane。您应该使用JPanel的paintComponent(…)
方法不在JLayeredPane中。请阅读JLayeredPane教程,因为您似乎还不知道如何使用它们。您可以在这里找到它……这些只是窗格,我还没有发布面板的代码,如果它们不扩展JLayeredPane,我如何在它们自己的类中创建它们?我在它们的类中找不到任何示例own classe你会使用一个单独的JLayeredPane,可能不会扩展它,你会将扩展JPanel或JComponent的对象添加到单独的JLayeredPane中。在这个网站上有很多使用它的例子,有些是我写的。这意味着要反复绘制它,不是吗?@user3081855:绘制BufferedIm的成本很低年龄。那太好了,我本来是这样做的,但出于某种原因,我认为最好的表现是加快速度layers@user3081855:例如。谢谢,我刚刚测试过,我在图标中有BuffereImage,以前有问题,这就是我放弃此解决方案的原因,但您的解决方案非常有效,谢谢
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class SimExample extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final Color BKGD_COLOR = new Color(0, 230, 0);
private BufferedImage bkgrnd = new BufferedImage(PREF_W, PREF_H,
BufferedImage.TYPE_INT_ARGB);
public SimExample() {
Graphics2D g = bkgrnd.createGraphics();
g.setBackground(BKGD_COLOR);
g.clearRect(0, 0, PREF_W, PREF_H);
g.setColor(Color.black);
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
g.drawRect((x * 10), (y * 10), 10, 10);
}
}
g.dispose();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (bkgrnd != null) {
g.drawImage(bkgrnd, 0, 0, null);
}
// draw sprites here
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
SimExample mainPanel = new SimExample();
JFrame frame = new JFrame("SimExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}