Reputation: 1
I am trying to display a graphic from a separate class on a JPanel of my main class.
The main class is mytest and the separate class is Ball. Ball has a paint component method and simply draws a colored circle. In mytest, I instantiate a ball and add it to a JPanel (dp): dp.add(ball). Very simple, but all I get is the white panel background and no ball is drawn.
package myStuff;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class mytest {
private JFrame frame=new JFrame();
private JPanel dp = new JPanel();
public static void main(String[] args) {
mytest gui = new mytest();
gui.go();
}
public void go() {
frame.setTitle("Test");
frame.setSize(1000,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel dp=new JPanel();
dp.setBackground(Color.WHITE);
Ball ball = new Ball(dp.getWidth(),dp.getHeight());
dp.add(ball);
frame.add(dp);
frame.setVisible(true);
}
}
package myStuff;
import java.awt.*;
import javax.swing.*;
public class Ball extends JComponent{
private int Width;
private int Height;
public Ball (int width, int height ) {
Width=width;
Height=height;
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
g2d.setColor(Color.RED);
g2d.fillOval(Width/2,Height/2,40,40);
System.out.println("Doing graphics....");
}
}
An red ball should show up on the dp panel. All I get is the panel background and no ball. I know it is trying since the "Doing graphics" prints out twice.
Upvotes: 0
Views: 771
Reputation: 40044
Here is a working example.
import java.awt.*;
import javax.swing.*;
public class Mytest {
private JFrame frame = new JFrame();
public static void main(String[] args) {
Mytest gui = new Mytest();
SwingUtilities.invokeLater(() -> gui.go());
}
public void go() {
frame.setTitle("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel dp = new JPanel();
dp.setPreferredSize(new Dimension(500, 500));
dp.setBackground(Color.WHITE);
Ball ball = new Ball(150, 150);
dp.add(ball);
frame.add(dp);
frame.pack(); // invokes layout and sizes components
frame.setLocationRelativeTo(null); // centers on screen
frame.setVisible(true);
}
}
class Ball extends JComponent {
private int width;
private int height;
// A ball should probably only have a "diameter"
public Ball(int width, int height) {
this.width = width;
this.height = height;
setPreferredSize(new Dimension(width, height));
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
g2d.setColor(Color.RED);
// smooths out the graphics
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(0, 0, width, height);
System.out.println("Doing graphics....");
}
}
The two biggest suggestions are to:
The reason no red ball was drawn (or only 1/4 of one) was because you changed the location of where to draw it within the Component window. You tried to draw it at width/2 and height/2 which was the center of the Component. It should have been at 0,0 for normal rendering.
Also read about painting in the The Java Tutorials 1
Upvotes: 1
Reputation: 41
Set the panel size before the line,
Ball ball = new Ball(dp.getWidth(),dp.getHeight());
Then add this code
setPreferredSize(new Dimension(Width, Height));
at the end of the "Ball" Constructor.
See this stack question for more details.
Upvotes: 0
Reputation: 4084
You are setting the size of the frame, but the panel has zero size. You should set the preferred size of the panel, not the size of the frame. Then get the preferred size of the panel to pass to the Ball constructor, and pack the frame before making it visible.
Upvotes: 0