使用Java中的paintingComponent时遇到问题

时间:2013-04-10 03:44:41

标签: java swing jframe paintcomponent jcomponent

Java新手。做一个项目,要求我使用JFrame在舞台上显示我的Actors。还是真的很新,所以我不确定我在做什么。我知道你不能直接绘制对象......它们必须首先成为组件。我很难找到显示这种转变的信息。如果有人可以指导我如何做到这一点,那将是一个很大的帮助。

先谢谢。

Actor.java

 import java.awt.Color;
 import java.awt.Point;
 import java.awt.geom.Point2D;
 import javax.swing.*;



/** The Actor class is an abstract class that must be subclassed to implement 
 * various game characters.  Different game characters have different attributes
 * and powers but all characters share a common set of attributes that are 
 * supplied by this class.  The common attributes are: 
 * 
 * Strength (0-100) Full Stength is 100
 * Speed (0-100) Maximum speed is 100
 * Health (0-100) Maximum health is 100
 * 
 * These game values are generated randomly when a new Actor instance is created.
 * However, game values can also be supplied by the user via keyboard input.   
 */

    public abstract class  Actor {

   // Instance Variables

   private String sName;
   private int nStrength;
   private int nSpeed;
   private int nHealthPoints;
   protected Point2D location;
   private Army myArmy;

   // Static variables 

   private static int nSequenceCount = 0;

   // Constants

final static int MIN_STRENGTH = 0;
final static int MAX_STRENGTH = 100;
final static int MIN_SPEED = 0;
final static int MAX_SPEED = 100;
final static int MIN_HEALTH = 0;
final static int MAX_HEALTH = 100;
final static int MIN_LOC = 0;
final static int MAX_LOC = 100;

/** The Actor constructor invokes the init method to establish initial 
 * game values for an Actor.  These values include the hobbit's name, 
 * strength, speed and health.
 */
public Actor() {
    nSequenceCount++;
    init();

}



/** The set method prompts the user to enter game values for an Actor.  These values include
 *  the wizard's name, strength, speed and health.
 */ 
public void init() {
    // Assign random Gaussian values to the remaining attributes
    this.sName ="Actor" + nSequenceCount;
    this.nHealthPoints = ((int) Math.round(SingletonRandom.instance.getNormalDistribution(MIN_HEALTH, MAX_HEALTH, 1.0 ) ));
    this.nSpeed = ((int) Math.round(SingletonRandom.instance.getNormalDistribution(MIN_SPEED, MAX_SPEED, 1.0) ));
    this.nStrength = ((int) Math.round(SingletonRandom.instance.getNormalDistribution(MIN_STRENGTH, MAX_STRENGTH, 1.0) ));
    int nX = ((int) Math.round(SingletonRandom.instance.getNormalDistribution(MIN_LOC,MAX_LOC, 1.0)));
    int nY = ((int) Math.round(SingletonRandom.instance.getNormalDistribution(MIN_LOC,MAX_LOC, 1.0)));
    location = new Point(nX,nY);
}



/** The toString method below displays game values for an actor. 
 */ 
@Override
public String toString() {
    String myColor;
    if(this.myArmy==null){
        myColor = "Colour not assigned";
    }else{
        myColor = this.myArmy.getColor().toString();
    }
    return String.format("Actor:%7s\tHealth:%3d\tSpeed:%3d\tStrength:%3d\tLocation:%7s Color:%7s", sName, nHealthPoints, nSpeed, nStrength, location, myColor); 
}



/** The set method prompts the user to enter game values for an actor.  These values include
 *  the wizard's name, strength, speed and health.
 */
public void set() {
    this.sName = InputGUI.getStringGUI("Please Enter the name: ");
    this.nStrength = InputGUI.getIntGUI("Please Enter the Strength: ",MIN_STRENGTH, MAX_STRENGTH);
    this.nSpeed = InputGUI.getIntGUI("Please Enter the Speed: ",MIN_SPEED, MAX_SPEED);
    this.nHealthPoints = InputGUI.getIntGUI("Please Enter the Health Points: ",MIN_HEALTH, MAX_HEALTH);
}


/** Return the actor's name. 
 */ 
public String getName(){
    return this.sName;
}



/** Return the actor's strength. 
 */     
public int getStrength(){
    return this.nStrength;
}



/** Return the actor's speed. 
 */     
public int  getSpeed(){
    return this.nSpeed;
}


public Army  getArmy(){
    return this.myArmy;
}

public void setArmy(Army myArmy){
    this.myArmy = myArmy;
}

/** Return the actor's health. 
 */     
public int getHealth(){
    return this.nHealthPoints;
}

 }

Army.java

   import java.util.ArrayList; 
   import java.awt.Color;
   import javax.swing.JComponent; 
   import java.awt.Dimension;
   import java.awt.Graphics;
   import javax.swing.Timer;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;


   public class Army extends JComponent{

static int nActor;
String sName;
ArrayList<Actor> actors;
Color color;


private Timer t;

public Army(String sName, Color color){
    this.sName = sName;
    this.actors = new ArrayList<Actor>();
    this.color = color;
    ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    };
    t = new Timer(1000 / 30 /* frame rate */, al);
    t.start();  
}

@Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g); 
   }

@Override
public Dimension getPreferredSize() {
    return new Dimension(500, 500); 
}







public Army(String sName, int actorCount, Color color){
    this.sName = sName;     
    this.actors = new ArrayList<Actor>();
    makeArmy(actorCount);
    this.color = color;

    ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    };
    t = new Timer(1000 / 30 /* frame rate */, al);
    t.start();  
}

public void makeArmy(int actorCount){   
    for(int i=0;i<actorCount; i++) {
        Actor actor = ActorFactory.createNewActor(true);
        this.actors.add(actor);
    }
}

public boolean removeActor(Actor actor){
    return actors.remove(actor);
}

public void addActor(Actor actor){
    actors.add(actor);
    actor.setArmy(this);
}



public Actor setActor(int pos){
    Actor actor;
    if (pos < 0 || pos >= actors.size() ) {
        actor = null;
    } else {
        actor = actors.get(pos); 
        actor.set(); 
    }
    return actor;
}



public void displayAll(){
    for(int i=0;i<actors.size(); i++) {
        Actor actor = actors.get(i);
        System.out.println(actor);
    }
}
 public ArrayList<Actor> getActors(){
     return this.actors;
 }

@Override
public String toString() {
    return String.format("Actor:%7s\tColor:%7s", sName, color);
}

public Color getColor(){
    return this.color;
}


public static void main(String[] args) {
    Army army = new Army("Dexter's Army",50,Color.WHITE);
    army.displayAll();

}



  }

TestActor.java

  import java.util.ArrayList; 
  import java.awt.Color;
  import javax.swing.JFrame;

  public class TestActor  {

     public void test1(){

        Army army = new Army("Dexter's Army",1000,Color.WHITE);
        army.displayAll();

        JFrame frame = new JFrame("Battlefield Simulator");
        frame.add(army);
        frame.pack();
        frame.setVisible(true);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



public static void main(String[] args) {
    TestActor tester = new TestActor();
    tester.test1();
}
   }

0 个答案:

没有答案