1. 程式人生 > >java實訓項目-模擬自動擋汽車

java實訓項目-模擬自動擋汽車

enter 命令 dia out rake 提取 邊框 wid 不能

Java程序設計課程設計項目名稱

?

項目名稱:自動擋汽車操作模擬系統的設計與實現

英文名稱:Design and Implementation of Operation Simulation System aboutAutomatic Vehicle

1、功能描述:

1)汽車的操作正常為啟動(或熄火)、掛檔、制動、油門控制等操作;

2)掛檔實現自動擋的4檔操作,既駐車檔、倒車檔、空檔、行車檔;

3)使用圖形界面進行操作,汽車的狀態在圖形界面上顯示出來;

4)操作流程不合理,要有相應的控制;

5)附加功能:

(1)汽車的油也要進行控制,發動機需要消耗汽油,提供汽油報警功能;

(2)汽車的行程數進行控制,用戶界面顯示當前行程數;

6)正常操作流程為:啟動->剎車->掛檔->放剎車->汽車運動->加油門->剎車->駐車檔->熄火。行駛中嚴禁換擋,行駛檔位不能啟動。

參考文檔:

【1】系統類的API文檔,見CarApp.jar文件內的doc下的index.html文件

【2】系統運行效果,使用控制臺,輸入命令為:java –jar CarApp.jar

【3】汽車的運行可以選擇gif文件,從網上搜索關鍵詞“車運動? gif”或“動漫小車”等關鍵字搜索你喜歡的gif小車運動圖片,然後使用“工具GIF幀提取器 V1.1 綠色版.rar”提出圖片中的每幀圖片。

【4】系統的運行效果參考圖見圖1.

圖1:系統運行效果圖??

主要有四個類

測試類

public class CarApp {
public static void main(String[] args){
new MyFrame();
}
}

窗口類

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

//import javafx.beans.value.ChangeListener;
import javax.swing.;
import javax.swing.event.
;

public class MyFrame extends JFrame implements ActionListener{
private JPanel pl1;
private JPanel pl2;
private JPanel pl3;
private JToggleButton start;
private JToggleButton barke;
private CardLayout card = null;
private MytomaticCar car;
private JRadioButton rb1;
private JRadioButton rb2;
private JRadioButton rb3;
private JRadioButton rb4;
private JSlider s2;//一個讓用戶以圖形方式在有界區間內通過移動滑塊來選擇值的組件。
public JTextArea area1;
private JTextArea area;
private JButton refresh,jiayou;
private int s;
private JTextArea ll3;
public MyFrame(){
//大容器設置
super("自動擋小車模擬駕駛系統");
pack();//調整此窗口的大小,以適合其子組件的首選大小和布局
//setDefaultCloseOperation(3);//默認關閉狀態
setSize(500, 680);
Dimension screen = //屏幕居中
Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)((screen.getWidth() - getWidth()) / 2.0D), (int)((screen.getHeight() - getHeight()) / 2.0D));

    this.car= new MytomaticCar(new Engine());//創建自動控制對象並以發動機作為參數調用相應的構造函數
    this.card = new CardLayout();

    //容器上部分
    this.pl1= new JPanel(card);//創建容器對象采用卡片布局
    add(this.pl1, BorderLayout.NORTH);
    this.pl1.setBorder(BorderFactory.createTitledBorder("汽車狀態顯示"));//BorderFactory創建一個具有指定顏色的線邊框。
    ImageIcon[] ims = new ImageIcon[25];//創建一個數組未初始化的圖像圖標。
    int i = 0;
    String[] imfn = new String[25];//用來存儲相對路徑制定文件名
    JPanel[] pls = new JPanel[25];//面板數組裝卡片
    for (i = 0; i < 25; i++) imfn[i] = (".\\images\\Frame" + i + ".jpg");
    for (i = 0; i < 25; i++) ims[i] = new ImageIcon(imfn[i]);//根據指定的文件創建一個 ImageIcon,指定 String 可以是一個文件名或是一條文件路徑  
    for (i = 0; i < 25; i++) {
       pls[i] = new JPanel();//創建對象並加載圖片到面板上
       pls[i].add(new JLabel(ims[i]));//把圖片放在標簽中再放到面板中顯示
       this.pl1.add(pls[i], "p" + i);//加到面板中去 同時命名
        }

    //容器中間部分(顯示耗油量和裏程數)
    this.pl3 = new JPanel();
    this.pl3.setBorder(BorderFactory.createTitledBorder("數據顯示"));           
    add(this.pl3,BorderLayout.CENTER);

    JLabel jl1 = new JLabel("油量(L):");
    this.area1 = new JTextArea(1,5);
    this.ll3=new JTextArea(1,7);
    this.area1.setFont(new Font("華文行楷",Font.BOLD,20));
    this.ll3.setFont(new Font("華文行楷",Font.BOLD,20));
    this.ll3.setEditable(false);
    this.ll3.setBackground(Color.yellow);
    pl3.add(jl1); 
    pl3.add(ll3);
    this.jiayou=new JButton("加油");
    this.jiayou.addActionListener(this);
    pl3.add(this.jiayou);
    pl3.add(area1);

    JLabel jl2 = new JLabel("裏程(KM):");       
    pl3.add(jl2); 
    this.area= new JTextArea(1,5);
    this.area.setEditable(false);
    this.area.setBackground(Color.blue);
    this.area.setForeground(Color.RED);
    this.area.setFont(new Font("華文行楷",Font.BOLD,20));
    pl3.add(this.area);
    this.refresh=new JButton("重置");
    this.refresh.addActionListener(this);
    pl3.add(this.refresh);

    //容器下部分
    this.pl2 = new JPanel();
    this.pl2.setBorder(BorderFactory.createTitledBorder("汽車駕駛室"));          
    add(this.pl2,BorderLayout.SOUTH);

    //四個小部件的添加
    JPanel m1=new JPanel();
    m1.setBorder(BorderFactory.createTitledBorder("引擎發動器"));
    m1.setLayout(new GridLayout(7,2));//網格布局管理器
    this.start=new JToggleButton("啟動");//JToggleButton按鈕按下去會陷下 去,不會彈回來,除非你再按一次
    this.start.addActionListener(this);//添加監聽器
    m1.add(new JLabel(""));
    m1.add(this.start);
    this.pl2.add(m1);
    this.pl2.add(new JLabel("    "));

    JPanel m2 = new JPanel();
    m2.setBorder(BorderFactory.createTitledBorder("制動器"));
    m2.setLayout(new GridLayout(7,2));//網格布局管理器
    this.barke=new JToggleButton("制動器");//JToggleButton按鈕按下去會陷下 去,不會彈回來,除非你再按一次
    this.barke.addActionListener(this);//添加監聽器
    m2.add(new JLabel("   "));
    m2.add(this.barke);
    this.pl2.add(m2);
    this.pl2.add(new JLabel("    "));

    JPanel m3 = new JPanel();
    m3.setBorder(BorderFactory.createTitledBorder("檔位控制器"));
    m3.setLayout(new GridLayout(1,3));
    //需要再創一個小面板才能實現排序
    JPanel m33=new JPanel();
    m33.setLayout(new GridLayout(8,2));
    this.rb1 = new JRadioButton("P");
    this.rb1.addActionListener(this);
    this.rb1.setSelected(true);//默認位置
    this.rb2 = new JRadioButton("R");
    this.rb2.addActionListener(this);
    this.rb3 = new JRadioButton("N");
    this.rb3.addActionListener(this);
    this.rb4 = new JRadioButton("D");
    this.rb4.addActionListener(this);
    ButtonGroup grp1 = new ButtonGroup();//單選按鈕
    m3.add(new JLabel("   "));
    grp1.add(this.rb1); grp1.add(this.rb2); grp1.add(this.rb3); grp1.add(this.rb4);
    m33.add(this.rb1);m33.add(new JLabel(""));m33.add(this.rb2); m33.add(new JLabel(""));
    m33.add(this.rb3);m33.add(new JLabel("")); m33.add(this.rb4);
    m3.add(m33);
    this.pl2.add(m3);
    this.pl2.add(new JLabel("     "));

    JPanel m4 = new JPanel();
    m4.setBorder(BorderFactory.createTitledBorder("油門控制器"));
    m4.setLayout(new GridLayout(0, 2));
    this.s2 = new JSlider(1, 0, 200, 0);
    this.s2.setForeground(Color.ORANGE);
    this.s2.setMajorTickSpacing(50);
    this.s2.setMinorTickSpacing(10);
    this.s2.setPaintTicks(true);
    this.s2.setPaintLabels(true);
    this.s2.setPaintTrack(true);
    this.s2.setSnapToTicks(true);
    this.s2.addChangeListener(new ChangeListener()//事件監聽器
    {
      public void stateChanged(ChangeEvent e)
      {
        int n = MyFrame.this.s2.getValue();
        MyFrame.this.car.addSpeed(n);
      }
    });
    m4.add(s2);
    this.pl2.add(m4);
    this.pl2.add(new JLabel("     "));

    int nn=this.car.getoil();
    this.ll3.setText(""+nn);
    setVisible(true);//可視 一定放在所有組件放好之後  否則有些不會顯示
    showautocarspeed();//控制顯示車速
 }
 public void actionPerformed(ActionEvent e){
     Object o = e.getSource();//獲得按鈕的變化
        if ((o instanceof JRadioButton)) {//單選按鈕變化
          JRadioButton rbt = (JRadioButton)e.getSource();//獲取按鈕狀態
          String ch = rbt.getText();//獲得按鈕的字符
          String s = "PRND";
          int n = s.indexOf(ch);//返回該字符在字符串中出現的位置
          int gear = this.car.getGear();//獲得檔位
          if (!this.car.getParkBrake()){//判斷制動器狀態
            JOptionPane.showMessageDialog(this, "制動時才能換擋!", "非法操作", 0);

            switch (gear) {//制動時候才能換擋的操作若換擋,則不會改變單選按鈕的選中狀態
            case 0:
              this.rb1.setSelected(true);//將該按鈕設置為選中模式
              break;
            case 1:
              this.rb2.setSelected(true);
              break;
            case 2:
              this.rb3.setSelected(true);
              break;
            case 3:
              this.rb4.setSelected(true);
            default:
              break;
            }
          }else {
            this.car.engageGear(n);//掛檔操作
          }
        }else if ((o instanceof JToggleButton)) {//判斷按鈕是否是JToggleButton
          JToggleButton jtb = (JToggleButton)o;
          String name = jtb.getText();
          if (name.equals("啟動")) {
            this.car.startEngine();
            this.s2.setValue(1);
            jtb.setText("熄火");
          }else if(name.equals("制動器")) {
             if (!this.car.ParkBrake) {
              this.car.brake();
              this.s2.setValue(1);
              jtb.setText("制動中");
             } 
          } else if (name.equals("熄火")){
            if(this.car.getGear()!=0){
                JOptionPane.showMessageDialog(this, "停車擋才能熄火!", "非法操作", 0);
                this.start.setSelected(true);

            }else{
                jtb.setText("啟動");
                this.rb1.setSelected(true);
                this.car.flameout();
            }               
          }else if(name.equals("制動中")){
              this.car.freeBrake();
              jtb.setText("制動器");
          }
       }else if((o instanceof JButton)){
           JButton jtb = (JButton)o;
           String name = jtb.getText();
           if((!this.car.getParkBrake()) && (this.car.getEngineThread() != null) && (this.car.getEngineThread().isAlive())){//判斷狀態
               JOptionPane.showMessageDialog(this, "運動狀態不能進行此操作", "非法操作", 0);
           }else if(name.equals("重置")){
                this.area.setText("0");                
                this.s=0;
           }else if(name.equals("加油")&& (this.car.getEngineThread() == null)){
                String sss=area1.getText();
                int n=Integer.parseInt(sss);       
                int nn=this.car.getoil();
                if(nn<500){
                    if(n+nn>=500){
                        this.ll3.setText(""+500);
                    }else{
                        this.car.addOil(n);
                    int ppp=this.car.getoil();
                    this.ll3.setText(""+ppp);
                    }                       
                }else if(nn==500){
                     JOptionPane.showMessageDialog(this, "油量已滿", "非法操作", 0);
                }                      
           }               
       }
    }    
 public void showautocarspeed(){
        while (true){
            if(this.car.getEngineThread() != null){
              int n=this.car.getoil();//顯示油量
              this.ll3.setText(""+n);    
            }
            if((this.car.getoil()==69)||(this.car.getoil()==70)){
                JOptionPane.showMessageDialog(this, "油量不足", "非法操作", 0);
            }
            if(this.car.getoil()==0){
                this.start.setText("啟動");
                this.start.setSelected(false);
                this.car.flameout();
            }                     
          try{
            Thread.sleep(1411 - this.car.getSpeed());

            if ((!this.car.getParkBrake()) && (this.car.getEngineThread() != null) && (this.car.getEngineThread().isAlive())) {
                this.area.setText(""+s);    //顯示裏程      
              if (this.car.getGear() == 1) {//後退擋
                this.card.previous(this.pl1);
                this.s=s+1;

                continue;
              }
              if (this.car.getGear() == 3) {//前進檔
                this.card.next(this.pl1);
                this.s=s+1;
                continue;
              }
            }
          }catch (InterruptedException e){
            e.printStackTrace();
          } 

        }
 }

}

引擎類

public class Engine implements Runnable{//多線程,汽車運行是發動機需要啟動

public int speed = 0;
private MytomaticCar car;

public void setCar(MytomaticCar car){//在汽車上安裝發動機
   this.car = car;
}

public void run(){
    while (true){
      try{
        Thread.sleep(2000-this.car.getSpeed());
      }
      catch (InterruptedException e){
        e.printStackTrace();
      }
      if(this.car.innage > 0) {
        this.car.innage -= 1;
        if(!this.car.getParkBrake()&&this.car.innage > 0)
          if (this.car.getGear() == 1)
          {
            this.car.innage -= 1;
          }
          else if (this.car.getGear() == 3&&this.car.innage > 1)
          {
            this.car.innage -= 2;
          }
      }else{
        try{
          this.car.flameout();
        }catch (Exception localException){

        }
      }

      if (this.car.innage >= 70)
        continue;           
    }
  }

}
控制室類

public class MytomaticCar {
public static int innage = 500;
private Engine engine;
private Thread engineThread;
public static int P = 0;
public static final int R = 1;
public static final int N = 2;
public static final int D = 3;
public boolean ParkBrake = false;
private int gear = 0;

  public MytomaticCar(){//無參構造方法
    this.engine = null;
  }
  public MytomaticCar(Engine engine) {//有參構造方法
    this.engine = engine;
    engine.setCar(this);
  }

  public Thread getEngineThread(){//獲得發動機線程
         return this.engineThread;
  }
   public void setEngine(Engine engine){//安裝發動機
        this.engine = engine;
        engine.setCar(this);
    }

  public int getoil(){//獲取油量
      return this.innage;
  }
  public void addOil(int value){//加油操作
      if(this.innage<500){
           this.innage+= value;
      }
  }

  public boolean startEngine(){//啟動發動
        if (this.engineThread == null) {
          this.engineThread = new Thread(this.engine);
        }
        if (!this.engineThread.isAlive()) {
          this.engineThread.start();
          System.out.println("發動機啟動正常!");
        }
        return true;
  }
  public void flameout(){//熄火操作
        try{
          if (this.engineThread.isAlive())
            this.engineThread.stop();
        }catch (Exception localException) {
        }finally {//初始化操作
          this.engineThread = null;
          this.gear = 0;
        }
  }

  public boolean getParkBrake(){//制動狀態
    return this.ParkBrake;
  }
  public void freeBrake(){//放剎車
    if (((this.gear == 1) || (this.gear == 3)) && (this.engineThread != null) && (this.engineThread.isAlive())) {
      this.engine.speed = 10;
    }
    this.ParkBrake = false;
  }

  public void brake(){//剎車
    this.engine.speed = 0;
    this.ParkBrake = true;
  }

  public int getGear(){//獲得檔位
    return this.gear;
  }
  public void engageGear(int gear){//掛擋操作
    if (this.ParkBrake)
      this.gear = gear;
  }

  public int getSpeed(){//提出汽車速度
    return this.engine.speed;
  }
  public void addSpeed(int n){//加速
    if ((this.engine.speed >= 0) && (this.engine.speed < 1200))
      this.engine.speed = (10 + n * 100);
  }

}

小編整理了一些java進階學習資料和面試題,需要資料的請加JAVA高階學習Q群:701136382 這是小編創建的java高階學習交流群,加群一起交流學習深造。群裏也有小編整理的2019年最新最全的java高階學習資料!

java實訓項目-模擬自動擋汽車