package Graphics;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ThreadGraphic extends JPanel implements Runnable{
// Counter counter ;
int counter=5;
Thread thread;
private boolean start=true;
private int value;
public ThreadGraphic() {
// counter= new Counter();
setBackground(Color.white);
thread = new Thread(this);
thread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (counter % 2 == 0) {
g.setColor(Color.magenta);
g.fillOval(100, 50, 100, 100);
counter-=1;
}else{
g.setColor(Color.black);
g.fillOval(100, 50, 100, 100);
counter+=1;
}
}
public void run() {
while(true){
// outputLabel.setText("Counter: "+getValue());
try {
thread.sleep(200);
this.repaint();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private boolean isStart() {
return start;
}
private int getValue() {
return value;
}
private void setValue(int i) {
this.value=value;
}
public static void main(String args[]){
JFrame frame = new JFrame( "Grafik 2 Dimensi" );
frame.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit( 0 );
}
});
frame.setLayout(new BorderLayout());
ThreadGraphic graph = new ThreadGraphic();
frame.add(graph,BorderLayout.CENTER);
frame.setSize( 450, 250 );
frame.setVisible(true);
AnotherCounter counter = new AnotherCounter();
}
}
apa-aja-lah~~
Rabu, 21 Desember 2011
Thread_Kelas SimpleThread
package Graphics;
/* SimpleThread.java */
public class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
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());
}
}
/* TwoThreadsTest.java */
/* SimpleThread.java */
public class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
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());
}
}
/* TwoThreadsTest.java */
Thread_Kelas ShapePanel
package Graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class ShapePanel extends JPanel {
public ShapePanel(){
setBackground(Color.white);
}
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(){
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);
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class ShapePanel extends JPanel {
public ShapePanel(){
setBackground(Color.white);
}
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(){
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);
}
}
Thread_Kelas RandomCounter
package Graphics;
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;
import javax.swing.JTextField;
public class RandomCounter implements Runnable {
JTextField outputLabel;
private int value;
Thread thread;
private boolean start = true;
/** Creates a new instance of Counter */
public RandomCounter() {
setValue(0);
}
/** Creates a new instance of Counter */
public RandomCounter(JTextField output) {
outputLabel = output;
setValue(0);
thread = new Thread(this);
thread.start();
}
public void run() {
while(isStart()){
outputLabel.setText("Counter: "+getValue());
try {
thread.sleep(200);
setValue(getValue()+(int)(Math.random()*1000));
} 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;
}
}
class SimpleGUI extends JFrame{
private JButton clickButton,stopButton;
private JTextField counterLabel;
private RandomCounter counter;
/** Creates a new instance of SimpleGUI */
public SimpleGUI() {
clickButton = new JButton("Start");
stopButton = new JButton("Stop");
counterLabel = new JTextField("Counter: ",20);
this.setLayout(new FlowLayout());
this.add(clickButton);
this.add(stopButton);
this.add(counterLabel);
this.setTitle("Random Counter");
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 RandomCounter(counterLabel);
}
public void stopAction(){
counter.setStart(false);
}
}
class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
public SimpleGUITester() {
}
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
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;
import javax.swing.JTextField;
public class RandomCounter implements Runnable {
JTextField outputLabel;
private int value;
Thread thread;
private boolean start = true;
/** Creates a new instance of Counter */
public RandomCounter() {
setValue(0);
}
/** Creates a new instance of Counter */
public RandomCounter(JTextField output) {
outputLabel = output;
setValue(0);
thread = new Thread(this);
thread.start();
}
public void run() {
while(isStart()){
outputLabel.setText("Counter: "+getValue());
try {
thread.sleep(200);
setValue(getValue()+(int)(Math.random()*1000));
} 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;
}
}
class SimpleGUI extends JFrame{
private JButton clickButton,stopButton;
private JTextField counterLabel;
private RandomCounter counter;
/** Creates a new instance of SimpleGUI */
public SimpleGUI() {
clickButton = new JButton("Start");
stopButton = new JButton("Stop");
counterLabel = new JTextField("Counter: ",20);
this.setLayout(new FlowLayout());
this.add(clickButton);
this.add(stopButton);
this.add(counterLabel);
this.setTitle("Random Counter");
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 RandomCounter(counterLabel);
}
public void stopAction(){
counter.setStart(false);
}
}
class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
public SimpleGUITester() {
}
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
Thread_Kelas ImagePanel
package Graphics;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.swing.*;
public class ImagePanel extends JPanel{
private BufferedImage logo;
/** Creates a new instance of ImagePanel */
public ImagePanel() {
try {
// buka file. Cari file gambar lain di computer anda
File img = new File("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\winter.jpg");
// baca image
logo = ImageIO.read(img);
//img.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
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);
}
}
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.swing.*;
public class ImagePanel extends JPanel{
private BufferedImage logo;
/** Creates a new instance of ImagePanel */
public ImagePanel() {
try {
// buka file. Cari file gambar lain di computer anda
File img = new File("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\winter.jpg");
// baca image
logo = ImageIO.read(img);
//img.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
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);
}
}
Thread_Kelas Counter
package Graphics;
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 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(200);
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;
}
}
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);
}
}
class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
public SimpleGUITester() {
}
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
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 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(200);
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;
}
}
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);
}
}
class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
public SimpleGUITester() {
}
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
Thread_Kelas AnotherSimpleCounter
package Graphics;
/* AnotherSimpleThread.java */
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 */
/* AnotherSimpleThread.java */
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 */
Langganan:
Postingan (Atom)