Senin, 15 Desember 2014

Cara membuat kipas dengan java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package retno;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;

    /**
 *
 * @author retno
 */
public class retno extends JPanel implements Runnable {
    private int x = 0;
    int x2 = 500;
    int x3 = 500;
    Thread th = new Thread(this);

public retno(){
    setPreferredSize(new Dimension(350, 300));
            th.start();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        GradientPaint wp = new GradientPaint(150,0, Color.white,200,0, Color.orange);

            if (x < 256){
                x2 = x;
            }
        GradientPaint wl = new GradientPaint(0, 0, new Color(0 + x2, 225 - x2, 225 - x2), 0, 100, new Color(255, 255, 255 - x2));
        GradientPaint wt = new GradientPaint(0, 200, Color.green, 0, 250, new Color(18, 102, 2));

        g2.setPaint(wl);
        g2.fillRect(0, 0, 500, 500);
        g2.setPaint(wt);
        g2.fillRect(0, 200, 350, 100);
      
      
        g2.setPaint(wp);
        GeneralPath gp = new GeneralPath();
        gp.moveTo(150, 150);
        gp.lineTo(200, 150);
        gp.lineTo(210, 270);
        gp.lineTo(140, 270);
        gp.closePath();
        g2.fill(gp);
        g2.setColor(Color.black);
        g2.draw(gp);

        gamTri(g, x);
        gamTri(g, x + 90);
        gamTri(g, x + 180);
        gamTri(g, x + 270);
        g2.setColor(Color.BLACK);
        g2.fillOval(171, 171, 10, 10);
            }

            private void gamTri(Graphics g, int angle){
                g.setColor(Color.red);
                g.fillArc(100, 100, 150, 150, angle, 30);
            }

    public void run() {
        while (true){
            this.x = x + 1;
            try {
                repaint();
                Thread.sleep(100);
            }
            catch(InterruptedException ex){
                Logger.getLogger(retno.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

/**
     * @param args the command line arguments
     */

     public static void main(String[] args){
        // TODO code application logic here

         JFrame fra = new JFrame();
         JPanel pa = new retno();
         fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         fra.getContentPane().add(pa);
         fra.pack();
         fra.setVisible(true);

     };
}

Tidak ada komentar:

Posting Komentar