设置javax.swing.Timer

时间:2016-04-15 20:35:26

标签: java user-interface

好的我正在做一个项目,图像应该随机移动,当用户点击图像时,它应该计算点击它的次数。从那里开始它一直在移动,直到它们x。但是,我犯了一个错误,让图像在点击后移动,所以当程序启动时图像还没有移动。我需要它从程序的开头移动。

附在这里因为无论出于何种原因,堆栈溢出一直说我格式错误。

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

// Zombie class 
public class Zombie {

//declare variables 
private int x;
private int y;

private int size;
private Image image;

//constructor 
public Zombie(int xIn, int yIn, String imagePath) {
    x = xIn;
    y = yIn;
    size = Settings.DEFAULT_SIZE;
    setImage(imagePath);
}

//getter for x
public int getX() {
    return x;
}

//getter for y
public int getY() {
    return y;
}

//setter for x
public void setX(int x) {
    this.x = x;
}

//setter for y
public void setY(int y) {
    this.y = y;
  }

  //drawImage method 
  public void update(Graphics g) {
    g.drawImage(image, x, y, size, size, null);
  }

  //try catch exception, if image isn't found 
  public void setImage(String imagePath) { 
    try {
        image = ImageIO.read(new File(imagePath));
    } catch (IOException ioe) {
        System.out.println("Unable to load image file.");
    }
  }

 }



 public class Settings {

//adjusts the width and height of the creature 
 public static final int WIDTH         = 500;
    public static final int HEIGHT        = 300;

    public static final int DEFAULT_SIZE  = 50;

    //image name 
    public static final String ZOMBIE_IMAGE = "zombie.png";
    }

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.util.Random;

class Controller implements MouseListener {

//declares variables 
Zombie zombie;
View view;
private int count = 0;

Controller() { 
    //System.out.println ("Controller()");
} 

public void addZombie(Zombie z){
    //System.out.println("Controller: adding zombie");
    this.zombie = z;
}

public void addView(View v){
    //System.out.println("Controller: adding view");
    this.view = v;
}

public void mousePressed(MouseEvent e) {
    //System.out.println("Controller sees mouse pressed: acting on Model");
    int prevX = e.getX();
    int prevY = e.getY();

    prevX -= zombie.getX();
    prevY -= zombie.getY();

    if((prevX > 0 && prevX < Settings.DEFAULT_SIZE) &&
            (prevY > 0 && prevY < Settings.DEFAULT_SIZE)) {
        //System.out.println("Got Zombie.");
        Random r = new Random();
        zombie.setX(r.nextInt(view.getWidth() - Settings.DEFAULT_SIZE));
        zombie.setY(r.nextInt(view.getHeight() - Settings.DEFAULT_SIZE));
        ++count;
    }
}

public int getCount() {
    return count;
}

//mouse events 
public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {
}

public void update(Graphics g) {
    zombie.update(g); 
}
}

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.util.Random;

public class View implements ActionListener {

private JFrame frame;
Controller controller;

public static void main(String[] args) {
    Zombie zombie = new Zombie(Settings.WIDTH/2, Settings.HEIGHT/2, Settings.ZOMBIE_IMAGE);
    View view   = new View();

    Controller myController = new Controller();
    myController.addZombie(zombie);
    myController.addView(view);

    view.addController(myController);

    new Timer(500, view).start();

}

private class MyPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    public void paintComponent(Graphics g) {
        controller.update(g);
        revalidate();
    }
}

private MyPanel panel;

View() {
    //System.out.println("View()"); 

    frame = new JFrame("Catch The Zombie");

    // Create a panel to contain a label, a text box, and a button
    panel = new MyPanel();
    frame.add(panel);    

    frame.setSize(Settings.WIDTH, Settings.HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent e)
        {
            // TODO Auto-generated method stub
            System.out.println("Zombie was caught " + controller.getCount() + "times");
        }
    });
} 

public void revalidate() {

}

public void addController(Controller controller){
    //System.out.println("View      : adding controller");

    this.controller = controller;
    frame.getContentPane().addMouseListener((MouseListener) controller);
}

public int getWidth() {
    return frame.getWidth();
}

public int getHeight() {
    return frame.getHeight();
}

public void actionPerformed(ActionEvent e) {
    frame.repaint();
}
} 

0 个答案:

没有答案