Professional Documents
Culture Documents
Java
public class FunImages implements ActionListener, MouseListener{ // to allow add function
JLayeredPane main; // declare main as Layer
JFrame window; // declare window as the JFrame
CharacterImage char_panel; //declare that the char_panel is for CharacterImage
JButton sizeUp_btn, sizeDown_btn; //declare the button used in the frame
public FunImages(){
createWindow();
createMainPanel();
createViewArea();
createTools();
}
public void createWindow(){
// Title
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set Terminate when press X
window.setResizable(false); // does not allow windows to change size
window.setSize(new Dimension(800,580)); // windows size
window.setVisible(true); //to show the JFrame(window) when run
}
public void createMainPanel(){ //method for the Layer for panel
main = new JLayeredPane();
main.setSize(new Dimension(800, 580)); // Panel size
window.getContentPane().add(main); //add panel into JFrame
}
public void createViewArea(){
//a canvas which is a panel (where the image will show up), this is also a method
char_panel = new CharacterImage();
char_panel.loadImage(); // initialize/load image from CharacterImage class.
char_panel.addMouseListener(this);
char_panel.setBackground(Color.white);
//Background are set plain white(No image are used as BG)
char_panel.setBorder(BorderFactory.createLineBorder(Color.black));
//(no idea what this border stuff does >.<") LOL (Change it n does nothing) -.-"
char_panel.setBounds(5,5,785,500); //set position (X and Y) and the size of the
ViewArea (JPanel)
main.add(char_panel, 0,1); // set Character initial position
}
}
}
CharacterImage.java
public class CharacterImage extends JPanel{ // Character image has the properties of a JPanel
private int pos_x, pos_y, width, height;
//private value only allowed to be used by this class only
private
private
private
private
File img_file;
Image img;
Timer move_timer;
AnimationTimer animator;
AnimationTimer.java
public class AnimationTimer extends TimerTask{ // timer class
private int finalX, finalY; //private value only to be used by this class only
private CharacterImage character;
private int speed = 5;
it as c
public AnimationTimer(CharacterImage c){ //get image from CharacterImage class and named
}
public void setFinal(int x, int y){ //get final x and y coordinate from FunImages
finalX = x;
finalY = y;
}
public void run(){
//method to get new position (getPosX() and getPosY() from CharacterImage which is (0,0) initial
position)
if(character.getPosX() != finalX && character.getPosX() != finalY){
//if final x and y not = 0
int newX = character.getPosX() - ((character.getPosX() - finalX)/speed);
//get new x-axis
int newY = character.getPosY() - ((character.getPosY() - finalY)/speed);
//get new y-axis
character.updatePosition(newX, newY); //new update coordinate
}
}