Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************/
- /** 20151112 Mid-term Exam **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2015/11/12 am.10:00 **/
- /***************************************/
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class Segment extends JPanel{
- int a, b, c, d, e, f, g;
- Color[] color = {Color.GRAY, Color.RED};
- private void moveSegment(int segled) {
- a = segled & 0x01;
- b = (segled >> 1) & 0x01;
- c = (segled >> 2) & 0x01;
- d = (segled >> 3) & 0x01;
- e = (segled >> 4) & 0x01;
- f = (segled >> 5) & 0x01;
- g = (segled >> 6) & 0x01;
- }
- @Override
- /* Overriding 是指「子類別」繼承父類別,但是改寫父類別既有的函式,
- 條件必須包括「引數相同」、「函式名稱相同」、「傳回值型態要相同」
- 三項。
- 不過,雖然中譯「覆寫」或是「重載」,不過實際上並非確切改寫覆類別
- 函式,故仍然可用 super 調用原父類別函式。 */
- public void paint(Graphics x) {
- /* Super 可調用原父類別函式 */
- super.paint(x);
- /* Graphics2D 類別 (Class) 以 Shape 物件進行繪圖 */
- Graphics2D g2d = (Graphics2D)x;
- g2d.setColor(color[a]); g2d.fillRect( 90, 40 , 100, 20);
- g2d.setColor(color[b]); g2d.fillRect(190, 60 , 20, 80);
- g2d.setColor(color[c]); g2d.fillRect(190, 160 , 20, 80);
- g2d.setColor(color[d]); g2d.fillRect( 90, 240 , 100, 20);
- g2d.setColor(color[e]); g2d.fillRect( 70, 160 , 20, 80);
- g2d.setColor(color[f]); g2d.fillRect( 70, 60 , 20, 80);
- g2d.setColor(color[g]); g2d.fillRect( 90, 140 , 100, 20);
- }
- /* 目前七節顯示其開關狀態旗標 */
- private static boolean FLAG = false;
- /**/
- public static boolean getFlag() { return FLAG; }
- public static void setFlag(boolean _setFlag) { FLAG = _setFlag; }
- public Segment() {
- // Add JButton
- JButton button = new JButton("Set");
- button.setSize(100, 30);
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Segment.setFlag(!getFlag());
- }
- }); add(button);
- }
- public static void main(String[] args) throws InterruptedException {
- // TODO Auto-generated method stub
- JFrame frame = new JFrame();
- Segment Seg = new Segment();
- frame.add(Seg);
- frame.setSize(300, 300);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- /* LED 0 - F 各十六進位數值 */
- int[] segled = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x27, 0x7f, 0x67,
- 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
- int SegledPoint = 0;
- while (true) {
- if (FLAG) {
- Seg.moveSegment(segled[SegledPoint++ % segled.length]);
- Seg.repaint();
- } Thread.sleep(1000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment