Java graphics help
I'm not too sure exactly what your problem is. I assume that it's not displaying what is being rendered on the second panel. If my understanding of your problem is correct, you have the JFrame, with a JPanel on it. That JPanel then has another JPanel contained within it. The second JPanel has no layout and nothing is being drawn to the screen when adding it in paintComponent(). If that is the case, here's a example I typed up to show you a potential issue.
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;public class GraphicsTest extends JFrame{ public GraphicsTest(){ JPanel container = new JPanel(); // the panel that contains the panel you are drawing to needs to have a // layout or the child panel will not render, comment out the line // below to see the problem in action container.setLayout(new BorderLayout()); container.add(new DrawablePanel(), BorderLayout.CENTER); this.add(container); // set up jFrame this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args){ new GraphicsTest(); }}class DrawablePanel extends JPanel { @[member=OverRide] public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.BLACK); g.drawString("Test", 125, 125); }}
The first panel, the one that contains the panel you are drawing to needs to have a layout for this to work. You can't nest two panels with no layout and attempt to paint to the second. I hope the code above is clear, but you're entirely right about using paintComponent.
If I assumed incorrectly, what exactly is not working? Also, sorry for the verbose comments, lol. The example code is not the prettiest but hopefully it conveys the point.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now