Sabtu, 16 Desember 2017









untuk koding :
package javaapplication21;

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 animasiMobil extends JPanel{
    private static final int D_W = 400;
    private static final int D_H = 400;

    List<Car> cars;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public animasiMobil() {
        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.green);
            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 animasiMobil());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("Animasi Mobil");
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

hasil output 
Add caption

 package orangbergerak;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 *
 */
public class orangbergerak extends JPanel  {
    //Membuat variabel thread

    Thread animasi, repaint;
    int x=10,y=10,a=10, batas = 240;
    int xOrang = -100, yOrang = 100, v = 1;



    public orangbergerak() {
        setPreferredSize(new Dimension(500, 300));
        setFocusable(true);
        setBackground(Color.green);
        requestFocusInWindow();
        initThread();
        //untuk memulai thread
        animasi.start();
        repaint.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        drawText(g2);
        drawOrang(g2, xOrang, yOrang, v);
    }

    //method untuk menampung thread
    public void initThread() {
        animasi = new Thread(new Runnable() {

            public void run() {
                while (true) {
                    // <editor-fold defaultstate="collapsed" desc="menggerakkan Orang">
                    if (xOrang < 500) {
                        if (v == 1) {
                            xOrang = xOrang + 10;
                            v = 2;
                        } else {
                            xOrang = xOrang + 10;
                            v = 1;
                        }
                    } else {
                        xOrang = -100;
                    }
                    // </editor-fold>
                    try {
                        //untuk mengatur kecepatan thread, semakin kecil nilai semakin cepat
                        Thread.sleep(150);
                    } catch (Exception e) {
                    }
                }
            }
        });

        repaint = new Thread(new Runnable() {

            public void run() {
                while (true) {
                    // <editor-fold defaultstate="collapsed" desc="Untuk mengacak warna">
                    if(a<batas){
                        a=a+15;
                        batas=240;
                    }else{
                        a=a-15;
                        batas=15;
                    }
                    // </editor-fold>

                    // <editor-fold defaultstate="collapsed" desc="menggerakkan text">
                    x=(int)(Math.random()*10);
                    y=(int)(Math.random()*10);
                    // </editor-fold>
                    try {
                        //untuk mengatur kecepatan thread, semakin kecil nilai semakin cepat
                        Thread.sleep(10);
                    } catch (Exception e) {
                    }
                    //untuk memanggil repaint
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            repaint();
                        }
                    });
                }
            }
        });
    }

    public void drawText(Graphics2D g2) {
        g2.setFont(new Font("Comic sans ms", 1, 35));
        g2.setColor(new Color((int)(Math.random()*250), (int)(Math.random()*250), (int)(Math.random()*250), a));
        g2.drawString("beta mau pi stikom artha buana", 20+x, 50+y);
    }

    public void drawOrang(Graphics2D g2, int x, int y, int v) {
        g2.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        Ellipse2D kepala = new Ellipse2D.Double(x + 38, y + 10, 20, 20);

        GeneralPath gp = new GeneralPath();
        gp.moveTo(x + 46, y + 32);
        gp.lineTo(x + 34, y + 52);
        gp.lineTo(x + 27, y + 73);

        gp.moveTo(x + 46, y + 32);
        gp.lineTo(x + 54, y + 55);
        gp.lineTo(x + 71, y + 72);

        gp.moveTo(x + 46, y + 32);
        gp.lineTo(x + 46, y + 69);
        gp.lineTo(x + 46, y + 93);
        gp.lineTo(x + 26, y + 112);

        gp.moveTo(x + 46, y + 69);
        gp.lineTo(x + 59, y + 93);
        gp.lineTo(x + 66, y + 118);
//=============================================================
        Ellipse2D kepala1 = new Ellipse2D.Double(x + 42, y + 10, 20, 20);

        GeneralPath gp1 = new GeneralPath();
        gp1.moveTo(x + 46, y + 29);
        gp1.lineTo(x + 40, y + 48);
        gp1.lineTo(x + 40, y + 77);

        gp1.moveTo(x + 46, y + 29);
        gp1.lineTo(x + 49, y + 58);
        gp1.lineTo(x + 51, y + 74);

        gp1.moveTo(x + 46, y + 29);
        gp1.lineTo(x + 46, y + 68);
        gp1.lineTo(x + 52, y + 90);
        gp1.lineTo(x + 46, y + 120);

        gp1.moveTo(x + 46, y + 68);
        gp1.lineTo(x + 60, y + 92);
        gp1.lineTo(x + 38, y + 106);

        g2.setPaint(Color.white);

        if (v == 1) {
            g2.fill(kepala);
            g2.draw(gp);
            g2.draw(kepala);
        } else if (v == 2) {
            g2.fill(kepala1);
            g2.draw(gp1);
            g2.draw(kepala1);
        }
    }

    public void xplay() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                final JFrame frame = new JFrame("Animasi Orang");
                frame.setDefaultCloseOperation(
                        JFrame.EXIT_ON_CLOSE);
                frame.add(new orangbergerak());
                frame.pack();
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });

    }


    public static void main(String salis[]) {
        new orangbergerak().xplay();
    }
}

 




PERHITUNGAN JARAK EUCLDIAN

pengukuran jarak Eucldian : Studi kasus susu formula untuk anak balita Beberapa mnggu yang lalu datang seorang ibu kepada saya memi...