未调用paintComponent,无法使图像变量起作用

时间:2012-08-10 23:13:35

标签: java image swing paintcomponent japplet

所以我知道我很喜欢编程,但如果any1可以给我一些代码帮助我会很感激,我不知道为什么我会收到这个错误

无法找到符号

  g.drawImage(movPic2, 35, 515, 200, 200,this);                 ^
  symbol:   variable movPic2
  location: class MovieDis

如果any1可以给我一个代码片段示例,我只能用语言理解。 厌倦了这个程序,只是想完成它。

import java.awt.*;
import javax.swing.*;
import javax.swing.JComponent.*;

public class Movie extends JApplet {

    private String movName1;
    private String director1;
    private int yearMade1;
    private Image movPic1;
    private String movName2;
    private String director2;
    private int yearMade2;
    private Image movPic2;
    private String movName3;
    private String director3;
    private int yearMade3;
    private Image movPic3;
    private String movName4;
    private String director4;
    private int yearMade4;
    private Image movPic4;

    public void init() {
        MovieDis goo = new MovieDis(movPic1, movPic2, movPic3, movPic4);
        goo.setBounds(0, 0, 750, 500);
        add(goo);
    }
}

class MovieDis extends JComponent {

    private String movName1;
    private String director1;
    private int yearMade1;
    private Image movPic1;
    private String movName2;
    private String director2;
    private int yearMade2;
    private Image movPic2;
    private String movName3;
    private String director3;
    private int yearMade3;
    private Image movPic3;
    private String movName4;
    private String director4;
    private int yearMade4;
    private Image movPic4;

    public MovieDis(Image movPic1, Image movPic2, Image movPic3, Image movPic4) {
        setBackground(Color.black);
        movPic1 = Toolkit.getDefaultToolkit().createImage("Shaw.jpg");
        movPic2 = Toolkit.getDefaultToolkit().createImage("dances.jpg");
        movPic3 = Toolkit.getDefaultToolkit().getImage("Inception.jpg");
        movPic4 = Toolkit.getDefaultToolkit().getImage("Cuckoo.jpg");
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.fillRect(0, 0, 750, 500);
        g.drawImage(movPic1, 35, 35, 200, 200, this);
        g.drawImage(movPic2, 35, 515, 200, 200, this);
        g.drawImage(movPic3, 265, 35, 200, 200, this);
        g.drawImage(movPic4, 35, 515, 200, 200, this);
    }
}

1 个答案:

答案 0 :(得分:2)

变量movPic1(和2-4)不会存储在MovieDis类中的任何位置。这就是paintComponent()方法抱怨的原因。您需要将这些添加到MovieDis,然后在类构造函数中分配它们。