Search code examples
javaswingpaintcomponent

Why aren't my graphics showing?


I'm an amateur java coder and I want to make a game similar to SpaceInvaders.

I was wondering why is my frame is blank when I attempt to add my paintComponent Graphic via a panel but when I add my code regularly it shows all results?

Here's my objects class when the graphics are added without a panel and there is no conflict:

package SS;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Serializable;
import java.util.*;
public class objects extends JPanel{
private int rand1, rand2, y;
int x=0;
Random rand = new Random();
public class picture extends JPanel{
@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.BLACK);
    for (int c=0;c<=1000;c++){
        g.setColor(Color.yellow);
        rand1=rand.nextInt(1920);
        rand2=rand.nextInt(720);
        g.drawOval(rand1, rand2, 1, 1);}
    g.setColor(Color.MAGENTA);
    g.fillRect(1920/2-x,720-100+y, 50, 50);
        }
    }
}

Now this version of my objects class produces a blank frame

package SS;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Serializable;
import java.util.*;
public class objects extends JPanel{
private int rand1, rand2, y;
int x=0;
Random rand = new Random();
public objects(){
    JPanel jp = new JPanel();
    picture p = new picture();
    jp.add(p);
}
public class picture extends JPanel{
@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.BLACK);
    for (int c=0;c<=1000;c++){
        g.setColor(Color.yellow);
        rand1=rand.nextInt(1920);
        rand2=rand.nextInt(720);
        g.drawOval(rand1, rand2, 1, 1);}
    g.setColor(Color.MAGENTA);
    g.fillRect(1920/2-x,720-100+y, 50, 50);
        }
    }
}

My purpose of making an external class around paintComponent is that I can make a component out of it for future key binding.

It's driving me crazy! I searched around this site and I cant find anything! If anyone can tell me the cause and solution I would deeply appreciate it :)

This is my main class by the way

package SS;
import javax.swing.JFrame;
public class mainframe {
    public static void main(String[] args){
        JFrame mainFrame = new JFrame();
        mainFrame.setBounds(0, 0, 1920, 720);
        mainFrame.setResizable(false);
        mainFrame.getContentPane().add(new objects());
        mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
}

I'm open to criticism and proper wording for anything I said in this post.

Just to iterate what I mean: I want to add the graphics I made from my paintComponent method to the jpanel listed as jp, then add that jpanel to my frame in the mainFrame class

Also sorry if my missed anything, this is my first post in StackOverflow.


Solution

  • In the second example you are adding picture panel to a new jp panel in objects constructor but that new panel is not added to the objects itself. Try to add the following line at the end of objects constructor: this.add(jp);.

    Update on how to specify preferred size:

    public class picture extends JPanel{
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 500);
                }  
    ...
    }