標題:
龜兔賽跑 (一)
[打印本頁]
作者:
周政輝
時間:
2018-5-26 10:37
標題:
龜兔賽跑 (一)
本帖最後由 周政輝 於 2018-5-26 11:05 編輯
利用多執行緒,模擬龜兔賽跑。烏龜跑得慢,但持續而不間斷;兔子跑得快,但會不定時的停下休息。同時進行中的兩個執行緒不會互相干擾。參考執行畫面如下:
[attach]4146[/attach]
[attach]4147[/attach]
[attach]4148[/attach]
作者:
蔡季樺
時間:
2018-5-26 11:35
import javax.swing.JLabel;
public class MyThread1 extends Thread{
JLabel lblNewLabel = null;
public MyThread1(JLabel lblNewLabel) {
this.lblNewLabel = lblNewLabel;
}
public void run()
{
int x = lblNewLabel.getLocation().x;
int sleep=0;
while(true)
{
x+=20;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
lblNewLabel.setLocation(x, 40);
sleep=(int)(Math.random()*3000);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
}
}
}
複製代碼
import javax.swing.JLabel;
public class MyThread2 extends Thread{
JLabel lblNewLabel_1 = null;
public MyThread2(JLabel lblNewLabel_1) {
this.lblNewLabel_1 = lblNewLabel_1;
}
public void run()
{
int x = lblNewLabel_1.getLocation().x;
while(true)
{
x+=2;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
lblNewLabel_1.setLocation(x, 132);
}
}
}
複製代碼
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class main extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
main frame = new main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\r.gif"));
lblNewLabel.setBounds(22, 40, 75, 64);
contentPane.add(lblNewLabel);
MyThread1 t = new MyThread1(lblNewLabel);
t.start();
JLabel lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\t.gif"));
lblNewLabel_1.setBounds(10, 132, 87, 64);
contentPane.add(lblNewLabel_1);
MyThread2 t2 = new MyThread2(lblNewLabel_1);
t2.start();
}
}
複製代碼
作者:
林侑成
時間:
2018-5-26 11:50
package asdf;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.Window;
import net.miginfocom.swing.MigLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EmptyBorder;
public class Main {
private JPanel contentPane;
protected Object frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
((Window) window.frame).setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\r.gif"));
label.setBounds(22, 40, 75, 64);
contentPane.add(label);
MyThread1 t = new MyThread1(label);
t.start();
JLabel label_1 = new JLabel("");
label_1.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\r.gif"));
label_1.setBounds(10, 132, 87, 64);
contentPane.add(label_1);
MyThread2 t2 = new MyThread2(label_1);
t2.start();
}
}
複製代碼
package asdf;
import javax.swing.JLabel;
public class MyThread1 extends Thread{
JLabel label = null;
public MyThread1(JLabel label) {
this.label = label;
}
public void run()
{
int x = label.getLocation().x;
int sleep=0;
while(true)
{
x+=20;
try {
Thread.sleep(75);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
label.setLocation(x, 40);
sleep=(int)(Math.random()*3000);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
}
}
}
複製代碼
package asdf;
import javax.swing.JLabel;
public class MyThread2 extends Thread{
JLabel label_1 = null;
public MyThread2(JLabel label_1) {
this.label_1 = label_1;
}
public void run()
{
int x = label_1.getLocation().x;
while(true)
{
x+=2;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
label_1.setLocation(x, 132);
}
}
}
複製代碼
作者:
黃茂勛
時間:
2018-5-26 11:53
本帖最後由 黃茂勛 於 2018-5-26 11:55 編輯
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class haung20180526 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
haung20180526 frame = new haung20180526();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public haung20180526() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 704, 381);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel rabbit = new JLabel("");
rabbit.setIcon(new ImageIcon(haung20180526.class.getResource("/bbs/istak/org/tw/r.gif")));
rabbit.setBounds(60, 38, 67, 73);
contentPane.add(rabbit);
int x_r = rabbit.getLocation().x;
RunRabbit rb = new RunRabbit(x_r,rabbit);
rb.start();
JLabel turtle = new JLabel("");
turtle.setIcon(new ImageIcon(haung20180526.class.getResource("/bbs/istak/org/tw/t.gif")));
turtle.setBounds(60, 204, 82, 66);
contentPane.add(turtle);
int x_t = turtle.getLocation().x;
Runturtle rt = new Runturtle(x_t,turtle);
rt.start();
}
}
複製代碼
package bbs.istak.org.tw;
import javax.swing.JLabel;
public class RunRabbit extends Thread{
int x_r = 0;
JLabel rabbit = null;
RunRabbit(int x_r, JLabel rabbit)
{
this.x_r = x_r;
this.rabbit = rabbit;
}
public void run()
{
while(x_r <= 537)
{
int r = (int)(Math.random()*10);
x_r+=6;
if(r==1 || r==5 || r==9)
try {
Thread.sleep(1500);
} catch (InterruptedException e) {}
//System.out.println(x_r);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
rabbit.setLocation(x_r, 38);
}
}
}
複製代碼
package bbs.istak.org.tw;
import javax.swing.JLabel;
public class Runturtle extends Thread{
int x_t = 0;
JLabel turtle = null;
Runturtle(int x_t, JLabel turtle)
{
this.x_t = x_t;
this.turtle = turtle;
}
public void run()
{
while(x_t <= 537)
{
x_t+=3;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
//System.out.println(x_t);
turtle.setLocation(x_t, 204);
}
}
}
複製代碼
作者:
張健勳
時間:
2018-6-1 21:21
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class Rabbit_and_Turtle_Run extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Rabbit_and_Turtle_Run frame = new Rabbit_and_Turtle_Run();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Rabbit_and_Turtle_Run() {
setTitle("\u9F9C\u5154\u8CFD\u8DD1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 898, 269);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/r.gif")));
lblNewLabel.setBounds(22, 40, 75, 64);
contentPane.add(lblNewLabel);
rabbitTH t = new rabbitTH(lblNewLabel);
t.start();
JLabel lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/t.gif")));
lblNewLabel_1.setBounds(22, 132, 75, 64);
contentPane.add(lblNewLabel_1);
turtleTH t2 = new turtleTH(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("New label");
lblNewLabel_2.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
lblNewLabel_2.setBounds(738, 85, 92, 158);
contentPane.add(lblNewLabel_2);
JLabel label = new JLabel("New label");
label.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
label.setBounds(738, 0, 92, 142);
contentPane.add(label);
JLabel label_1 = new JLabel("New label");
label_1.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
label_1.setBounds(738, 10, 92, 208);
contentPane.add(label_1);
t2.start();
}
}
class rabbitTH extends Thread{
JLabel lblNewLabel = null;
public rabbitTH(JLabel lblNewLabel) {
this.lblNewLabel = lblNewLabel;
}
public void run()
{
int x = lblNewLabel.getLocation().x;
int sleep=0;
while(true)
{
x+=20;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
lblNewLabel.setLocation(x, 40);
sleep=(int)(Math.random()*3000);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
}
}
}
class turtleTH extends Thread{
JLabel lblNewLabel_1 = null;
public turtleTH(JLabel lblNewLabel_1) {
this.lblNewLabel_1 = lblNewLabel_1;
}
public void run()
{
int x = lblNewLabel_1.getLocation().x;
while(true)
{
x+=2;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
lblNewLabel_1.setLocation(x, 132);
}
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-6-2 10:20
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
public class RTR extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RTR frame = new RTR();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RTR() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 252);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel flag = new JLabel("");
flag.setIcon(new ImageIcon(RTR.class.getResource("/bbs/istak/org/tw/flag (2).png")));
flag.setBounds(374, 180, 24, 24);
contentPane.add(flag);
JLabel rabbit = new JLabel("New label");
rabbit.setIcon(new ImageIcon(RTR.class.getResource("/bbs/istak/org/tw/r.gif")));
rabbit.setBounds(10, 10, 58, 55);
contentPane.add(rabbit);
Random sleep = new Random();
JLabel turtle = new JLabel("New label");
turtle.setIcon(new ImageIcon(RTR.class.getResource("/bbs/istak/org/tw/t.gif")));
turtle.setBounds(10, 88, 82, 46);
contentPane.add(turtle);
JLabel result = new JLabel("going on");
result.setBounds(10, 180, 112, 24);
contentPane.add(result);
Thread tr = new Thread(){
public void run(){
for(int i = rabbit.getLocation().x ; i <= flag.getLocation().x+flag.getWidth() ; i+=20){
rabbit.setLocation(i, 10);
try {
Thread.sleep((sleep.nextInt(5)+1)*1000);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
if(rabbit.getLocation().x == flag.getLocation().x+flag.getWidth()){
result.setText("rabbit win");
break;
}
}
}
};
Thread tt = new Thread(){
public void run(){
for(int i = turtle.getLocation().x ; i <= flag.getLocation().x+flag.getWidth() ; i+=3){
turtle.setLocation(i, 88);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
if(turtle.getLocation().x == flag.getLocation().x+flag.getWidth()){
result.setText("turtle win");
break;
}
}
}
};
tr.start();
tt.start();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2