I did a small test according to a problem in another program I am writing. I need to draw an image to the screen, but it does not work. However, I am able to draw rectangles to the screen, I do not understand
Code:
Main class:
package test;
import javax.swing.JFrame;
public class Main extends JFrame{
private static final long serialVersionUID = 1L;
final static int WW = 800;
final static int WH = 600;
public Main(){
setSize(WW,WH);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Space Game");
add(new testClass());
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
testClass class:
package test;
import java.awt.*;
import javax.swing.*;
public class testClass extends JPanel{
private Image image;
public testClass(){
ImageIcon image = new ImageIcon("spaceship.png");
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(50,50,50,50);
g.drawImage(image,0,0,null);
}
}
Please help and explain why it does not work. Sorry for bad English, any help is appreaciated :) *I am sorry if my question is newbie, I am new in programming...