Hetalia: Axis Powers - Liechtenstein

Senin, 11 Mei 2015

Grafik 2 dimensi sinus


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GrafikSinus extends JPanel {

    public static void main(String[] args) {
        JFrame f = new JFrame("Grafik Sinusku");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        GrafikSinus gpk = new GrafikSinus();
        f.add(gpk);
        f.setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g); //To change body of generated methods, choose Tools | Templates.

        g.drawLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);
        Graphics2D g2d = (Graphics2D) g;
        double yPerPixel = (double) 2 / (double) (this.getHeight() / 2);
        double xPerPixel = 2 * Math.PI / (this.getWidth());

        Ellipse2D.Double e1 = new Ellipse2D.Double(0, 0, 50, 50);
        Random r = new Random();
        int posX;
        int posY;
        int merah;
        int biru;
        int hijau;
        int ukuran;
        for (int i = 0; i < this.getWidth(); i++) {
            double x = i * xPerPixel;
            double y = Math.sin(x);
            posY = (int) (y / yPerPixel) + (this.getHeight() / 2) - 1;
            ukuran = r.nextInt(50) + 20;
            merah = r.nextInt(256);
            hijau = r.nextInt(256);
            biru = r.nextInt(256);
            Color c = new Color(merah, hijau, biru);
            g2d.drawOval(i - ukuran / 2, posY - ukuran / 2, ukuran, ukuran);
            g2d.setColor(c);

        }
    }

}

Tidak ada komentar:

Posting Komentar