public class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
public SimpleGUITester() {
}
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
-------------------------------------------------------------------------------------
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(500, 100);
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);
}
}
Rabu, 21 Desember 2011
String
public class StringConstructors {
public static void main(String args[]) {
char charArray[] = {'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y'};
String s = new String("hello");
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);
if (s.equals("hello")) {
System.out.println("s is equals as \"hello\"");
} else {
System.out.println("s is not equals as \"hello\"");
}
if (s.equalsIgnoreCase("HELLO")) {
System.out.println("s is equals as \"hello\"");
} else {
System.out.println("s is not equals as \"hello\"");
}
if (s3.contains("day")) {
System.out.println("s3 contains \"day\"");
} else {
System.out.println("s3 do not contains \"day\"");
}
String s5 = s.concat(" " + s3);
System.out.println(s5);
String s6 = s5.replaceAll("hello", "happy");
System.out.println(s6);
String[] s7 = new String[3];
s7 = s6.split(" ");
for (int i = 0; i < s7.length; i++) {
System.out.println(s7[i]);
}
StringTokenizer tokens = new StringTokenizer(s6);
System.out.println("Number of Token =" + tokens.countTokens());
while (tokens.hasMoreTokens()) {
System.out.println(tokens.nextToken());
}
}
public static void main(String args[]) {
char charArray[] = {'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y'};
String s = new String("hello");
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);
if (s.equals("hello")) {
System.out.println("s is equals as \"hello\"");
} else {
System.out.println("s is not equals as \"hello\"");
}
if (s.equalsIgnoreCase("HELLO")) {
System.out.println("s is equals as \"hello\"");
} else {
System.out.println("s is not equals as \"hello\"");
}
if (s3.contains("day")) {
System.out.println("s3 contains \"day\"");
} else {
System.out.println("s3 do not contains \"day\"");
}
String s5 = s.concat(" " + s3);
System.out.println(s5);
String s6 = s5.replaceAll("hello", "happy");
System.out.println(s6);
String[] s7 = new String[3];
s7 = s6.split(" ");
for (int i = 0; i < s7.length; i++) {
System.out.println(s7[i]);
}
StringTokenizer tokens = new StringTokenizer(s6);
System.out.println("Number of Token =" + tokens.countTokens());
while (tokens.hasMoreTokens()) {
System.out.println(tokens.nextToken());
}
}
SleepingThread
public class SleepingThread extends Thread
{
final public static int END=100;
final public static int TEN_SECONDS=1000;
public void run()
{
for(int x=1;x<=END;x++)
{
System.out.print(x+" ");
try
{
if (x%25==0){
System.out.print("\n");
sleep(TEN_SECONDS);
}
}
catch(InterruptedException e){
}
}
}
}
{
final public static int END=100;
final public static int TEN_SECONDS=1000;
public void run()
{
for(int x=1;x<=END;x++)
{
System.out.print(x+" ");
try
{
if (x%25==0){
System.out.print("\n");
sleep(TEN_SECONDS);
}
}
catch(InterruptedException e){
}
}
}
}
thread String
public class TwoStrings2 {
synchronized static void print(String str1, String str2) {
System.out.print(str1);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {/* */
}
System.out.println(str2);
}
}
class PrintStringsThread implements Runnable {
//Thread thread;
String str1, str2;
PrintStringsThread(String str1, String str2) {
this.str1 = str1;
this.str2 = str2;
Thread thread = new Thread(this);
thread.start();
}
public void run() {
TwoStrings.print(str1, str2);
}
}
class TestThread {
public static void main(String args[]) {
new PrintStringsThread("Hello ", "there.");
new PrintStringsThread("How are ", "you?");
new PrintStringsThread("Thank you ", "very much!");
}
}
synchronized static void print(String str1, String str2) {
System.out.print(str1);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {/* */
}
System.out.println(str2);
}
}
class PrintStringsThread implements Runnable {
//Thread thread;
String str1, str2;
PrintStringsThread(String str1, String str2) {
this.str1 = str1;
this.str2 = str2;
Thread thread = new Thread(this);
thread.start();
}
public void run() {
TwoStrings.print(str1, str2);
}
}
class TestThread {
public static void main(String args[]) {
new PrintStringsThread("Hello ", "there.");
new PrintStringsThread("How are ", "you?");
new PrintStringsThread("Thank you ", "very much!");
}
}
main thread
public class ThreadKotak extends Thread {
// execute application
private static Kotak shapes2JPanel;
public static void main(String args[]) {
// create frame for Shapes2JPanel
JFrame frame = new JFrame("Drawing 2D Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shapes2JPanel = new Kotak();
frame.add(shapes2JPanel); // add shapes2JPanel to frame
frame.setBackground(Color.WHITE); // set frame background color
frame.setSize(400, 400); // set frame size
frame.setVisible(true); // display frame
ThreadKotak sh = new ThreadKotak();
sh.start();
} // end main
public void run() {
while (true) {
try {
this.sleep(500);
} catch (InterruptedException ie) {
break;
}
shapes2JPanel.repaint();
}
}
}
// execute application
private static Kotak shapes2JPanel;
public static void main(String args[]) {
// create frame for Shapes2JPanel
JFrame frame = new JFrame("Drawing 2D Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shapes2JPanel = new Kotak();
frame.add(shapes2JPanel); // add shapes2JPanel to frame
frame.setBackground(Color.WHITE); // set frame background color
frame.setSize(400, 400); // set frame size
frame.setVisible(true); // display frame
ThreadKotak sh = new ThreadKotak();
sh.start();
} // end main
public void run() {
while (true) {
try {
this.sleep(500);
} catch (InterruptedException ie) {
break;
}
shapes2JPanel.repaint();
}
}
}
thread
public class Kotak extends JPanel {
private static GeneralPath gp;
int jum = 1;
// draw general paths
public void paintComponent(Graphics g) {
super.paintComponent(g);
Random random = new Random();
Graphics2D g2d = (Graphics2D) g;
g2d.translate(200, 200);
if (jum == 18) {
jum = 0;
}
for (int count = 1; count <= jum; count++) {
g2d.rotate(Math.PI / 3.0);
g2d.setColor(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
g2d.fillRect(0, 0, 100, 100);
// g2d.fill( starImage() );
}
jum++;
}
//bisa pake ini juga
// public GeneralPath starImage()
// {
//
// int xPoints[] = { 0, 80,80, 0 };
// int yPoints[] = { 0, 0,80, 80};
//
// GeneralPath Kotak = new GeneralPath();
//
//
// Kotak.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
//
//
// for ( int count = 1; count < xPoints.length; count++ )
// Kotak.lineTo( xPoints[ count ], yPoints[ count ] );
//
// Kotak.closePath(); // close the shape
// return Kotak;
// }
}
private static GeneralPath gp;
int jum = 1;
// draw general paths
public void paintComponent(Graphics g) {
super.paintComponent(g);
Random random = new Random();
Graphics2D g2d = (Graphics2D) g;
g2d.translate(200, 200);
if (jum == 18) {
jum = 0;
}
for (int count = 1; count <= jum; count++) {
g2d.rotate(Math.PI / 3.0);
g2d.setColor(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
g2d.fillRect(0, 0, 100, 100);
// g2d.fill( starImage() );
}
jum++;
}
//bisa pake ini juga
// public GeneralPath starImage()
// {
//
// int xPoints[] = { 0, 80,80, 0 };
// int yPoints[] = { 0, 0,80, 80};
//
// GeneralPath Kotak = new GeneralPath();
//
//
// Kotak.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
//
//
// for ( int count = 1; count < xPoints.length; count++ )
// Kotak.lineTo( xPoints[ count ], yPoints[ count ] );
//
// Kotak.closePath(); // close the shape
// return Kotak;
// }
}
Langganan:
Postingan (Atom)