私のコードはこれです:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.Timer;
public class chromeNPlayerScreen extends JFrame implements ActionListener{
DrawScreen dPnl = new DrawScreen();
public void actionPerformed(ActionEvent e){
}
public void main(String[ ] args){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(dPnl);
this.setSize(600,600);;
this.setVisible(true);
this.setResizable(false);
this.setLocation(200, 200);
}
}
しかし、私がそれを実行すると.....
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
なぜこれがうまくいかないのか、誰かが私に説明してもらえますか?
DrawScreenコードは
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
public class DrawScreen extends JPanel {
String picPath = "pictures/";
ClassLoader cl = pokemonChromeNewPlayerScreen.class.getClassLoader();
URL imgURL = cl.getResource(picPath+"welcomeBG.png"),imgURL2 = cl.getResource(picPath+"dialogBox.png"),
imgURL3 = cl.getResource(picPath+"Professor.png");
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG, imgDialog, imgProfessor;
public void imgImport(){
imgBG = tk.createImage(imgURL);
imgDialog = tk.createImage(imgURL2);
imgProfessor = tk.createImage(imgURL3);
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2 = (Graphics2D)g;
for(int x=0;x<=600;x+=25){
g2.drawLine(x,0,x,600);
g2.drawString(""+x,x+5,20);
}
for(int y=0;y<=600;y+=25){
g2.drawLine(0,y,600,y);
g2.drawString(" "+y,0,y+20);
}
}
}
これは DrawScreen のコードです。グリッドをドラッグするだけで済みますが、それは、開始したばかりで、さまざまな位置の x、y 値が必要だったためです。