2

I'm a bit new to programming right now and am trying to make Pong in java. However I'm not able to make any graphics show up.

Main Class

public class Pong1 {

    public static Frame frame = new Frame();
    public static Panel panel = new Panel();

    public static void main(String[] args) {
        initUI();
    }

    public static void initUI(){

        frame.setLayout(new BorderLayout());
        frame.add(BorderLayout.CENTER, panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static int getPanelWidth(){
        return panel.getWidth();
    }

    public static int getPanelHeight(){
        return panel.getHeight();
    }
}

JFrame Class

package pong1;

import java.awt.Dimension;
import javax.swing.JFrame;

public class Frame extends JFrame{

    Frame(){
        setTitle("Pong");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(true);
        setMinimumSize(new Dimension(800,500));
        setLocationRelativeTo(null);
    }       
}

In this final class is the JPanel class where I override and call paintComponent. However, nothing shows up.

public class Panel extends JPanel{

    Panel(){
        setPreferredSize(new Dimension(800,500));
        setMinimumSize(new Dimension(800,500));
    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.blue);
        g2.fillOval(100,100,100,100);


    }

I've looked at dozens of other posts and I've tried many different things, and yet nothing shows up. Does anyone know what is going on?

mike413
  • 35
  • 3
  • `static` is not your friend and is a poor design choice – MadProgrammer Jun 17 '15 at 01:05
  • Oh, and your code works fine for me – MadProgrammer Jun 17 '15 at 01:06
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jun 17 '15 at 01:06
  • Maybe starting the UI within the context of the Event Dispatching Thread might help. See [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) for more details – MadProgrammer Jun 17 '15 at 01:08
  • When do you add your JPanel to your JFrame? – arjuns Jun 17 '15 at 01:10
  • @javaislife The OP does so in the `initUI` method of `Pong1` - `frame.add(BorderLayout.CENTER, panel);` <-- `panel` is a `static` reference to a`Panel`... – MadProgrammer Jun 17 '15 at 01:13
  • I see. I really need to read the code rather than skimming over it! :) – arjuns Jun 17 '15 at 01:15
  • @javaislife I ran it, and it works fine :P – MadProgrammer Jun 17 '15 at 01:20
  • @MadProgrammer Thanks for the quick replies, but the code works fine for you? As in the circle is visible? I don't understand exactly what to do with fixing it. – mike413 Jun 17 '15 at 01:21
  • @mike413 Yep, nice big blue circle – MadProgrammer Jun 17 '15 at 01:23
  • @MadProgrammer Well shoot, I don't know why it comes up blank if you didn't change anything. Another question, what did you mean by static is a poor design choice? And thanks for your help. – mike413 Jun 17 '15 at 01:26
  • `static` references are dangerous and easily reassigned meaning you can never gurentee what they might be pointing to. Case in point, in you code, if after you add `panel` to `frame` and call `panel = new Panel();` the instance which is on the screen is no longer the instance pointed to by your `static` reference and nobody knows that it's occurred. It also means that the class becomes available for (possible) mishandling, meaning I can make changes to it as I please without any control what so ever. – MadProgrammer Jun 17 '15 at 01:31
  • Typically, `static` is used this way when people want to "access" the instance of the object from some other part of their program, but don't have the experience to use other methods, like passing the reference as a parameter via constructors or methods. This leads into a wider discussion of models and observers to better manage object exposure, but that's a topic for another question ;) – MadProgrammer Jun 17 '15 at 01:33
  • Thanks, this clears up a lot of things. – mike413 Jun 17 '15 at 01:35

1 Answers1

2

I run you posted code and it seemed to work just fine, however, the reliance on static is very worrying and is likely to lead into some very interesting problems, best to avoid it.

Also, you should ensure that your UI is created within the context of the Event Dispatching Thread to reduce possible thread violation issues which could cause more problems.

Finally, you don't really need to extend JFrame, you're not really adding any value to the class and you could use a factory or builder pattern to achieve the same thing with less complexity

Blue Sun

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Pong1 {

    public static void main(String[] args) {
        new Pong1();
    }

    public Pong1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Pong");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Panel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Panel extends JPanel {

        public Panel() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 500);
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println("...");
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            g2.fillOval(100, 100, 100, 100);

        }
    }
}

Oh, and you really should avoid using setPreferred/Minimum/MaximumSize, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more discussions on the subject

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366