Senin, 26 Oktober 2015

Program Garis Java, Random Hline dan Vline menggunakan JButton.





Tugas Grafik Komp. & Pengolahan Citra
Franky Silalahi ( 13113572 ) 
3KA32

//
Berikut ini saya akan memberikan codingan Program Garis, Random HLine dan VLine menggunakan Java.

// Source Code;

package garis;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class randomline extends JComponent{
private static class Line{
    final int x1;
    final int y1;
    final int x2;
    final int y2;
    final Color color;
    public Line(int x1, int y1, int x2, int y2, Color color) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.color = color;
    }            
}
private final LinkedList<Line> lines = new LinkedList<Line>();
public void addLine(int x1, int x2, int x3, int x4) {
    addLine(x1, x2, x3, x4, Color.black);
}
public void addLine(int x1, int x2, int x3, int x4, Color color) {
    lines.add(new Line(x1,x2,x3,x4, color));    
    repaint();
}
public void clearLines() {
    lines.clear();
    repaint();
}
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Line line : lines) {
        g.setColor(line.color);
        g.drawLine(line.x1, line.y1, line.x2, line.y2);
    }
}

public static void main(String[] args) {
    JFrame testFrame = new JFrame();
    testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    final randomline comp = new randomline();
    comp.setPreferredSize(new Dimension(500, 500));
    testFrame.getContentPane().add(comp, BorderLayout.CENTER);
    JPanel buttonsPanel = new JPanel(new GridLayout(1,3));
    JButton newLineButton = new JButton("Random V Line");
    JButton newLineButton2 = new JButton("Random H Line");
    JButton clearButton = new JButton("Clear");
    buttonsPanel.add(newLineButton);
    buttonsPanel.add(newLineButton2);
    buttonsPanel.add(clearButton);
    testFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
    newLineButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int x1 = (int) (Math.random()*500);
            int y1 = (int) (Math.random()*500);
            int y2 = (int) (Math.random()*500);
            Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
            comp.addLine(x1, y1, x1, y2, randomColor);
        }
    });
   
    newLineButton2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int x1 = (int) (Math.random()*500);
            int x2 = (int) (Math.random()*500);
            int y1 = (int) (Math.random()*500);
            Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
            comp.addLine(x1, y1, x2, y1, randomColor);
        }
    });
   
    clearButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            comp.clearLines();
        }
    });
    testFrame.pack();
    testFrame.setVisible(true);
}

}


//
Penjelasan Source Code Java tersebut:

Dalam program ini kita menggunakan beberapa macam import diatas, yaitu beberapa java.awt , java.until, dan javax.swing . pada intinya semua dalam import tersebut adalah untuk menampilkan semua hal yang berhubungan dengan antarmuka berbasis grafis dan tombol untuk random hline dan vline garisnya

Setelah melakukan semua import, kita akan masuk ke public class randomline di private static class Line(I, menjelaskan beberapa variable x1,x2,y1,y2 dengan menggunakan tipe data dan juga warna.

Pada private final LinkedList, didalam ini menjelaskan dalam bentuk grafis atau dalam hasil output akan membuat sebuah garis, Hline dan Vlinenya dalam warna hitam atau warna lainnya, dan juga menghapus garis yang telah dibuat. dan terakhir, didalam public static void main(String[] args) {, tersebut akan menampilkan hasilnya pada output dibawah, pada saat mengklik random V line, program akan mencetak garis vertikal yang random ukurannya dan warnanya, begitu juga saat mengklik random H linenya. dan juga tombol clear tersebut akan menghapus semua garis yang tadi dibuat dengan dimensi layar 500:500 x dan y.


// Output Hasil dari Program Garis
Random Horizontal Line (HLine) dan Vertical Line (VLine) :




// using with Netbeans 8.0.2.
// coding source from here.

0 komentar:

Posting Komentar