Reputation: 35
I'm a bit new to programming right now and am trying to make Pong in java. However I'm not able to make any graphics show up.
Main Class
public class Pong1 {
public static Frame frame = new Frame();
public static Panel panel = new Panel();
public static void main(String[] args) {
initUI();
}
public static void initUI(){
frame.setLayout(new BorderLayout());
frame.add(BorderLayout.CENTER, panel);
frame.pack();
frame.setVisible(true);
}
public static int getPanelWidth(){
return panel.getWidth();
}
public static int getPanelHeight(){
return panel.getHeight();
}
}
JFrame Class
package pong1;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Frame extends JFrame{
Frame(){
setTitle("Pong");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(true);
setMinimumSize(new Dimension(800,500));
setLocationRelativeTo(null);
}
}
In this final class is the JPanel class where I override and call paintComponent. However, nothing shows up.
public class Panel extends JPanel{
Panel(){
setPreferredSize(new Dimension(800,500));
setMinimumSize(new Dimension(800,500));
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
g2.fillOval(100,100,100,100);
}
I've looked at dozens of other posts and I've tried many different things, and yet nothing shows up. Does anyone know what is going on?
Upvotes: 2
Views: 281
Reputation: 347334
I run you posted code and it seemed to work just fine, however, the reliance on static
is very worrying and is likely to lead into some very interesting problems, best to avoid it.
Also, you should ensure that your UI is created within the context of the Event Dispatching Thread to reduce possible thread violation issues which could cause more problems.
Finally, you don't really need to extend JFrame
, you're not really adding any value to the class and you could use a factory or builder pattern to achieve the same thing with less complexity
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Pong1 {
public static void main(String[] args) {
new Pong1();
}
public Pong1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Panel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Panel extends JPanel {
public Panel() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 500);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("...");
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
g2.fillOval(100, 100, 100, 100);
}
}
}
Oh, and you really should avoid using setPreferred/Minimum/MaximumSize
, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more discussions on the subject
Upvotes: 2