[英]Java graphics PaintComponent problems. Can't seem to find the mistake
I'm making a program for drawing out an Image and it seems I've made a mistake and my program just doesn't want to draw out the image. 我正在制作一个用于绘制图像的程序,看来我犯了一个错误,而我的程序只是不想绘制图像。 Can someone please point out the mistake for mi because i really don't see it.
有人可以指出mi的错误,因为我真的看不到它。
package basic_game_programing;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Practise extends JPanel {
public Image image;
//#####PAINT__FUNCTION#####
public void PaintComponent(Graphics g){
super.paintComponent(g);
ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
image = character.getImage();
g.drawImage(image,20,20,null);
g.fillRect(20, 20, 100, 100);
}
//######MAIN__FUCTION#######
public static void main(String[]args){
Practise panel = new Practise();
//SETTING UP THE FRAME
JFrame frame = new JFrame();
//
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(panel);
//SETTING UP THE PANEL
//
}
} }
You're miscapitalizing paintComponent by using PaintComponent instead (note the first "P"). 您通过使用PaintComponent来错误地分配paintComponent的大小写(请注意第一个“ P”)。
paintComponent
. paintComponent
。 @Override
annotation above the method to let the compiler tell you when you're making this kind of mistake. @Override
批注,可以让编译器在您犯此类错误时告诉您。 protected
not public
. protected
而不是public
。 ImageIO.read(...)
to read in your image, and use resources and relative path within the jar file, rather than use files or ImageIcons. ImageIO.read(...)
读取图像,并在jar文件中使用资源和相对路径,而不是使用文件或ImageIcons。 setVisible(true)
on the JFrame until after adding all components, else some might not show. setVisible(true)
,否则某些组件可能不会显示。 eg, 例如,
public class Practise extends JPanel {
private Image image;
public Practice() {
// read in your image here
image = ImageIO.read(.......); // fill in the ...
}
@Override // use override to have the compiler warn you of mistakes
protected void paintComponent(Graphics g){
super.paintComponent(g);
// never read within a painting method
// ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
// image = character.getImage();
g.drawImage(image, 20, 20, this);
g.fillRect(20, 20, 100, 100);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.