2011-12-11 45 views
0

这里的属性是设置:绘制的形象是一个对象的Java中

我有一类名为雪碧,其中包含一个子类,称为动物是动物世界阵列的一部分图像类。使用完成所有图形的Canvas类,我怎样才能看到图像?

Survival.class - 主类

package survival; 
import javax.swing.*; 

public class Survival { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame("Survival"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(1000, 750); 

     Canvas g = new Canvas(); 
     frame.add(g); 

     frame.setVisible(true); 

     World environment = new World(10, 25, 1000, 750); 
     environment.step(); 
    } 
} 

Canvas.class - 处理所有的绘图

package survival; 
import java.awt.*; 
import javax.swing.*; 

public class Canvas extends JPanel { 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2D = (Graphics2D) g; 
    } 
} 

World.class - 创建动物

package survival; 

public class World { 
    private int width = 1000; 
    private int height = 750; 

    private Animal[] animals; 
    private Plant[] plants; 

    public World(int animalNumber, int plantNumber, int width, int height) { 
     this.width = width; 
     this.height = height; 

     animals = new Animal[animalNumber]; 
     for (int i = 0; i < animalNumber; i++) { 
      animals[i] = new Animal(0,0); 
     } 

     plants = new Plant[plantNumber]; 
     for (int i = 0; i < plantNumber; i++) { 
      plants[i] = new Plant(0,0); 
     } 
    } 

    public void step() { 
     for (int i = 0; i < plants.length; i++) { 
      plants[i].move(); 
     } 
     for (int i = 0; i < animals.length; i++) { 
      animals[i].move(); 
     } 
    }  

    public int getWidth() { 
     return width; 
    } 
    public int getHeight() { 
     return height; 
    } 
} 

Sprite.class - 超类动物,包含图像!

package survival; 

public class Sprite { 
    private Image; //IMAGE I NEED!!! 
    private double x; 
    private double y; 

    public Sprite(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 

    public void move() { 
     x += 1; 
     y += 1; 
    } 

    public void setX(int x) { 
     this.x = x; 
    } 
    public void setY(int y) { 
     this.y = y; 
    } 
} 

Animal.class - 动物的子类

package survival; 

public class Animal extends Sprite { 
    public Animal(int x, int y) { 
     super(x, y); 
    } 
} 
+1

你能更准确?关于你的代码等等? 也许通过提供一个访问者的形象? –

+0

http://sscce.org/我的朋友。 – Jon

回答