2回定義imageしました...
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
これは本質的に、paintメソッドに到達するまでにnull、インスタンス変数を初期化していないことを意味します。
もう 1 つの問題は、静的参照から画像を読み込もうとしているが、imageとして宣言されていないという事実ですstatic。このロジックをコンストラクターまたはインスタンス メソッドに移動することをお勧めします。
JAppletに追加するときはコンテナとして使用しないでください。JFrameのようなものを使用する方が適切ですJPanel。コンテナに物を追加するときに役立ちます。
YOU MUST CALL super.paint(g) ...実際には、やのような最上位コンテナのメソッドをオーバーライドしないでください。のようなものを使用して、代わりにメソッドをオーバーライドしてください。最上位のコンテナはダブル バッファリングされません。paintJFrameJAppletJPanelpaintComponent
メソッドは多くのpaint重要な作業を行い、使いやすくなっていますJComponent#paintComponent...しかし、呼び出すことを忘れないでくださいsuper.paintComponent
更新しました
image使用するコンテキスト内で定義する必要があります。
imageを のインスタンス フィールドとして宣言したため、参照するGraphicsMovement2には のインスタンスが必要にGraphicsMovement2なります。
ただし、mainメソッドである ではstatic、 という名前の変数も宣言していますimage。
のpaintメソッドはGraphicsMovement2、で宣言した変数を見ることができずmain、インスタンス フィールド (これは ですnull) だけを見ることができます。
この問題を解決するには、イメージのロードを のインスタンスのコンテキストに移動する必要がありますGraphicsMovement2。これは (コンテキスト内で) 最善の方法ですが、イメージのロードを のコンストラクタに移動する必要があります。GraphicsMovement2
public GraphicsMovement2() {
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
}
以下の 2 つの例では、同じ結果が生成されます...

簡単な方法
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
public ImagePane() {
setLayout(new BorderLayout());
ImageIcon icon = null;
try {
icon = new ImageIcon(ImageIO.read(new File("/path/to/your/image")));
} catch (Exception e) {
e.printStackTrace();
}
add(new JLabel(icon));
}
}
}
ハードウェイ
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage background;
public ImagePane() {
try {
background = ImageIO.read(new File("/path/to/your/image"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
時間をかけてチュートリアルを読む