简体   繁体   English

Java图形PaintComponent问题。 似乎找不到错误

[英]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”)。

  • So Change PaintComponent to paintComponent . 因此,将PaintComponent更改为paintComponent
  • Use the @Override annotation above the method to let the compiler tell you when you're making this kind of mistake. 使用方法上方的@Override批注,可以让编译器在您犯此类错误时告诉您。
  • Never read an image into a painting method since this slows down a method that needs to be fast, and makes the image be read in over and over when one read is enough. 切勿将图像读取到绘画方法中,因为这会使需要快速的方法变慢,并在一次读取就足够时使图像一遍又一遍地读取。
  • The method should be protected not public . 该方法应被protected而不是public
  • Use 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。
  • Don't call setVisible(true) on the JFrame until after adding all components, else some might not show. 添加所有组件之后,请不要在JFrame上调用setVisible(true) ,否则某些组件可能不会显示。
  • Do read the tutorials as most all of this is well explained there. 一定要阅读教程,因为所有这些都在这里得到了很好的解释。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM