import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;

public class Praktikum3 extends JFrame implements KeyListener{
    private int kiri;
    private int sensor1;
    private int sensor2;
    private Color warna;
    public Praktikum3(){
        super("Background Bergerak");
        addKeyListener(this);
    }

    public static void main(String[] args) {
        Praktikum3 p3 = new Praktikum3();
        p3.setSize(660, 250);
        p3.setVisible(true);
        p3.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            kiri -= 3;
        }
        else if(e.getKeyCode() == KeyEvent.VK_LEFT){
            kiri += 3;
        }
        repaint();
    }
    public void background(Graphics g, int gerakanHorisontal, int a1, int a2){
        g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.blue);

        g.drawRect(a1 + gerakanHorisontal, 30, 60, 200);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal, 30, 60, 200);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 60, 60, 40, 170);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 60, 60, 40, 170);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 100, 70, 90, 160);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 100, 70, 90, 160);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 200, 70, 90, 160);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 200, 70, 90, 160);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 290, 100, 20, 130);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 290, 100, 20, 130);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 310, 100, 40, 130);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 310, 100, 40, 130);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 360, 100, 30, 130);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 360, 100, 30, 130);
        sensor2 = a2 + gerakanHorisontal;

        g.drawRect(a1 + gerakanHorisontal + 410, 60, 60, 170);
        sensor1 = a1 + gerakanHorisontal;
        g.drawRect(a2 + gerakanHorisontal + 410, 60, 60, 170);
        sensor2 = a2 + gerakanHorisontal;
    }

    public void paint(Graphics g){
        Graphics2D g2=(Graphics2D) g ;

        background(g, kiri,getWidth() + 20, 0);
        background(g, kiri,getWidth() - 90, 0);

        if(sensor2 + 20 >= getWidth()){
            kiri = 0;
        }
        else if(sensor1 <= 0){
            kiri = 0;
        }
        g.drawRect(0, 0, getWidth(), getHeight());
            // Rangka Mobiil
           int mX1 = 240, mY1 = 140;
            int mX2 = 330, mY2 = 140;
            int mX3 = 380, mY3 = 150;
            int mX4 = 410, mY4 = 225;
            int mX5 = 420, mY5 = 225;
            int mX6 = 420, mY6 = 225;
            int mX7 = 380, mY7 = 225;
            int mX8 = 360, mY8 = 225;
            int mX9 = 340, mY9 = 225;
            int mX10 = 260, mY10 = 225;
            int mX11 = 240, mY11 = 225;
            int mX12 = 220, mY12 = 225;
            int mX13 = 190, mY13 = 225;
            int mX14 = 190, mY14 = 200;
            int mX15 = 200, mY15 = 200;
            int mX16 = 200, mY16 = 180;
            int mX17 = 210, mY17 = 180;

            int kX1 = 226, kY1 = 170;
            int kX2 = 240, kY2 = 170;
            int kX3 = 240, kY3 = 150;
            int kX4 = 250, kY4 = 170;
            int kX5 = 290, kY5 = 170;
            int kX6 = 290, kY6 = 150;
            int kX7 = 250, kY7 = 150;
            int kX8 = 300, kY8 = 170;
            int kX9 = 360, kY9 = 170;
            int kX10 = 330, kY10 = 150;
            int kX11 = 300, kY11 = 150;

            int rX1 = 340, rY1 = 190;
            int rX2 = 220, rY2 = 190;

            int vX1 = 350, vY1 = 200;
            int vX2 = 230, vY2 = 200;

            //jalan
            Line2D jalan = new Line2D.Float(0, 230, 660, 230);
            warna = new Color(0, 0, 0);
            g2.setColor(warna);
            g2.setPaint(warna);
            g2.fill(jalan);
            g2.setPaint(warna);
            g2.draw(jalan);

            warna = new Color(0, 255, 0);
            g2.setColor(warna);
            Polygon mobil = new Polygon();
              mobil.addPoint(mX1, mY1);
              mobil.addPoint(mX2, mY2);
              mobil.addPoint(mX3, mY3);
              mobil.addPoint(mX4, mY4);
              mobil.addPoint(mX5, mY5);
              mobil.addPoint(mX6, mY6);
              mobil.addPoint(mX7, mY7);
              mobil.addPoint(mX8, mY8);
              mobil.addPoint(mX9, mY9);
              mobil.addPoint(mX10, mY10);
              mobil.addPoint(mX11, mY11);
              mobil.addPoint(mX12, mY12);
              mobil.addPoint(mX13, mY13);
              mobil.addPoint(mX14, mY14);
              mobil.addPoint(mX15, mY15);
              mobil.addPoint(mX16, mY16);
              mobil.addPoint(mX17, mY17);
            g2.fill(mobil);
            g2.drawPolygon(mobil);

            warna = new Color(0, 0, 0);
            g2.setColor(warna);
            Polygon kaca1 = new Polygon();
              kaca1.addPoint(kX1, kY1);
              kaca1.addPoint(kX2, kY2);
              kaca1.addPoint(kX3, kY3);
            g2.fill(kaca1);
            g2.drawPolygon(kaca1);

            Polygon kaca2 = new Polygon();
              kaca2.addPoint(kX4, kY4);
              kaca2.addPoint(kX5, kY5);
              kaca2.addPoint(kX6, kY6);
              kaca2.addPoint(kX7, kY7);
            g2.fill(kaca2);
            g2.drawPolygon(kaca2);

            Polygon kaca3 = new Polygon();
              kaca3.addPoint(kX8, kY8);
              kaca3.addPoint(kX9, kY9);
              kaca3.addPoint(kX10, kY10);
              kaca3.addPoint(kX11, kY11);
            g2.fill(kaca3);
            g2.drawPolygon(kaca3);

            Ellipse2D roda1 = new Ellipse2D.Float(rX1, rY1, 40, 40);
            Ellipse2D roda2 = new Ellipse2D.Float(rX2, rY2, 40, 40);
            g2.fill(roda1);
            g2.fill(roda2);
            g2.draw(roda1);
            g2.draw(roda2);

            Ellipse2D velg1 = new Ellipse2D.Float(vX1, vY1, 20, 20);
            Ellipse2D velg2 = new Ellipse2D.Float(vX2, vY2, 20, 20);
            warna = new Color(255,0,255);
            g2.setPaint(warna);
            g2.fill(velg1);
            g2.fill(velg2);
            g2.draw(velg1);
            g2.draw(velg2);
    }
}

No related posts.

Tulisan terkait dibawakan kepada Anda oleh Yet Another Related Posts Plugin.