简体   繁体   中英

I'm Need Help Adding My Paint Component to the Frame in a Class

I need help making what I am trying to paint visible on the screen. I was able to set it up properly in the main, however I feel like it would be more organized to keep everything in their own classes. The window will show up, but nothing will be painted. Even the background I set does not show up.

public class CharacterCreator extends JPanel {

//Declare Variables

ImageIcon icon = new ImageIcon();

//PAINT
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);


    //Drawing Code
    g.setColor(Color.red);
    g.drawOval(10, 10, 10, 10);
}

//Window Creator
public CharacterCreator() {
    super();
    JFrame application = new JFrame();
    application.setTitle("Window");
    application.setBackground(Color.WHITE);
    application.setIconImage(null);
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setSize(500, 400);
    application.setLocationRelativeTo(null);
    application.setVisible(true);


}
}

This is what the main looks like:

public class GameProject {
    public static void main(String [] args){
        JPanel CC = new CharacterCreator();
    }
}

You need to add CharacterCreator to your JFrame :

application.add(this);

Aside: Consider using Initial Threads

Change the name of the Window Creator and implement this:

 public CharacterCreator() {
    super();
    JFrame application = new JFrame();
    application.setTitle("Window");
    application.setBackground(Color.WHITE);
    application.setIconImage(null);
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setSize(500, 400);
    application.setLocationRelativeTo(null);
    application.setVisible(true);

    CharacterCreator panel = CharacterCreator();
    application.add(panel);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM