图像不显示.. Java

时间:2017-03-19 11:49:20

标签: java arrays eclipse image swing

我试图让这个程序有两个图像在直线上移动,当他们读到框架结束时,他们转向他们的方向......但事实是,图像不会出现在屏幕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){

            }

        }
        }

}

2 个答案:

答案 0 :(得分:1)

我想我看到了问题。运行下面的代码时,将最后一个值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是在图像加载时通知的对象,可以在屏幕上正确绘制。

如果不是这样,那么你应该在你的paint方法中添加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);
    }
}

我希望这会有所帮助。

答案 1 :(得分:0)

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

首先创建对象,运行线程

json_request_only = {
  defaults:    { format: :json }, 
  constraints: { format: :json }
}

ajax_server = {
  only: [:create, :destroy]
}.merge(json_request_only)

Rails.application.routes.draw do

  root to: 'home#index'

  get 'contact/us/:section', to: 'home#contactus', as: 'contact_us'

  resources :uploaded_files, ajax_server

  # only to create/delete shopping carts and only excepts JSON format
  resources :shopping_carts, ajax_server

  resources :item_types, :path => "category", json_request_only do
    member do 
      delete :delete_image
      delete :archive
      patch :recover
    end
  end
end

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

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