Pergunta

Eu estou aprendendo sobre o buffer duplo e o livro que eu recebi do meu professor diz-me a escrever o que escrevi.Mas quando o java tenta fazer com que os gráficos do objeto da imagem, esta falha.

frame = Imagem
motor = Gráficos

Erro

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)

Image 1

Image 2

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;
    }
}

}
Foi útil?

Solução

O que você está chamando getGraphics() em um nulo Imagem variável.Se você olhar para a API do Componente createImage() você vai ver por que isso é assim:

retorna um fora-da-tela drawable imagem, que pode ser usado para buffer duplo. O valor de retorno pode ser null se o componente não é visualizável.Isso sempre vai acontecer se GraphicsEnvironment.isHeadless() retorna true.

Motor não é exibido ainda em seu construtor.O GUI deve ser processado primeiro chamando pack() ou setVisible(true) na janela de nível superior antes de este JPanel será exibida.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top