How to add moving sprites to JFrame

时间:2015-10-29 15:48:53

标签: java jframe sprite

I'm trying to add sprites to a JFrame that move or 'fidget' in place(meaning they just move up a pixel and then down a pixel), almost like the Pokemon sprites in your party menu move, but just with a single sprite. I got the code to work when I was just running it as a JApplet, but my larger game is built in a JFrame with multiple JPanels. When I tried to incorporate the working code into my larger game, it stopped working. Any help would be greatly appreciated. This is not for school, just my own personal game.

This begins the JApplet that works.

public class SpriteAnimation extends JApplet{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 150;
public static final int HEIGHT = 50;

private PaintSurface canvas;

public void init(){
    this.setSize(WIDTH, HEIGHT);
    canvas = new PaintSurface();
    this.add(canvas, BorderLayout.CENTER);
    Timer t = new Timer(250, e -> {canvas.repaint();});
    t.start();
}
}


public class PaintSurface extends JComponent{

private static final long serialVersionUID = 1L;
ArrayList<Sprite> sprites = new ArrayList<Sprite>();

public PaintSurface(){
    sprites.add(new Hero(0));
}
@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    doDrawing(g);
    Toolkit.getDefaultToolkit().sync();
}
public void doDrawing(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for(Sprite h : sprites){
        g2.drawImage(h.getImage(), h.getX(), h.getY(), null);
        h.move();
    }
}
}


public class Sprite {
private int x, y, fidget;
private Image image;
boolean flip = true;

public Sprite(String s, int fidget, int startingX) {
    this.fidget = fidget;
    ImageIcon icon = new ImageIcon(s);
    image = icon.getImage();
    x = startingX; y = 15;
}
public void move() {
    y = 10;
    if(!flip){
        y += fidget;
        flip = !flip;
    }
    else{
        y -= fidget;
        flip = !flip;
    }
}
public Image getImage(){
    return image;
}
public int getX(){
    return x;
}
public int getY(){
    return y;
}
}


public class Hero extends Sprite {
private int startingX;

public Hero(int x){
    super("Hero.png", 1, x);
    this.startingX = x;
}
public int getStartingX() {
    return startingX;
}
public void setStartingX(int startingX) {
    this.startingX = startingX;
}
}

This is where I'd like to add the working Sprites to(Specifically the battlefield JPanel).

public class BattleFrame extends JFrame {
private static final long serialVersionUID = 1L;
static ArrayList<JButton> buttonsActions = new ArrayList<JButton>();
static ArrayList<JButton> buttonsConfirmation = new ArrayList<JButton>();
static ArrayList<JButton> buttonsAlly = new ArrayList<JButton>();
static ArrayList<JButton> buttonsEnemy = new ArrayList<JButton>();
static ArrayList<ArrayList<JButton>> buttons = new ArrayList<ArrayList<JButton>>();
static Box boxAllies = Box.createVerticalBox();
static int movesLeft = 3;
static JScrollPane logs;
static JTextArea log;

public BattleFrame(){
    this.setSize(1200, 700);
    this.setTitle("The Last Battle");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

    JPanel battleField = new JPanel();
    battleField.setBorder(BorderFactory.createTitledBorder("Battle Field"));
    battleField.setLayout(new BorderLayout());

    JPanel allies = new JPanel();
    allies.setBorder(BorderFactory.createTitledBorder("Armies of Light"));
    boxAllies.add(new JLabel("Moves left: " + movesLeft));

    JButton channellers = new JButton("Channellers");
    channellers.setName("Channellers"); buttonsAlly.add(channellers);
    channellers.addActionListener(e -> ButtonClick.clicked(channellers));
    JButton archers = new JButton("Archers");
    archers.setName("Archers"); buttonsAlly.add(archers);
    archers.addActionListener(e -> ButtonClick.clicked(archers));
    JButton infantry = new JButton("Infantry");
    infantry.setName("Infantry"); buttonsAlly.add(infantry);
    infantry.addActionListener(e -> ButtonClick.clicked(infantry));

    boxAllies.add(Box.createVerticalStrut(150)); boxAllies.add(channellers);
    boxAllies.add(Box.createVerticalStrut(40));  boxAllies.add(infantry);
    boxAllies.add(Box.createVerticalStrut(40));  boxAllies.add(archers);
    boxAllies.add(Box.createVerticalStrut(40));
    allies.add(boxAllies);


    JPanel enemies = new JPanel();
    enemies.setBorder(BorderFactory.createTitledBorder("The Shadow"));
    Box boxEnemies = Box.createVerticalBox();

    JButton enemyChannellers = new JButton("Dreadlords");
    enemyChannellers.setName("Dreadlords"); buttonsEnemy.add(enemyChannellers);
    enemyChannellers.addActionListener(e -> ButtonClick.clicked(enemyChannellers));
    JButton enemyArchers = new JButton("Enemy Archers");
    enemyArchers.setName("Enemy Archers"); buttonsEnemy.add(enemyArchers);
    enemyArchers.addActionListener(e -> ButtonClick.clicked(enemyArchers));
    JButton enemyInfantry = new JButton("Enemy Infantry");
    enemyInfantry.setName("Enemy Infantry"); buttonsEnemy.add(enemyInfantry);
    enemyInfantry.addActionListener(e -> ButtonClick.clicked(enemyInfantry));

    boxEnemies.add(Box.createVerticalStrut(150)); boxEnemies.add(enemyChannellers);
    boxEnemies.add(Box.createVerticalStrut(40));  boxEnemies.add(enemyInfantry);
    boxEnemies.add(Box.createVerticalStrut(40));  boxEnemies.add(enemyArchers);
    boxEnemies.add(Box.createVerticalStrut(40));
    enemies.add(boxEnemies);

    JPanel messagePanel = new JPanel();
    Box boxMessage = Box.createHorizontalBox();
    log = new JTextArea("This is where battle updates will appear.", 36, 32);
    logs = new JScrollPane(log, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    log.setWrapStyleWord(true);
    log.setAlignmentY(20); log.setEditable(false);
    logs.setSize(log.getWidth(), log.getHeight());
    boxMessage.add(logs);       boxMessage.add(Box.createVerticalStrut(100));
    messagePanel.add(boxMessage);


    JPanel actionPanel = new JPanel();
    actionPanel.setBorder(BorderFactory.createTitledBorder("Available Actions"));
    Box boxActions = Box.createHorizontalBox();

    JButton newUnit = new JButton("New Unit");
    newUnit.setName("new"); buttonsActions.add(newUnit);
    newUnit.addActionListener(e -> ButtonClick.clicked(newUnit));
    JButton attack = new JButton("Attack");
    attack.setName("attack"); buttonsActions.add(attack);
    attack.addActionListener(e -> ButtonClick.clicked(attack));
    JButton train = new JButton("Train");
    train.setName("train"); buttonsActions.add(train);
    train.addActionListener(e -> ButtonClick.clicked(train));
    JButton heal = new JButton("Heal");
    heal.setName("heal"); buttonsActions.add(heal);
    heal.addActionListener(e -> ButtonClick.clicked(heal));
    JButton rest = new JButton("Rest");
    rest.setName("rest"); buttonsActions.add(rest);
    rest.addActionListener(e -> ButtonClick.clicked(rest));
    JButton status = new JButton("Status Report");
    status.setName("status"); buttonsActions.add(status);
    status.addActionListener(e -> ButtonClick.clicked(status));
    JButton cancel = new JButton("Cancel");
    cancel.setName("cancel"); buttonsActions.add(cancel);
    cancel.addActionListener(e -> ButtonClick.clicked(cancel));

    boxActions.add(newUnit);  boxActions.add(Box.createHorizontalStrut(40));
    boxActions.add(attack);   boxActions.add(Box.createHorizontalStrut(40));
    boxActions.add(train);    boxActions.add(Box.createHorizontalStrut(40));
    boxActions.add(rest);     boxActions.add(Box.createHorizontalStrut(40));
    boxActions.add(heal);     boxActions.add(Box.createHorizontalStrut(40));
    boxActions.add(status);     boxActions.add(Box.createHorizontalStrut(40));
    boxActions.add(cancel);
    actionPanel.add(boxActions);

    JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, enemies, messagePanel);

    buttons.add(buttonsAlly);
    buttons.add(buttonsActions);
    buttons.add(buttonsEnemy);


    this.add(actionPanel, BorderLayout.SOUTH);
    this.add(allies, BorderLayout.WEST);
    this.add(pane, BorderLayout.EAST);
    this.add(battleField, BorderLayout.CENTER);
    this.setVisible(true);
}
}

0 个答案:

没有答案