It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
7年前关闭。
我正在学习有关双缓冲的知识,而我从老师那里得到的书告诉我写我写的东西。但是,当Java尝试从图像中获取图形对象时,它就会崩溃。
框架=图片
引擎=图形
错误
Game.java
Engine.java
7年前关闭。
我正在学习有关双缓冲的知识,而我从老师那里得到的书告诉我写我写的东西。但是,当Java尝试从图像中获取图形对象时,它就会崩溃。
框架=图片
引擎=图形
错误
Exception in thread "main" java.lang.NullPointerException
at Engine.<init>(Engine.java:30)
at Game.<init>(Game.java:35)
at Game.main(Game.java:22)
Game.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.util.Timer;
public class Game implements ActionListener {
private static Game hosmos2;
private JFrame frmMain;
private AntiCheat holyanticheat;
private Dimension screen;
private Engine holyengine;
private int sx, sy;
private Timer timCheat;
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
hosmos2 = new Game();
}
private Game() throws FileNotFoundException, InterruptedException {
frmMain = new JFrame("Hosmos 2");
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMain.setSize(1366, 768);
screen = Toolkit.getDefaultToolkit().getScreenSize();
sx = (int) ((screen.getWidth() - frmMain.getWidth()) / 2);
sy = (int) ((screen.getHeight() - frmMain.getHeight()) / 2);
frmMain.setLocation(sx, sy);
holyanticheat = new AntiCheat();
holyengine = new Engine();
timCheat = new Timer();
frmMain.setVisible(true);
timCheat.schedule(holyanticheat, 500);
Thread.sleep(1000);
timCheat.cancel();
}
public void actionPerformed(ActionEvent e) {
}
}
Engine.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
public class Engine extends JPanel {
private boolean countdown, quality;
private Font text, text2;
private Graphics engine;
private Image frame;
private ImageIcon background, button1, button21, button22, button3, button4, button51, button52, header;
private int weedbackground, fps, fps2;
public Rectangle box1, box2, box3, box4, box5;
public static void main(String[] args) {
}
public Engine() {
this.setBackground(Color.black);
frame = createImage(getSize().width, getSize().height);
text = new Font("Microsoft Sans Serif", Font.BOLD, 24);
text2 = new Font("Microsoft Sans Serif", Font.BOLD, 8);
engine = frame.getGraphics(); // Error
background = new ImageIcon("textures/background.png");
//background = background.GetThumbnailImage(source.ClientSize.Width, source.ClientSize.Height, null, IntPtr.Zero);
button1 = new ImageIcon("textures/button 1.png");
button21 = new ImageIcon("textures/button 2-1.png");
button22 = new ImageIcon("textures/button 2-2.png");
button3 = new ImageIcon("textures/button 3.png");
button4 = new ImageIcon("textures/button 4.png");
button51 = new ImageIcon("textures/button 5-1.png");
button52 = new ImageIcon("textures/button 5-2.png");
header = new ImageIcon("textures/header.png");
box1 = new Rectangle(20, 20, 87, 37);
box2 = new Rectangle(20, 60, 96, 37);
box3 = new Rectangle(20, 103, 182, 43);
box4 = new Rectangle(20, 146, 83, 43);
box5 = new Rectangle(20, 189, 206, 43);
}
public Rectangle GetBox1() {
return box1;
}
public Rectangle GetBox2() {
return box2;
}
public Rectangle GetBox3() {
return box3;
}
public Rectangle GetBox4() {
return box4;
}
public Rectangle GetBox5() {
return box5;
}
public void FPS()
{
fps2 = fps;
fps = 0;
}
public void Quality(boolean quality2)
{
quality = quality2;
}
public void Render(boolean music, boolean effect, int time, int spawned, int points, int cracktime, int crackend, int shieldtime, int shieldend, int weedtime, int weedend, ImageIcon malcolm, int malcolmX, int malcolmY, int item, ImageIcon crack, int crackX, int crackY, ImageIcon nuke, int nukeX, int nukeY, ImageIcon shield, int shieldX, int shieldY, ImageIcon skull, int skullX, int skullY, ImageIcon trollface, int trollfaceX, int trollfaceY, ImageIcon weed, int weedX, int weedY, LinkedList<Hosmin> hosmins) {
engine = frame.getGraphics();
super.paintComponent(engine);
if (effect == false) {
if (quality == false) {
background.paintIcon(this, engine, 0, 0);
}
else {
this.setBackground(Color.black);
}
}
else {
if (weedbackground == 1) {
this.setBackground(Color.green);
weedbackground++;
}
else if (weedbackground == 2) {
this.setBackground(Color.yellow);
weedbackground++;
}
else if (weedbackground == 3) {
this.setBackground(Color.red);
weedbackground = 1;
}
}
header.paintIcon(this, engine, 547, 10);
button1.paintIcon(this, engine, 20, 20);
if (music == false) {
button21.paintIcon(this, engine, 20, 60);
}
else {
button22.paintIcon(this, engine, 20, 60);
}
button3.paintIcon(this, engine, 20, 103);
button4.paintIcon(this, engine, 20, 146);
if (quality == false) {
button51.paintIcon(this, engine, 20, 189);
}
else {
button52.paintIcon(this, engine, 20, 189);
}
engine.setFont(text2);
engine.setColor(Color.green);
engine.drawString("FPS: " + fps2, 1293, 9);
engine.setFont(text);
engine.setColor(Color.red);
engine.drawString("Time: " + time, 1178, 22);
engine.drawString("Hosmins: " + spawned, 1124, 59);
engine.drawString("Points: " + points, 1160, 96);
engine.setFont(text2);
engine.setColor(Color.green);
engine.drawString("Developer: HolyDuFF", 1229, 708);
malcolm.paintIcon(this, engine, malcolmX, malcolmY);
if (countdown == true) {
if (crackend - cracktime == 0) {
countdown = false;
}
else if (cracktime >= 0) {
engine.setFont(text);
engine.setColor(Color.red);
crackend = crackend - cracktime;
if (crackend != 10) {
engine.drawString("" + crackend, malcolmX + 17, malcolmY - 40);
}
else {
engine.drawString("" + crackend, malcolmX + 7, malcolmY - 40);
}
}
else if (shieldtime >= 0) {
engine.setFont(text);
engine.setColor(Color.red);
shieldend = shieldend - shieldtime;
if (shieldend != 10) {
engine.drawString("" + shieldend, malcolmX + 17, malcolmY - 40);
}
else {
engine.drawString("" + shieldend, malcolmX + 7, malcolmY - 40);
}
}
else if (weedtime >= 0) {
engine.setFont(text);
engine.setColor(Color.blue);
weedend = weedend - weedtime;
if (weedend != 10) {
engine.drawString("" + weedend, malcolmX + 17, malcolmY - 40);
}
else {
engine.drawString("" + weedend, malcolmX + 7, malcolmY - 40);
}
}
}
for (int i = 0; i < hosmins.size(); i++) {
Hosmin temp = hosmins.get(i);
temp.Draw().paintIcon(this, engine, temp.GetX(), temp.GetY());
}
if (item == 1) {
crack.paintIcon(this, engine, crackX, crackY);
}
else if (item == 2) {
nuke.paintIcon(this, engine, nukeX, nukeY);
}
else if (item == 3) {
shield.paintIcon(this, engine, shieldX, shieldY);
}
else if (item == 4) {
skull.paintIcon(this, engine, skullX, skullY);
}
else if (item == 5) {
trollface.paintIcon(this, engine, trollfaceX, trollfaceY);
}
else if (item == 6) {
weed.paintIcon(this, engine, weedX, weedY);
}
engine.dispose();
repaint();
fps++;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(frame, 0, 0, this);
}
public void SetBackground(int background2) {
weedbackground = background2;
}
public void SetCountdown(boolean countdown2) {
countdown = countdown2;
}
public void SetBox2(int mode) {
if (mode == 1) {
box2.width = 96;
box2.height = 37;
}
else {
box2.width = 76;
box2.height = 43;
}
}
public void SetBox5(int mode) {
if (mode == 1) {
box5.width = 206;
}
else {
box5.width = 213;
}
}
}
最佳答案
您正在对空的Image变量调用getGraphics()
。如果您查看Component createImage()
的API,就会明白为什么会这样:
返回屏幕外的可绘制图像,该图像可用于双缓冲。如果该组件不可显示,则返回值可以为null。如果GraphicsEnvironment.isHeadless()返回true,这将始终发生。
引擎尚未在其构造函数中显示。必须先在顶级窗口上调用pack()
或setVisible(true)
呈现GUI,然后才能显示此JPanel。