java - 此代码不会显示矩形,但它应该

标签 java swing jframe bufferedimage graphics2d

我正在制作一个 JFrame 并在其上绘制一个矩形。 不行,有时全黑有时全白,这是我的方法。

因此渲染方法被调用两次,因为第一次创建缓冲区时,也忽略帧速率,现在无关紧要。

编辑1:我解决了一个问题: 现在它正在绘制一个矩形,但有时它只是显示一个白色屏幕。我还需要解决这个问题

Edit2:我不仅在寻找解决方案,我也在寻找我的问题发生的原因,所以我不只是盲目地编写代码。

    public MyFrame(String title,int width,int height)
    {
        super(title);
        this.setSize(width,height);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setUndecorated(true);
        this.addKeyListener(new KeyInput());
        this.setVisible(true);
    }
    public void draw(Graphics2D g,int arg)
    {
        g.setColor(new Color(0,0,0,255));
        g.fillRect(0,0,SIZE,SIZE);
        g.setColor(new Color(255,255,255));
        g.fillRect(0,0,50,50);
    }
    public void render(int arg)
    {
        BufferStrategy bs=this.getBufferStrategy();
        if(null==bs)
        {
            this.createBufferStrategy(3);
        }
        else
        {
            Graphics2D g=(Graphics2D)bs.getDrawGraphics();
            g.setColor(new Color(0,0,0,255));
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            BufferedImage canvas=new BufferedImage(SIZE,SIZE,2);
            int s=Math.min(this.getWidth(),this.getHeight());
            g.drawImage(canvas,(this.getWidth()-s)/2,(this.getHeight()-s)/2,s,s,this);
            Graphics2D g2=canvas.createGraphics();
            this.draw(g2,arg);
            bs.show();
            g.dispose();
            g2.dispose();
        }
    }
    public static void main(String[] args)
    {
        Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
        FRAME=new MyFrame("Insert name here!",d.width,d.height,1);
        FRAME.render(0);
        FRAME.render(0);
    }

编辑:这不再是必要的,我已经设法解决了问题,无论如何感谢您的帮助。

最佳答案

您需要的所有信息都可以在 tutorial 中找到气垫船建议。
以下代码演示了绘制全屏大小的 JFrame 黑色矩形,以及有关 mcve 的一些附加信息。

import java.awt.Color;   //mcve should include imports
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyFrame extends JFrame{

    public MyFrame()
    {
        super();
        //this.setSize(width,height); use :
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //comment 1 : see http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
        //as Hovercraft suggested
        JPanel customJPanel = new MyPanel();
        add(customJPanel);

        // not needed to demonstrate the issue. Remove to make mcve
        /*
        this.setLocationRelativeTo(null);
        this.setUndecorated(true);   also removes frame title
        this.addKeyListener(new KeyInput()); 
        */
        pack();
        setVisible(true);
    }

    //comment 1
    class MyPanel extends JPanel {

        public MyPanel() { super();  }

        @Override
        public void paintComponent(Graphics g) {

            super.paintComponent(g);

            int x = getRectangleXpos();         //if postion and / or size 
            int y = getRectangleYPos();         //do not change, no need to 
            int width = getRectangleWidth();    //recalculate : move from this method
            int height = getRectangleHeight();
            g.drawRect(x, y , width,height);
            g.setColor(Color.BLACK);
            g.fillRect(x, y , width,height);
        }

        private int getRectangleXpos() {
            //apply calculation logic if needed
            return 200;
        }

        private int getRectangleYPos() {
            //apply calculation logic if needed
            return 300;
        }

        private int getRectangleWidth() {
            //apply calculation logic if needed
            return 500;
        }

        private int getRectangleHeight() {
            //apply calculation logic if needed
            return 400;
        }
    }

    public static void main(String[] args)
    {
        //compilation error: The constructor MyFrame(String, int, int, int) is undefined
        //new MyFrame("Insert name here!",d.width,d.height,1);

        //comment 1
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MyFrame();
            }
        });
    }
} 

关于java - 此代码不会显示矩形,但它应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46043212/

相关文章:

java - 调用不同类中的方法并传递数组

java - java框架关闭后是否完全释放?

java - 在 JPanel 中检测 mouseClick 并防止在 JPanel 中绘制圆圈的问题

java - 将值从 jinternalframe 1 传递到 jinternalframe 2

java - 有人可以在 hibernate 中向我解释@MapsId吗?

java - 事件派发线程在哪里调用?

java - 通过 JFrame 添加到 ArrayList 时出现 NullPointerException 错误

java - 是什么导致了这个空指针异常?

使用 GUI 进行 Java swing 切换进程

java - 用冒号分隔小数点