0

I am trying to buff my java skills (been about 10 years since I coded). Currently I am just trying to make a basic program that will have balls bouncing off the edges of the JFrame. However, as a starter in this program I tried drawing a line and box on the JPanel.

The issue I am finding is I have to call frame.setResizable(false) in order or the screen to paint my box and line. It will paint them if I resize the JFrame after it comes up. However, I would like it to paint as soon as the JFrame opens.

Putting in:

frame.setResizable(false);
frame.setResizable(true);

seems redundant. Is there a cleaner way to do this so it paints when the JFrame opens?

Below is my code if this helps:

MAIN CLASS

package bbs;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class BouncingBalls {

    public static void main(String[] args) {
        //Create the basic frame, set its size, and tell it to be visible
        JFrame frame = new JFrame();
        frame.setSize(800, 600);
        frame.setVisible(true);

        //Get a icon for the Program
        ImageIcon logoicon = new ImageIcon("ball.jpg");
        Image logo = logoicon.getImage();
        frame.setIconImage(logo);

        frame.setResizable(false);
        frame.setResizable(true);

        //find the center of the screen and where the frame should go
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int w = frame.getSize().width;
        int h = frame.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        //Move the window
        frame.setLocation(x, y);
        //Tell the program to stop when the X button is selected
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

        Draw object = new Draw();
        frame.add(object);

        object.drawing();
    }

}

PAINTING CLASS

package bbs;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Draw extends JPanel {

    /**
     * This is added to handle the serialization warning and is of the type Long to accommodate the warning
     */
    private static final long serialVersionUID = 1L;

    public void drawing(){
        repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.drawLine(10, 20, 300, 200);

        g.setColor(Color.BLUE);
        g.fillRect(300, 200, 150, 200);
    }
}
  • Take `frame.setVisible(true);` and make it the last thing you call after you've added all the components to it. You can also use `frame.setLocationRelativeTo(null)` to center the frame on the screen, which will probably give better results than using `Toolkit.getDefaultToolkit().getScreenSize();` – MadProgrammer Mar 18 '17 at 02:33
  • If you need to update the UI after the frame is made visible, you can use `revalidate` and `repaint` on the container you've changed to trigger out a layout and paint pass – MadProgrammer Mar 18 '17 at 02:34

1 Answers1

1
frame.setVisible(true);

This should be the last statement executed AFTER all components have been added to the frame.

Then all the components will paint normally.

camickr
  • 321,443
  • 19
  • 166
  • 288