首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

自个儿写的一个简单的applet的程序

2012-08-26 
自己写的一个简单的applet的程序先上图吧:功能是: 点击play后三个图片会随机的进行变化, 然后点击相应的底

自己写的一个简单的applet的程序
先上图吧:


功能是: 点击play后三个图片会随机的进行变化, 然后点击相应的底下的stop按钮,相应
的图片就会停止下来,上面的stop按钮是三个同时停下来。

做这个有下面的体会:

1. VisualSwing 在eclipse底下特别好用,记录一下
http://download.eclipse.org/tools/ve/updates/1.5.0/

2. 我在重载paint函数的时候, 忘记了super.paint(g), 导致了一些控件没有正确的被绘制

3.  在写线程的时候,忘记加了while(true)这样的代码了, 导致了线程直接退出了,这个说明基础知识还不是很牢固。

4. 对applet的声明周期稍微有点了解了,实际上repaint里面是调用了paint(g)函数的,但是一般是不介意直接调用paint(g),因为paint(g)是由AWT的主线程调用的。

5. 这个里面是没有声音的,另外对图像的处理也很粗糙,感觉一个东西做好还是需要很多心思的。



import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.JApplet;import javax.swing.JButton;import javax.swing.JPanel;public class StopGo extends JApplet {private static final long serialVersionUID = 4396077428436372737L;private final static int CHERRY_STOP = 50;private final static int CLUB_STOP = 60;private final static int SPADE_STOP = 30;private JPanel rootContentPanel = null;private JButton startButton = null;private JButton stompButton = null;private JButton stopClub = null;private JButton stopSpade = null;private JButton stopCherry = null;private Image[] images = new Image[3];private Thread cherryThread; private Thread clubThread;private Thread spadeThread;private boolean stopClubFlag = true;private boolean stopSpadeFlag = true;private boolean stopCherryFlag = true;private Image lastCherry = null;private Image lastClub = null; private Image lastSpade = null;/** * This method initializes this *  * @return void */public void init() {images[0] = getImage(getCodeBase(), "club.jpg");images[1] = getImage(getCodeBase(), "spade.jpg");images[2] = getImage(getCodeBase(), "cherry.jpg");lastClub = images[0];lastSpade = images[1];lastCherry = images[2];this.setSize(420, 290);this.setContentPane(getJContentPane());}public void paint(Graphics g) {super.paint(g);g.drawImage(lastClub, 60, 60, this);g.drawImage(lastSpade, 190, 60, this);g.drawImage(lastCherry, 320, 60, this);}/** * This method initializes jContentPane *  * @return javax.swing.JPanel */private JPanel getJContentPane() {if (rootContentPanel == null) {rootContentPanel = new JPanel();rootContentPanel.setLayout(null);rootContentPanel.add(getStartButton(), null);rootContentPanel.add(getStopButton(), null);rootContentPanel.add(getStopCherry(), null);rootContentPanel.add(getStopStod(), null);rootContentPanel.add(getStopValid(), null);}return rootContentPanel;}/** * This method initializes startButton *  * @return javax.swing.JButton */private JButton getStartButton() {if (startButton == null) {startButton = new JButton("Play");startButton.setBounds(new Rectangle(120, 18, 82, 32));}startButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {cherryThread = new DrawCherryThread();clubThread = new DrawClubThread();spadeThread = new DrawSpadeThread();cherryThread.start();clubThread.start();spadeThread.start();stopCherryFlag = false;stopClubFlag = false;stopSpadeFlag = false;startButton.setEnabled(false);stompButton.setEnabled(true);stopCherry.setEnabled(true);stopClub.setEnabled(true);stopSpade.setEnabled(true);}});return startButton;}/** * This method initializes stopButton *  * @return javax.swing.JButton */private JButton getStopButton() {if (stompButton == null) {stompButton = new JButton("Stop");stompButton.setBounds(new Rectangle(260, 18, 82, 32));stompButton.setEnabled(false);}stompButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {stopCherryFlag = true;stopClubFlag = true;stopSpadeFlag = true;startButton.setEnabled(true);stompButton.setEnabled(false);stopCherry.setEnabled(false);stopClub.setEnabled(false);stopSpade.setEnabled(false);}});return stompButton;}private void stopAllAndActivePlay(){if(stopCherryFlag && stopClubFlag && stopSpadeFlag) {startButton.setEnabled(true);stompButton.setEnabled(false);}}/** * This method initializes stopCherry *  * @return javax.swing.JButton */private JButton getStopCherry() {if (stopClub == null) {stopClub = new JButton("Stop");stopClub.setBounds(new Rectangle(46, 181, 82, 32));stopClub.setEnabled(false);}stopClub.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {stopClubFlag = true;stopClub.setEnabled(false);stopAllAndActivePlay();}});return stopClub;}/** * This method initializes stopStod *  * @return javax.swing.JButton */private JButton getStopStod() {if (stopSpade == null) {stopSpade = new JButton("Stop");stopSpade.setBounds(new Rectangle(182, 181, 91, 32));stopSpade.setEnabled(false);}stopSpade.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {stopSpadeFlag = true;stopSpade.setEnabled(false);stopAllAndActivePlay();}});return stopSpade;}/** * This method initializes stopValid *  * @return javax.swing.JButton */private JButton getStopValid() {if (stopCherry == null) {stopCherry = new JButton("Stop");stopCherry.setBounds(new Rectangle(316, 181, 91, 32));stopCherry.setEnabled(false);}stopCherry.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {stopCherryFlag = true;stopCherry.setEnabled(false);stopAllAndActivePlay();}});return stopCherry;}public class DrawClubThread extends Thread {@Overridepublic void run() {while (!stopClubFlag) {lastClub = images[new Random().nextInt(3)];repaint();try {Thread.sleep(CLUB_STOP);} catch (InterruptedException e) {e.printStackTrace();}}}}public class DrawCherryThread extends Thread {@Overridepublic void run() {while (!stopCherryFlag) {lastCherry = images[new Random().nextInt(3)];repaint();try {Thread.sleep(CHERRY_STOP);} catch (InterruptedException e) {e.printStackTrace();}}}}public class DrawSpadeThread extends Thread {@Overridepublic void run() {while (!stopSpadeFlag) {lastSpade = images[new Random().nextInt(3)];repaint();try {Thread.sleep(SPADE_STOP);} catch (InterruptedException e) {e.printStackTrace();}}}}}

热点排行