Hi guys.. How are you ? Did you read my article about how to make a graphic2D in java ?
How is it ? It's amazing , isn't t ? hehehe :D
Now we will study about how to make an animation with java. Let's we start it....
package animasiq;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import sun.java2d.loops.FillRect;
public class Animasi extends JPanel {
Ball ball;
Player1 alir;
Tree tree;
public Animasi() {
setPreferredSize(new Dimension(800, 600));
addMouseListener(new ClickDetector(this));
tree=new Tree(600, 200);
ball = new Ball(0, 450);
alir=new Player1(-100, 350);
alir.nextX=390-alir.getwidth();
Thread t = new Thread(new Runnable() {
public void run() {
while(true) {
alir.update();
ball.update();
ball.nextX = alir.x + alir.getwidth() +5 ;
if(ball.nextY<=460) ball.nextY+=2; else ball.nextY-=2;
repaint();
try {
if(ball.x>=430){
Thread.sleep(400);
}else{
Thread.sleep(200);
}
} catch (InterruptedException ex) {}
}
}
});
t.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gd = (Graphics2D) g;
gd.setColor(Color.BLUE);
gd.fillRect(0, 0, 800, 400);
gd.setColor(Color.GREEN);
gd.fillRect(0, 400, 800, 400);
gd.setColor(Color.BLACK);
gd.fillRect(0, 390, 800, 30);
gd.setFont(new Font("Times New Roman", 1, 24));
gd.setColor(Color.WHITE);
gd.drawString("Created By Alir", 100, 300);
tree.drawTree(gd);
alir.drawBall(gd);
ball.drawBall(gd);
System.out.println(ball.x+" "+ball.y);
gd.dispose();
}
class ClickDetector extends MouseAdapter {
Animasi parent;
public ClickDetector(Animasi parent) {
this.parent = parent;
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
parent.alir.nextX=e.getX();
}
}
class Ball {
public BufferedImage image;
public int x, y;
public int nextX, nextY;
public int scale=40;
public Ball(int x, int y) {
this.x = x;
this.y = y;
nextX = x;
nextY = y;
try {
image = ImageIO.read(getClass().getResource("ball.png"));
} catch (IOException ex) {}
}
public void drawBall(Graphics2D gd) {
Image gambar = null;
Image ori=image;
gambar= ori.getScaledInstance(this.scale , -1, Image.SCALE_SMOOTH);
gd.drawImage(gambar, x, y, null);
}
public void growthBall(Graphics2D gd) {
Image gambar = null;
try {
Image ori=image;
gambar= ori.getScaledInstance(this.scale , -1, Image.SCALE_SMOOTH);
gd.drawImage(gambar, x, y, null);
this.scale+=10;
} catch (Exception ex) {
System.out.println("SALAH");
}
}
public void update() {
if (x != nextX) {
if(ball.x>430){
x = x > nextX ? x - 40 : x + 30;
}else{
x = x > nextX ? x - 20 : x + 20;
}
}
if (y != nextY) {
y = y > nextY ? y - 5 : y + 5;
}
}
}
class Tree {
public BufferedImage image;
public int x, y;
public int nextX, nextY;
public int scale=40;
public Tree(int x, int y) {
this.x = x;
this.y = y;
nextX = x;
nextY = y;
try {
image = ImageIO.read(getClass().getResource("pohon.png"));
} catch (IOException ex) {}
}
public void drawTree(Graphics2D gd) {
Image gambar = null;
Image ori=image;
gambar= ori.getScaledInstance(150 , -1, Image.SCALE_SMOOTH);
gd.drawImage(gambar, x, y, null);
}
public void update() {
x = x < 100 ? 0 : x - 30;
}
}
class Player1{
public BufferedImage image;
public int x, y;
public int nextX, nextY;
public String gbr[]={"Kanan.png","Kiri.png"};
public int posisi=0;
public boolean stop=false;
public Player1() {
this.x=0;
this.y=300;
}
public Player1(int x, int y) {
this.x = x;
this.y = y;
nextX = x;
nextY = y;
if(!stop){
if(this.posisi==0) this.posisi=1; else this.posisi=0;
}
try {
image = ImageIO.read(getClass().getResource(this.gbr[this.posisi]));
} catch (IOException ex) {}
}
public int getwidth(){
return image.getWidth();
}
public void drawBall(Graphics2D gd) {
if(!stop){
if(this.posisi==0) this.posisi=1; else this.posisi=0;
}
try {
image = ImageIO.read(getClass().getResource(this.gbr[this.posisi]));
} catch (IOException ex) {}
gd.drawImage(image, x, y, null);
}
public void update() {
if(!stop){
if (x != nextX) {
x = x > nextX ? x - 10 : x + 10;
}
if (y != nextY) {
y = y > nextY ? y - 1 : y + 1;
}
}
}
}
6 comments:
mana method mainya ???
Hehe.. lupa om..
tar malem ya q upload filenya... :D
udah q upload source programna...
maaf lama menunggu... :D
numpang donlod gan...
muito bom seu poste mas sabe o que tentei fazer aqui e não consegui o jogo tapatan em Java. Gostaria de aceitar o desafio e tentar?
@bsjug:
obrigado, boa sorte
I'm so sorry I can't speak portuguese well.
Post a Comment
Please leave a comment