Setting up a JPanel in a JFrame?
So, I've been working to redo my code so that the painting is all done in a JPanel instead of a JFrame so I can do some very much needed image buffering.
I've scoured StackOverflow and I've googled my fingers raw and I thought I had it set up right, but it's not working. I just get a blank white screen and some error script in the terminal. Any help is appreciated. Here is the code
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
public class RacerDoom extends JFrame {
private JPanel panel;
final int WIDTH = 900, HEIGHT = 640;
int counter = 0;
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*8,WIDTH,HEIGHT);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*4),(HEIGHT/9)*4);
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);
Rectangle finishtop = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);
//Starting lines
Rectangle startO = new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
public RacerDoom() {
//create JFrame
super("Racer Doom Squared");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().add(panel);
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
this.getContentPane().add(panel);
//set up Game countdown timer
final Timer timer=new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(counter>=10) {
((Timer)e.getSource()).stop();
}
else{
counter++;
}
System.out.println(counter);
}
});
//start timer
timer.start();
}
private class MainPanel extends JPanel {
public MainPanel() {
super();
}
//draw graphics
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,WIDTH,HEIGHT);
//boundaries
g.setColor(Color.LIGHT_GRAY);
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
//start line
g.setColor(Color.WHITE);
g.fillRect(startO.x,startO.y,startO.width,startO.height);
//finish line
g.setColor(Color.CYAN);
g.fillRect(finishtop.x,finishtop.y,finishtop.width,finishtop.height);
//p1
g.setColor(Color.BLUE);
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//HUD
g.setColor(Color.WHITE);
Font f = new Font("Monospaced", Font.BOLD, 24);
g.setFont(f);
g.drawString("Boosts: "+p1Boost,(WIDTH-(WIDTH/6)),(HEIGHT-(int)(HEIGHT/1.1)));
g.drawString("Time: "+(10-counter),540,100);
}
}
public static void main (String [] args) {
new RacerDoom();
}
}
The error script:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at RacerDoom.(RacerDoom.java:46)
at RacerDoom.main(RacerDoom.java:232)
I'm quite sure I'm just an idiot and the answer is probably giving me the middle finger in the error message, but it's still G开发者_JAVA百科reek to me. But even (especially?) idiots need help.
Your error is pretty simple:
getContentPane().add(panel);
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
You're adding the panel to the widow before the panel is actually created. Try rearranging it like this:
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
getContentPane().add(panel);
I believe line 46 is:
getContentPane().add(panel);
At that point, you haven't created the panel yet, so it is null. You can't add a null component to a container. Remove that line; your later this.getContentPane().add(panel);
is all you need.
The NullPointerException occurs because you try to add not initialized variable panel:
getContentPane().add(panel);
You have to first initialize this variable, and than add it to getContentPane()
精彩评论