0

I am making a JFrame and drawing a rectangle onto it. It doesn't work, sometimes it is completely black and sometimes completely white, here are my methods.

So the render method is called twice because the first time it creates the buffer, also ignore framerate, it is irrelavent right now.

Edit1: I solved one problem: It is drawing a rectangle now, but sometimes it is just displays a white screen. I still need to solve that problem

Edit2: I am not only looking for the solution, I am also looking for the reason my problem happens, so I am not just blindly writing code.

    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);
    }

Edit: this is no longer neessary, I have managed to solve the problem, thank you for your help anyways.

  • 2
    1) *"INSERT_FRAME_NAME_HERE"* Nor should it be. Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. Even in example code. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 04 '17 at 19:46
  • Ok, I will change it. – DSOI__UNUNOCTIUM Sep 04 '17 at 19:53
  • 2
    Please check out: [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html): introductory tutorial to Swing graphics – Hovercraft Full Of Eels Sep 04 '17 at 20:00
  • I checked it out, but it didn't give me much information, my problem is that it sometimes displays a completely black screen and sometimes completely white. – DSOI__UNUNOCTIUM Sep 04 '17 at 20:10
  • 1
    Like Eels is sayiing..kinda.. create a class that extends JPanel and override paintComponent method. add instance of class to JFrame contentpane. as in the example – Jack Flamp Sep 04 '17 at 20:13

1 Answers1

1

All the information you need can be found in the tutorial Hovercraft suggested.
The following code demonstrates drawing black rectangle of a full-screen sized JFrame, with some additional info about 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();
            }
        });
    }
} 
c0der
  • 18,467
  • 6
  • 33
  • 65