Jump to content

Java graphics help

Go to solution Solved by devianthead,

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.

While coding a game with JDK 8 in eclipse, I have hit a brick wall, so to speak. In JDK 7, I knew how to use graphics and was quite familiar with it, but I am completely at a loss after googling for solutions. According to multiple posts on Stackoverflow.com, the best practice is to use the paintComponent() method of a layoutless JPanel. I have done everything suggested as solutions for other people, including setting the preferred size and size, calling its super, setting its visibility to true, using validate(), and calling it via repaint() and setBackground(), but neither paintComponent() nor paint() is ever called. My JPanel is inside another JPanel which is inside a standard JFrame. My render() method calls repaint()/setBackground() about 60 times a second and is called indirectly via a gameloop in another thread.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
https://linustechtips.com/topic/484762-java-graphics-help/
Share on other sites

Link to post
Share on other sites

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.

Link to comment
https://linustechtips.com/topic/484762-java-graphics-help/#findComment-6530697
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×