Pages

Rabu, 21 Desember 2011

Graphic

AnotherSimpleThread

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

public class AnotherSimpleThread implements Runnable {

    Thread thread;

    /** Creates a new instance of AnotherSimpleThread */
    public AnotherSimpleThread() {
    }

    public AnotherSimpleThread(String name) {
        thread = new Thread(this, name);
        thread.start();
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i + " " + thread.getName());
            try {
                thread.sleep((long) (Math.random() * 1000));
            } catch (InterruptedException e) {
            }
        }
        System.out.println("DONE! " + thread.getName());
    }
//}
/* AnotherTwoThreadsTest.java */

//public class AnotherTwoThreadsTest {

    public static void main(String[] args) {
        new AnotherSimpleThread("Bali");
        new AnotherSimpleThread("Jogja");
    }
}



Counter

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

import javax.swing.JLabel;

public class Counter implements Runnable {

    JLabel outputLabel;
    private int value;
    Thread thread;
    private boolean start = true;

    /** Creates a new instance of Counter */
    public Counter() {
        setValue(0);
    }

    /** Creates a new instance of Counter */
    public Counter(JLabel output) {
        outputLabel = output;
        setValue(0);
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while (isStart()) {
            outputLabel.setText("Counter: " + getValue());
            try {
                thread.sleep(500);
                setValue(getValue() + 1);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}
 



Kelas ImagePanel


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

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

    private BufferedImage logo;

    /** Creates a new instance of ImagePanel */
    public ImagePanel() {
        try {
//            buka file
            File img = new File(" ");
//            baca image
            logo = ImageIO.read(img);
//            img.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(logo,
                0, 0, 400, 300,
                0, 0, logo.getWidth(null), logo.getHeight(null),
                null);
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame("Using Java2D");
        frame.setLayout(new BorderLayout());
        ImagePanel gc = new ImagePanel();
        frame.add(gc, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(600, 250);
        frame.setVisible(true);
    }
}




Kelas MyGraphic

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

package grafik;

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

/**
 *
 * @author admin
 */
public class MyGraphic extends JPanel{

    @Override
    public void paintComponent (Graphics g){
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillArc(100, 100, 100, 100, 0, 180);
    }

}



Kelas MyGraphic1


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

package grafik;

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

/**
 *
 * @author admin
 */
public class MyGraphic1 extends JPanel implements Runnable {
    Color warna = Color.red;

    public void run() {
           try{
               Thread.sleep(4000);
               warna = Color.green;
               this.repaint();
               System.out.println("Warna Jadi Berubah");
               Thread.sleep(4000);
               warna = Color.black;
               this.repaint();
               System.out.println("Warna Jadi Berubah");
           } catch (Interrupted ){

           }
    }


}





Kelas ShapePanel


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

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ShapePanel extends JPanel {

    public ShapePanel() {
        setBackground(Color.white);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.drawLine(0, 10, 50, 60);
        g.setColor(Color.red);
        g.drawRect(50, 10, 50, 50);
        g.setColor(new Color(255, 0, 0));
        g.fillOval(100, 10, 50, 50);
        g.setColor(new Color(0, 255, 0));
        g.fillArc(150, 10, 50, 50, 0, 180);
        Graphics2D g2 = (Graphics2D) g;
// fill RoundRectangle2D.Double
        GradientPaint redtowhite = new GradientPaint(200, 10, Color.red, 250, 10, Color.black);
        g2.setPaint(redtowhite);
        g2.fill(new RoundRectangle2D.Double(200, 10, 50, 50, 10, 10));
//g2.setPaint();
        g2.drawString("Filled RoundRectangle2D", 200, 80);
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame("Grafik 2 Dimensi");
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setLayout(new BorderLayout());
        ShapePanel shapePanel = new ShapePanel();
        frame.add(shapePanel, BorderLayout.CENTER);
        frame.setSize(450, 250);
        frame.setVisible(true);
    }
}




Kelas SimpleGUI


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

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SimpleGUI extends JFrame {

    private JButton clickButton, stopButton;
    private JLabel counterLabel;
    private Counter counter;

    /** Creates a new instance of SimpleGUI */
    public SimpleGUI() {
        clickButton = new JButton("Start");
        stopButton = new JButton("Stop");
        counterLabel = new JLabel("Counter: ");
        this.setLayout(new FlowLayout());
        this.add(clickButton);
        this.add(counterLabel);
        this.add(stopButton);
        this.setTitle("Thread application");
        this.setSize(300, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clickButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                clickAction();
            }
        });
        stopButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                stopAction();
            }
        });
    }

    public void clickAction() {
        counter = new Counter(counterLabel);
    }

    public void stopAction() {
        counter.setStart(false);
    }
//}

//public class SimpleGUITester {

    /** Creates a new instance of SimpleGUITester */
//    public SimpleGUITester() {
//    }

    public static void main(String[] args) {
        SimpleGUI test = new SimpleGUI();
    }
}





Kelas SimpleThread
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grafik;

public class SimpleThread extends Thread {

    public SimpleThread(String str) {
        super(str);
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i + " " + getName());
            try {
                sleep((long) (Math.random() * 1000));
            } catch (InterruptedException e) {
            }
        }
        System.out.println("DONE! " + getName());
    }

    public static void main(String[] args) {
        new SimpleThread("Bali").start();
        new SimpleThread("Jogja").start();
    }
}










LATIHAN

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

import javax.swing.JButton;
import javax.swing.JTextField;

/**
 *
 * @author admin
 */
public class No7 extends Thread {

    JTextField outputLabel;
    JButton StarStop;
    private int value;
    Thread thread;
    private boolean start = false;

    /** Creates a new instance of Counter */
    public No7() {
    }

    /** Creates a new instance of Counter */
    public No7(JTextField outputLabel, JButton StarStop) {
        this.outputLabel=outputLabel;
        this.StarStop=StarStop;
    }


    @Override
    public void  start(){
        thread = new Thread(this);
        if (start == false){
            thread.start();
            start = true;
            StarStop.setText("Stop");
        }else{
            start=false;
            StarStop.setText("Start");
        }
    }

    @Override
    public void run() {
        while (start) {
           value = (int) (Math.random()*1000);
           outputLabel.setText(String.valueOf(value));
            try {
                thread.sleep(500);
            } catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
    }

    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}




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

package Tugas;

/**
 *
 * @author admin
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Test_No7 m = new Test_No7();
        m.setVisible(true);
    }

}






Test_No7


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

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test_No7 extends JFrame {

    private JButton clickButton, stopButton;
    private JTextField counterLabel;
    private JPanel A;
    boolean start = true;
    private No7 counter;

    /** Creates a new instance of SimpleGUI */
    public Test_No7() {
        A = new JPanel();

        clickButton = new JButton("Start");
        counterLabel = new JTextField(10);
        counterLabel.setEditable(false);
        this.setLayout(new FlowLayout());
        this.add(clickButton);
        this.add(A);
        A.add(clickButton);
        A.add(counterLabel);
        this.setSize(300, 200);
        this.setVisible(true);
        this.setContentPane(A);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clickButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                No7();
            }
        });
    }

    public void No7() {
        if (start) {
            counter = new No7(counterLabel, stopButton);
            counter.start();
            start = false;
        } else if (start == false) {
            counter.start();
            start = true;
        }
    }

}

0 komentar:

Posting Komentar