JavaでWindowsプログラミングを学ぼうとしていて、フレームに画像を表示したい.ここに問題のコードがあります:
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("hello world");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
Graphics graph = frame.getGraphics();
BufferedImage image = ImageIO.read(new File("data/image.jpg"));
graph.drawImage(image, 0,0,frame);
frame.pack();
frame.setVisible(true);
}
Component クラスをサブクラス化し、paint メソッドで Graphics.DrawImage メソッドを呼び出す成功例をいくつか見てきました。フレームに関連付けられた Graphics オブジェクトを取得して、画像を直接描画することはできませんか?