图像不显示。.爪哇岛



我正在尝试使这个程序具有两个沿直线移动的图像,当他们读取帧的末尾时,它们会转向方向......但问题是,图像没有出现在屏幕上idk为什么..这是我的Actor类代码

public class Actor {

    private Image img;
    private int x,y,width,height;
    private final int RIGHT=1,LEFT=-1;
    private byte direction=RIGHT;
    public Actor(Image img, int x,int  y, int width, int height){
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
    }
    public Image getImg() {
        return img;
    }
    public void setImg(Image img) {
        this.img = img;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public void movement(int frameWidth){
        setX(getX()+direction);
        if(getX()<0) direction= RIGHT;
        if(getX()>(frameWidth-width)) direction= LEFT;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}

这是我的主要课程:

public class game extends JFrame implements Runnable{
    private int framewidth=1000;
    private int frameheight=1500;
    Image image= new ImageIcon("pics/buffy.png").getImage();
    Image image2= new ImageIcon("pics/buffythelayer.jpg").getImage();
    private Thread thread;
    private int picX=100;
    private int c=1;
    private int xSpeed=3;
    private int xFly=1;
    private int yFly=100;
    private Actor greenCar,pinkCar;
    public game(){
        setBounds(100,100,framewidth,frameheight);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        thread= new Thread(this);
        thread.start();
        greenCar=new Actor(image,30,70,98,40);
        pinkCar=new Actor(image2,400,70,98,40);
    }
    public void paint(Graphics g){
        g.fillRect(xFly, yFly, 10, 10);
        g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), null);
        g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), null);
        if(c==2){
        g.setColor(Color.CYAN);
        g.fillOval(100, 200, 150, 200);
        }
    }
    public static void main(String[] args) {
        new game();
    }
    public void run() {
        while(true)
        {
            xFly++;
            greenCar.movement(framewidth);
            pinkCar.movement(framewidth);
            /*if(picX>280){
                xSpeed=-xSpeed;
                picX=picX+xSpeed;
                c=2;
            }
            if(picX>=100){
                xSpeed=3;
            picX=picX+xSpeed;

            }*/
            repaint();
            try{
                thread.sleep(13);
            }
            catch(InterruptedException e){
            }
        }
        }
}
我想

我看到了问题。运行下面的代码时,将最后一个值 ImageObserver 设置为 null。

g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), null);
g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), null);

相反,你应该这样写:

g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), this);
g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), this);

因此,JFrame 是在图像加载时收到通知的对象,并且可以在屏幕上正确绘制。

如果不是这种情况,那么您应该将super.paint(g(添加到您的绘画方法中。

您的 paint(g( 方法应如下所示:

public void paint(Graphics g){
    super.paint(g);
    g.fillRect(xFly, yFly, 10, 10);
    g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), this);
    g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), this);
    if(c==2){
    g.setColor(Color.CYAN);
    g.fillOval(100, 200, 150, 200);
    }
}

我希望这有所帮助。

问题是你在构造汽车对象之前运行线程,所以

首先创建对象,运行线程

    greenCar=new Actor(image,30,70,98,40);
    pinkCar=new Actor(image2,400,70,98,40);
    thread.start();

你忘记了在Actor构造函数中设置图像

    public Actor(Image img, int x,int  y, int width, int height){
    this.x=x;
    this.y=y;
    this.width=width;
    this.height=height;
    this.img = img;
}

最新更新