Reputation: 3
I've tried multiple things to get this to run. The intent is to draw circles that users can click. I cannot get anything to paint onto the JPanel. I originally was extending the JFrame but realized I needed to extend JPanel. I'm using IntelliJ GUI Designer for the layout. I would greatly appreciate any help.
package com.package;
import javax.swing.*;
import java.awt.*;
public class Main extends JPanel implements Runnable {
private JPanel jPanel1;
private JTabbedPane tabbedPane1;
private JTextField textField1;
private JButton submitButton;
private JButton resetButton;
private JPanel cartPanel;
private JLabel imageLabel;
private static Main instance;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(100,100,500,500);
g.setColor(Color.YELLOW);
g.fillOval(10, 10, 200, 200);
// draw Eyes
g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);
// draw Mouth
g.fillOval(50, 110, 120, 60);
// adding smile
g.setColor(Color.YELLOW);
g.fillRect(50, 110, 120, 30);
g.fillOval(50, 120, 120, 40);
}
public Main(){
JFrame jf = new JFrame();
jf.setTitle("Defect Mapping");
jf.setContentPane(this.jPanel1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setJMenuBar(jUnitMenuBar());
ImageIcon img = new ImageIcon("C:\\Users\\jaitken\\IdeaProjects\\ERP\\icon.gif");
jf.setIconImage(img.getImage());
jf.pack();
jf.setSize(1280, 720);
jf.setResizable(false);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
run();
}
public void run(){
instance = this;
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
}
Here is the IntelliJ form designer file.
Upvotes: 0
Views: 78
Reputation: 324197
jf.setContentPane(this.jPanel1);
The "jPanel1` variable is null. So that statement does nothing.
public class Main extends JPanel implements
Your custom painting is done in the "Main" class, but you never create an instance of that class or add the panel to the frame.
Read the section from the Swing tutorial on Custom Painting for a better way to structure your code and do the custom painting. I suggest you start over and download the demo code from "step 2" to use as the starting point for for your code.
jf.pack();
jf.setSize(1280, 720);
jf.setResizable(false);
The point of invoking pack()
is to that all the components added to the frame are displayed at their preferred size. Once you do the custom painting properly your custom panel will have a preferred size.
Invoking setSize(...)
defeats the purpose of invoking pack(). Get rid of the statement.
Also, the setResizable(...)
statement needs to be invoked BEFORE the pack() statement since it will change the size of the decorations on the frame.
run();
That statement should be the first statement in the main() method. The LAF must be set BEFORE you create Swing components.
The intent is draw circles that users can click
After you get the basic painting done you will need to fix it to keep an ArrayList of Shapes you want to paint so that you can later determine which circle the user clicks on. Check out: Drag a painted Shape for a complete example showing how to click and drag a shape around the panel.
Upvotes: 3