Java programming part 102 Graphics : Java Animation 5 MouseListener 1

Published: 10 July 2012
on channel: Redemptie
2,665
5

Java Programming tutorial, GUI, Graphical User Interface. this tutorial is teaching. How to use Java MouseListener for animation,Showing you how to use mouse buttons to control the game.

// BKGround.java class below


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class BKGround extends JPanel implements KeyListener,MouseListener{
Graphics gr;
Image image;
Image image2;
int x1,y1,x2,y2;
int num;
boolean right;
boolean down;

public BKGround(){

x1 = 100;
y1 = 100;
x2 = 200;
y2 = 200;
num = 0;
right = true;
down = true;

}

public void paint(Graphics g){
num = num + 1;
ImageIcon I = new ImageIcon("image/Abstract-background-1.png");
image = I.getImage();
ImageIcon I2 = new ImageIcon("image/charcustomsheet.gif");
image2 = I2.getImage();

g.drawImage(image, 0, 0, null);
g.drawImage(image2, x1, y1, x2, y2, 40, 60, 80, 120, null);
repaint();
}

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP){
y1 = y1 - 1;
y2 = y2 - 1;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
y1 = y1 + 1;
y2 = y2 + 1;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
x1 = x1 + 1;
x2 = x2 + 1;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
x1 = x1 - 1;
x2 = x2 - 1;
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent me) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent me) {
// TODO Auto-generated method stub
System.out.println("Entered at x "+me.getX());

}

@Override
public void mouseExited(MouseEvent me) {
// TODO Auto-generated method stub
System.out.println("Exited");
}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Pressed");
}

@Override
public void mouseReleased(MouseEvent me) {
// TODO Auto-generated method stub
System.out.println("released");
}
}

//GraphicsTut.java below


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.*;


public class GraphicsTut extends JFrame{
Image image;
Image image2;
int x1,y1,x2,y2;
BKGround bkg = new BKGround();


public GraphicsTut(){
this.setTitle("Remember g for Graphics");
this.setSize(450,350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(bkg);
this.addKeyListener(bkg);
this.addMouseListener(bkg);

}

public static void main(String[] args){
new GraphicsTut();

}

}


On this page of the site you can watch the video online Java programming part 102 Graphics : Java Animation 5 MouseListener 1 with a duration of hours minute second in good quality, which was uploaded by the user Redemptie 10 July 2012, share the link with friends and acquaintances, this video has already been watched 2,665 times on youtube and it was liked by 5 viewers. Enjoy your viewing!