Showing posts with label Graphic With Java. Show all posts
Showing posts with label Graphic With Java. Show all posts

Animation With Java

10:44 PM | , , , , ,

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

Here is The Source .
Read More

Make a Graphic with Java

9:18 PM | , , , , , , ,

Hey.. long time no see you ... Coz  I'm so very busy. Now I want to share about how to make a graphic in the Java. This is a screenshot picture of the program that we want to make.


How to make it in java ? This is the code ..


package tugas_1;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author alircute
 * Game Programming Task
 * 21th March 2012
 */

public class Gambar extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2= (Graphics2D) g;
     
        //Make the window
        g2.setColor(Color.BLACK);
        g2.fillRect(400, 0, 400, 400);
        g2.setColor(Color.CYAN);
        g2.fillRect(400, 0, 400, 10);      
        g2.fillRect(400, 195, 400, 10);
        g2.fillRect(400, 390, 400, 10);
        g2.fillRect(400, 0, 10, 400);
        g2.fillRect(600, 0, 10, 400);
     
        //Make the moon
        g2.setColor(Color.yellow);
        Shape b1 = new Ellipse2D.Double(680, 60, 60, 70);      
        Shape b2 = new Ellipse2D.Double(670,70,45,45);
        Shape m1 = new Ellipse2D.Double(705, 80, 10, 20);
        Shape m3 = new Ellipse2D.Double(705, 85, 8, 15);
        Shape m2 = new Ellipse2D.Double(715, 80, 10, 20);
        Shape m4 = new Ellipse2D.Double(715, 85, 8, 15);      
        Area r11 = new Area(b2);
        Area a11 = new Area(b1);
        a11.subtract(r11);
        g2.fill(a11);
        g2.setColor(Color.BLACK);
        g2.draw(m1);
        g2.draw(m2);
        g2.setColor(Color.WHITE);
        g2.fill(m1);
        g2.fill(m2);
        g2.setColor(Color.BLUE);
        g2.fill(m3);
        g2.fill(m4);
        g2.setColor(Color.BLACK);
        g2.fillRect(700, 108, 20, 5);
     
        //Question mark
        g2.setFont(new Font("Times New Roman", 1, 24));
        g2.setColor(Color.WHITE);
        g2.drawString("?", 700, 50);
        g2.drawString("?", 670, 80);
        g2.drawString("?", 740, 80);
     
        //Make a people
        g2.setColor(Color.BLUE);
        g2.fillRoundRect(120, 250, 200, 300, 35, 35);
        g2.setColor(Color.yellow);
        g2.fillRoundRect(200, 220, 30, 50,35,35);
        g2.setColor(Color.GREEN);
        Shape kepala = new Ellipse2D.Double(155, 100, 120, 150);
        g2.fill(kepala);
        g2.setColor(Color.WHITE);
        g2.fillOval(185, 130, 20, 40);
        g2.fillOval(225, 130, 20, 40);
        g2.setColor(Color.BLACK);
        g2.fillOval(190, 150, 15, 20);
        g2.fillOval(225, 150, 15, 20);
        Shape mulut = new Ellipse2D.Double(190, 195, 50, 30);  
        Shape lidah = new Ellipse2D.Double(190, 205, 50, 30);
        g2.setColor(Color.RED);
        g2.fill(mulut);
        Area lidahnya = new Area(lidah);
        Area m = new Area(mulut);
        m.subtract(lidahnya);
        g2.setColor(Color.BLACK);
        g2.fill(m);
       
     
     
        //Make a notebook
        g2.setColor(Color.DARK_GRAY);
        g2.fillRoundRect(45, 295, 350, 250, 15, 15);
        g2.setColor(Color.DARK_GRAY);
        RoundRectangle2D rd=new RoundRectangle2D.Double(45, 545, 350, 20, 15, 15);
        g2.fill(rd);
             
        //Make an Apple Logo but I deliberate reserve it
        Shape e1 = new Ellipse2D.Double(200,320,40,40);
        Shape e2 = new Ellipse2D.Double(220,330,40,40);
        Shape e3 = new Ellipse2D.Double(160,370,45,45);
        Shape s1 = new Ellipse2D.Double(180, 360, 60, 70);
        Shape s2 = new Ellipse2D.Double(210, 360, 50, 70);
        Area d1 = new Area(e1);
        Area d2 = new Area(e2);
        Area r1 = new Area(e3);
        Area a2 = new Area(s2);
        Area a1 = new Area(s1);
        a1.subtract(r1);
        a1.add(a2);      
        d2.intersect(d1);
        a1.add(d2);      
        g2.setColor(Color.LIGHT_GRAY);
        g2.draw(a1);
        g2.fill(a1);
     
        g2.setFont(new Font("Arial", 1, 32));
        g2.setColor(Color.MAGENTA);
        g2.drawString("http://www.mabok-coding.com", 10, 650);
    }

 
   //The main method
    public static void main(String[] args) {
        Gambar gambarQ=new Gambar();
        gambarQ.setVisible(true);
        JFrame f=new JFrame("Alir Emang Oke");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.getContentPane().add(gambarQ);
        f.setSize(800,800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

You can download the source at http://www.4shared.com/rar/CqIe7nal/Java_Graphic2D.html

Read More