【问题标题】:Images aren't displaying.. Java图像未显示.. Java
【发布时间】:2017-08-10 16:57:27
【问题描述】:

我正在尝试制作这个程序,它有两个沿直线移动的图像,当它们读取帧的结尾时,它们会改变方向......但问题是,图像没有出现在屏幕上我知道为什么.. 这是我的 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){

            }

        }
        }

}

【问题讨论】:

  • setVisible(true); 移动到构造函数的末尾。这应该是你做的最后一件事:一旦一切都设置好,你就让框架可见。下一步是学习How to do custom painting properly
  • Image image= new ImageIcon("pics/buffy.png").getImage(); 你研究过这个吗?同样的问题每天都会出现。在忙碌的一天中两次或三次。
  • while(true), Thread.sleep() 覆盖paint(),不调用super.paint(g),调用setBounds() 不尊重EDT...学习如何使用Swing Timer,如何进行自定义绘画并使用布局管理器以及如何将您的程序放在 EDT 上

标签: java arrays eclipse image swing


【解决方案1】:

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

先创建对象,再运行线程

    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;

}

【讨论】:

  • 只有一张图片出现..第二张没有
  • 检查您的文件名、路径、扩展名
【解决方案2】:

我想我看到了问题所在。当您运行下面的代码时,您将最后一个值 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);
    }
}

我希望这会有所帮助。

【讨论】:

  • 好吧,只有一张照片出现..第二张没有:/
猜你喜欢
  • 2011-04-02
  • 2011-11-19
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多