issue with JFrame, JPanel (can't see my line - simple problem)
I added a Line and Button to JPanel. And then I added the JPanel to the JFrame. I can only see the button. Can someone explain what I did wrong? Thanks in advance.
I used two classes:
Class Panel - where I add the Line and Button to the Frame.
Code:
package model;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Panel {
public static void main(String[] args){
JFrame frame = new JFrame("Practice");
JPanel panel = new JPanel();
JButton button = new JButton("Hello");
frame.setVisible(true);
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Line object = new Line();
object.drawing();
panel.add(object);
panel.add(button);
frame.add(panel);
}
}
Class Line - where I draw the line
Code:
package model;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Line extends JPanel{
public void drawing(){
repaint();
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawLine(20, 40, 300, 280);
}
}
Re: issue with JFrame, JPanel (can't see my line - simple problem)
Just a guess, but in the Line's paintComponent method place this:
Code:
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawLine(20, 40, 300, 280);
System.out.println("Line size: " + getSize());
}
I think it will tell you why you're not seeing the line. Consider giving your Line class a getPreferredSize() method override to fix this.
Re: issue with JFrame, JPanel (can't see my line - simple problem)
Not exactly.
Nothing happened when i did it.
but in line 28 I wrote
frame.add(object) //instead of panel.add(object)
and it worked and gave me the dimensions of my line.
Why is this happening?
I thought Jpanel is supposed to be a powerful drawing tool where I can do multiple actions on it at once
Re: issue with JFrame, JPanel (can't see my line - simple problem)
Quote:
Originally Posted by
juhess88
Not exactly.
Nothing happened when i did it.
You're not seeing a println statement showing the size of the Line JPanel?
Quote:
but in line 28 I wrote
frame.add(object) //instead of panel.add(object)
and it worked and gave me the dimensions of my line.
Why is this happening?
You're adding a JPanel with a preferredSize of 0, 0 to a FlowLayout-using container, and so the size of your Line JPanel will be 0.
Quote:
I thought Jpanel is supposed to be a powerful drawing tool where I can do multiple actions on it at once
It is when used correctly.
Again, consider giving the Line class a getPreferredSize() method override returning an appropriate Dimension. consider putting your JButton onto the Line JPanel.
Avoid using setSize() anywhere and use pack() on the JFrame as well before setVisible(). Also, there's no benefit to calling repaint() on your Line JPanel so get rid of that (unless you're doing it as part of an animation).
Re: issue with JFrame, JPanel (can't see my line - simple problem)
For example
Code:
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class TestLine {
public static void main(String[] args) {
JFrame frame = new JFrame("Practice");
Line panel = new Line();
JButton button = new JButton("Hello");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class Line extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 500;
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawLine(20, 40, 300, 280);
System.out.println("Line size: " + getSize());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
Also, call pack() and setVisible(true) on the JFrame AFTER adding all components to the GUI, else it will paint itself prematurely.
Re: issue with JFrame, JPanel (can't see my line - simple problem)
Thanks so much! you are amazing.
Unfortunately I am still confused a tiny bit...
Why am I adding the button to line? I thought I was supposed to create a panel Jpanel and add button and line to it.
and then add the panel to the JFrame
Re: issue with JFrame, JPanel (can't see my line - simple problem)
When I create my Line instance am I actually creating my own JPanel??
Re: issue with JFrame, JPanel (can't see my line - simple problem)
Quote:
Originally Posted by
juhess88
When I create my Line instance am I actually creating my own JPanel??
You can answer that easily. Your Line class inherits all the public states and behaviors of its parent class, and so what class does Line extend?
Re: issue with JFrame, JPanel (can't see my line - simple problem)
Re: issue with JFrame, JPanel (can't see my line - simple problem)
My problem got a little more complicated (and I tried doing this myself for a couple hours but I need some more guidance)
I want to add a Line, Rectangle, Circle to the JFrame. But it only adds the circle and line.
I think my mistake is that each of my classes creates it's own individual JPanel.
How can I create a general JPanel and put Line, Circle, Rectangle in it?
Also as you can see from line 18 circle.add(rectangle) added the rectangle to the circle panel. Why am I able to view the rectangle if it is it's own rectangle panel? And why can't I view the line when in line 20 I write circle.add(line)?
Class Panel - adds the shapes to JFrame
Code:
package model;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class Panel {
public static void main(String[] args) {
JFrame frame = new JFrame("Practice");
Line line = new Line();
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
circle.add(rectangle); //adds rectangle
circle.add(line); //doesn't add line
frame.add(circle);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Class Line - draws a line
Code:
package model;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
class Line extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 500;
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawLine(20, 40, 300, 280);
System.out.println("Line size: " + getSize());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
Class Rectangle - draws rectangle
Code:
package model;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
class Rectangle extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 500;
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawRect(200, 220, 30, 40);
System.out.println("Rectangle size: " + getSize());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
Class Circle - draws circle
Code:
package model;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
class Circle extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 500;
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawOval(10, 20, 50, 50);
System.out.println("Circle size: " + getSize());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}