You are on page 1of 5

FunImages.

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(){

// this a constructor.. main use is to initialize the object

createWindow();
createMainPanel();
createViewArea();
createTools();
}
public void createWindow(){

// method for the JFrame

window = new JFrame("Fun with Images");

// 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
}

public void createTools(){ //add button component into the panel


sizeUp_btn = new JButton("Increase Size"); //named 1st button as "increase Size"

sizeDown_btn = new JButton ("Decrease Size"); // named 2nd button as "Decrease


Size"
sizeUp_btn.setBounds(5,510,150,30); // set 1st button position and size
sizeDown_btn.setBounds(160,510,150,30); // set 2st button position and size
main.add(sizeUp_btn, 1,1); // adding 1st button into the LayeredPane (Panel)
main.add(sizeDown_btn, 1,1); // adding 2st button into the LayeredPane (Panel)
sizeUp_btn.addActionListener(this); //add 1st button into actionPerformed method
sizeDown_btn.addActionListener(this); //add 2nd button into actionPerformed method
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == sizeUp_btn){ // action when 1st button is pressed
char_panel.increaseCharacter();
// initialize increaseCharacter method from CharacterImage class
char_panel.repaint(); // just to check either to update()/ Paint() and then closes
}else if(evt.getSource() == sizeDown_btn){ // action when 2nd button is pressed
char_panel.decreaseCharacter();
// initialize increaseCharacter method from CharacterImage class
char_panel.repaint(); // just to check either to update()/ Paint() and then closes
}
}
public void mousePressed(MouseEvent evt){}
// some other mouseEvent function (action happened at the moment the mouse is pressed/hold)
public void mouseReleased(MouseEvent evt){
// mouseEvent function (action happened at the moment the mouse is released)
char_panel.moveCharacter(evt.getX(), evt.getY());
// get the released mouse click position (new position)
}

public void mouseEntered(MouseEvent evt){}


// even more mouseEvent function (action when the mouse enters a component)
public void mouseExited(MouseEvent evt){}
// EVEN even more mouseEvent function!! (action when the mouse leave/exits a component)
public void mouseClicked(MouseEvent evt){}
// EVEN EVEN even more mouseEvent function!!! >.< (action when the mouse pressed and
released (a single click))
public static void main(String[] args){
// main class (you know what it does XP) basically run the program (FunImages class) without it
program won't run
FunImages f = new FunImages();

}
}
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;

public CharacterImage(){ //method for the CharacterImage


pos_x = 0; // image initial position
pos_y = 0;
img_file = new File("images/hulk.png");
// import .png image from resource file
move_timer = new Timer ("Move Timer");
animator = new AnimationTimer(this); // get AnimationTimer class
move_timer.scheduleAtFixedRate(animator, new Long(50), new Long(50));
// this is a timer to set the speed of the image, how long it takes for it to move from one
position to another
}
public int getPosX(){ //get the position of x-axis
return pos_x;
}
public int getPosY(){ // get the position of y-axis
return pos_y;
}
public void loadImage(){ // method to load the image
try{
img = ImageIO.read(img_file);
// read the image (useful when using a tiled image)
width = img.getWidth(null); // get image size
height = img.getHeight(null);
}catch(Exception ex){
System.out.println("Image cannot be loaded");
// error message when no image is loaded (useful for checking error)
}
}

public void increaseCharacter(){ //method to increase the size of the image


System.out.println(img.getWidth(null));
int tmp_width = width + ((10*width)/100);
int tmp_height = height + ((10*height)/100);
if(tmp_height < 300){ // image max size
width = tmp_width;
height = tmp_height;
}
}
public void decreaseCharacter(){ //method to decrease the image size
System.out.println(img.getHeight(null));
int tmp_width = width - ((10*width)/100);
int tmp_height = height - ((10*height)/100);
if(tmp_height > 128){ // image min size
width = tmp_width;
height = tmp_height;
}
}
public void updatePosition(int x, int y){ // to update new position (from AnimationTimer
class)
pos_x = x;
pos_y = y;
this.repaint(); // to check if new position is received or not from FunImage
}
public void moveCharacter(int x, int y){ // to move the character image to the new position
animator.setFinal(x, y);
//get x and y position from FunImages class (action when mouse is released)
}
public void paint(Graphics g){ // to draw image onto the canvas (a JPanel)
super.paintComponent(g); //get attribute from superclass
g.drawImage(img, pos_x, pos_y, width, height, null);
// to allow what item to be draw (the image(img))
// at what position (pos_x and pos_y)
// within the specific canvas (size = width * height)
// image observer is set to null since there is nothing to be notify.
}
}

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
}

character = c; //give c a different name called character

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
}
}

You might also like