Holey_yan

[Java] Mid-term Exam

Nov 11th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. /***************************************/
  2. /** 20151112 Mid-term Exam            **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/11/12 am.10:00      **/
  6. /***************************************/
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import javax.swing.*;
  14.  
  15. public class Segment extends JPanel{
  16.    
  17.     int a, b, c, d, e, f, g;
  18.     Color[] color =  {Color.GRAY, Color.RED};
  19.    
  20.     private void moveSegment(int segled) {
  21.         a = segled & 0x01;
  22.         b = (segled >> 1) & 0x01;
  23.         c = (segled >> 2) & 0x01;
  24.         d = (segled >> 3) & 0x01;
  25.         e = (segled >> 4) & 0x01;
  26.         f = (segled >> 5) & 0x01;
  27.         g = (segled >> 6) & 0x01;
  28.     }
  29.    
  30.     @Override
  31.     /*  Overriding 是指「子類別」繼承父類別,但是改寫父類別既有的函式,
  32.         條件必須包括「引數相同」、「函式名稱相同」、「傳回值型態要相同」
  33.         三項。
  34.         不過,雖然中譯「覆寫」或是「重載」,不過實際上並非確切改寫覆類別
  35.         函式,故仍然可用 super 調用原父類別函式。              */
  36.     public void paint(Graphics x) {
  37.         /* Super 可調用原父類別函式 */
  38.         super.paint(x);
  39.         /* Graphics2D 類別 (Class) 以 Shape 物件進行繪圖 */
  40.         Graphics2D g2d = (Graphics2D)x;
  41.         g2d.setColor(color[a]); g2d.fillRect( 90,  40 , 100,  20);
  42.         g2d.setColor(color[b]); g2d.fillRect(190,  60 ,  20,  80);
  43.         g2d.setColor(color[c]); g2d.fillRect(190, 160 ,  20,  80);
  44.         g2d.setColor(color[d]); g2d.fillRect( 90, 240 , 100,  20);
  45.         g2d.setColor(color[e]); g2d.fillRect( 70, 160 ,  20,  80);
  46.         g2d.setColor(color[f]); g2d.fillRect( 70,  60 ,  20,  80);
  47.         g2d.setColor(color[g]); g2d.fillRect( 90, 140 , 100,  20);
  48.        
  49.     }
  50.    
  51.     /* 目前七節顯示其開關狀態旗標 */
  52.     private static boolean FLAG = false;
  53.     /**/
  54.     public static boolean getFlag() { return FLAG; }
  55.     public static void setFlag(boolean _setFlag) { FLAG = _setFlag; }
  56.     public Segment() {
  57.         // Add JButton
  58.         JButton button = new JButton("Set");
  59.             button.setSize(100, 30);
  60.             button.addActionListener(new ActionListener() {
  61.             public void actionPerformed(ActionEvent e) {
  62.                 Segment.setFlag(!getFlag());
  63.             }
  64.         }); add(button);
  65.     }
  66.    
  67.     public static void main(String[] args) throws InterruptedException {
  68.         // TODO Auto-generated method stub
  69.         JFrame frame = new JFrame();
  70.             Segment Seg = new Segment();
  71.         frame.add(Seg);
  72.        
  73.         frame.setSize(300, 300);
  74.         frame.setVisible(true);
  75.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.        
  77.         /* LED 0 - F 各十六進位數值 */
  78.         int[] segled = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x27, 0x7f, 0x67,
  79.                 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
  80.         int SegledPoint = 0;
  81.         while (true) {
  82.             if (FLAG) {
  83.                 Seg.moveSegment(segled[SegledPoint++ % segled.length]);
  84.                 Seg.repaint();
  85.             } Thread.sleep(1000);
  86.         }
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment