Java JPanel在重叠期间闪烁
我正在尝试制作一个在屏幕顶部随机显示JPanel的程序。这些JPanel包含笑脸的PNG缓冲图像(通过覆盖PaintComponent)。不幸的是,每当我运行程序,并且面板被绘制在任何重叠的位置时,图像就会开始闪烁(似乎试图同时显示自己,而不是合成)。我做了一些研究,试图解决这个问题,但没有成功。我的代码如下:Java JPanel在重叠期间闪烁,java,swing,jpanel,bufferedimage,compositing,Java,Swing,Jpanel,Bufferedimage,Compositing,我正在尝试制作一个在屏幕顶部随机显示JPanel的程序。这些JPanel包含笑脸的PNG缓冲图像(通过覆盖PaintComponent)。不幸的是,每当我运行程序,并且面板被绘制在任何重叠的位置时,图像就会开始闪烁(似乎试图同时显示自己,而不是合成)。我做了一些研究,试图解决这个问题,但没有成功。我的代码如下: public class MainScreen extends JFrame{ public MainScreen ms; private Smiley smileyPanel; pr
public class MainScreen extends JFrame{
public MainScreen ms;
private Smiley smileyPanel;
private int randomPosition;
public AlphaComposite ac;
private Graphics2D graphicsPane;
//main method
public static void main (String[] args){
MainScreen ms = new MainScreen();
}
//this is my attempted fix to the program
protected void Paint (Graphics g){
Graphics2D graphicsPane = (Graphics2D) g;
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
graphicsPane.setComposite(ac);
super.paint(g);
}
//this creates the main frame and also starts creating smileys at the top
public MainScreen(){
super("Main Screen Window");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500,800);
this.getContentPane().setBackground(Color.black);
this.setLayout(null);
createSmiley(); createSmiley(); createSmiley(); createSmiley(); createSmiley(); createSmiley();
this.setVisible(true);
}
//random number generator used to place an image at the top
public void setRandomPosition(){
Random generator = new Random();
randomPosition = generator.nextInt(473);
}
//this is the method to create an image at a random location at the top
public void createSmiley(){
smileyPanel = new Smiley();
this.add(smileyPanel);
setRandomPosition();
smileyPanel.setBounds(randomPosition, 0, 28, 29);
}
}
还有我的另一个班:
public class Smiley extends JPanel{
private BufferedImage smileyFace;
private Dimension smileyPosition;
private File smileyFile;
public Smiley() {
//this is the code to retrieve the smiley file
try{
smileyFile = new File("C:\\Users\\Devon\\Desktop\\smiley.png");
smileyFace = ImageIO.read(smileyFile);
}
catch (Exception e){
System.out.println("There was an error finding or reading the file \" smiley.png.\"");
}
}
//override of paintComponent method in order to display the grabbed image file onto a JPanel
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(smileyFace, 0, 0, null);
super.setBackground(null);
super.setSize(28,29);
}
}
Swing
的基本知识了解得足够多了吗?你开始尝试图形了。由于真正的基础建议-首先将所有组件添加到顶级容器,然后在提供的代码中将其设置为可见,因此我很失望看到其他情况。此外,您说您重写了paintComponent
方法,尽管在粘贴的代码中我看不到这种情况。这是什么Paint
方法?谁在调用此方法?super.paint(g)
在paint()方法中做什么?其中是EDT事件调度程序线程?受保护的无效油漆(图g){
使用@Override
符号表示信息丰富的编译器错误。现在停下来重新思考一下-你真的足够了解Swing
的基本知识了吗,以至于你开始尝试图形
。因为真正的基本知识建议-首先将所有组件添加到顶级容器中,然后将其设置为在提供了代码,我很失望看到其他情况。此外,你说你重写了paintComponent
方法,尽管在粘贴的代码中我看不到这种情况。这个Paint
方法是什么?谁在调用这个方法?在super.Paint(g)
内部做什么Paint()
method?EDT事件调度程序线程在哪里
?受保护的void Paint(图形g){
使用@Override
符号表示信息丰富的编译器错误。现在停下来重新思考一下-你真的足够了解Swing
的基本知识了吗,以至于你开始尝试图形
。因为真正的基本知识建议-首先将所有组件添加到顶级容器中,然后将其设置为在提供了代码,我很失望看到其他情况。此外,你说你重写了paintComponent
方法,尽管在粘贴的代码中我看不到这种情况。这个Paint
方法是什么?谁在调用这个方法?在super.Paint(g)
内部做什么Paint()
method?EDT事件调度程序线程在哪里
?受保护的void Paint(图形g){
使用@Override
符号表示信息丰富的编译器错误。现在停下来重新思考一下-你真的足够了解Swing
的基本知识了吗,以至于你开始尝试图形
。因为真正的基本知识建议-首先将所有组件添加到顶级容器中,然后将其设置为在提供了代码,我很失望看到其他情况。此外,你说你重写了paintComponent
方法,尽管在粘贴的代码中我看不到这种情况。这个Paint
方法是什么?谁在调用这个方法?在super.Paint(g)
内部做什么Paint()
method?EDT事件调度程序线程在哪里
?受保护的无效绘制(图形g){
使用@Override
表示编译器错误信息。。