Java : Menggambar Pelangi

Last Updated on 13 years by Mas Herdi

Sebagai pemanasan karena telah lama saya tidak menulis di blog ini, saya akan membahas sedikit tentang computer generated image, dimana yang dihasilkan adalah sebuah gambar pelangi.

GambarPelangi.java

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

class GambarPelangi extends JPanel
{
    final Color VIOLET = new Color(128,0,128);
    final Color INDIGO = new Color(75,0,130);

    private Color colors[] =
        {    Color.WHITE, Color.WHITE, VIOLET, INDIGO, Color.BLUE,
            Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED};

    public DrawRainbow()
    {
        setBackground(Color.WHITE);

    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int radius = 20;

        int centerX = getWidth()/2;
        int centerY = getHeight() - 10;

        for( int counter = colors.length; counter > 0; counter--)
        {
            g.setColor(colors[counter - 1]);
            g.fillArc(    centerX - counter * radius,
                        centerY - counter * radius,
                        counter * radius * 2, counter * radius * 2, 0, 180);
        }
    }
}

GambarPelangiTest.java

import javax.swing.JFrame;
import javax.swing.JLabel;

public class DrawRainbowTest
{
    public static void main(String... args)
    {
        DrawRainbow panel = new DrawRainbow();
        JFrame application = new JFrame();
        JLabel label = new JLabel("TWOH ENGINEERING Computer Generated Rainbow");
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.add(label);
        application.add(panel);
        application.setSize(400,250);
        application.setVisible(true);
        }
}

Dan inilah hasilnya :

Gambar Pelangi

Untuk penjelasannya nanti saja, karena saya sudah capek.. ^_^





Download aplikasi kami di Google Play Store


Tutorial Menarik Lainnya :

Leave a Reply

Your email address will not be published. Required fields are marked *

TWOH&Co.