Jumat, 09 Juni 2017

BELAJAR MEMBUAT ANIMASI BERJALAN DI NETBEANS

Assalamualaikum WR. WB.
Haloo kesempatan kali saya akan memposting belajar membuat sebuah animasi dengan menggunakan program aplikasi netbeans, animasi ini bisa bergerak atau berjalan yang berbentuk 2D hhe langsung saja...
1. Buka Netbean Kalian.
2. Buat new project, pilih java lalu next.
3. Setelah itu akan muncul seperti gambar di bawah ini.


lalu Project Name kasih nama sesuai dengan keinginan kalian, kali ini saya akan menggunakan JavaApplication18 hhe, lalu klik finish.
4. Lalu bikin class baru disini saya memakai nama class Animasi.


5. lalu untuk source codenya :

package JavaApplication18;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Animasi extends JPanel{
    private static final int D_W = 400;
    private static final int D_H = 400;

    List<Car> cars;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Animasi() {
        setBackground(new Color(153, 102, 51));
        setLayout(null);
        
        cars = new ArrayList();
        cars.add(new Car(100, 300));
        cars.add(new Car(200, 100));

        Timer timer = new Timer(50, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for (Car car : cars) {
                    car.move();
                    repaint();
                }
            }
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Car car : cars) {
            car.drawCar(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }

    public class Car {
        private static final int INCREMENT = 5;
        int x, y;
        public Car(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public void drawCar(Graphics g) {
            g.setColor(Color.RED);
            g.fillRect(x, y, 100, 30);
            g.setColor(Color.BLACK); // body
            g.fillOval(x + 15, y + 20, 20, 20); // wheel
            g.fillOval(x + 60, y + 20, 20, 20); // wheel
            g.fillRect(x + 15, y - 20, 60, 20); // top
        }

        public void move() {
            if (x == D_W) {
                x = 0;
            } else {
                x += INCREMENT;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.getContentPane().add(new Animasi());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("Mobil");
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

NB : disini JavaApplication18 adalah package saya dan Animasi adalah classnya.

6. Setelah itu tekan SHIFT+F6 untuk menjalankan.
7. Dan akhirnya akan jadi seperti gambar di bawah ini.






Sekian bila ada sesuatu yang tidak berkenan mohon maaf, Terimakasih sebelumnya.
Wassalamualaikum WR. WB.


2 komentar: