Advertisement
Doragonroudo

[EGCO213] EX.8 - SnowWhite: Listeners/Thread/Sound

Nov 21st, 2018
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.10 KB | None | 0 0
  1. // NEW VERSION
  2.  
  3. package ex8;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.util.Random;
  8. import javax.swing.*;
  9.  
  10. public class SnowwhiteFrame extends JFrame {
  11.  
  12.     // components
  13.     private JPanel contentpane;
  14.     private JLabel drawpane;
  15.     private JComboBox combo;
  16.     private JToggleButton[] tb;
  17.     private JButton moveButton, stopButton;
  18.     private JTextField scoreText;
  19.     private JLabel snowwhiteLabel, basketLabel;
  20.     private ButtonGroup bgroup;
  21.     private MySoundEffect hitSound, themeSound;
  22.  
  23.     // working variables - adjust the values as you want
  24.     private int frameWidth = 1000, frameHeight = 650;
  25.     private int snowwhiteWidth = 180, snowwhiteHeight = 300;
  26.     private int snowwhiteCurX = 700, snowwhiteCurY = 250;
  27.     private int basketWidth = 100, basketHeight = 100;
  28.     private int basketCurX = 0, basketCurY = 0;
  29.     private int snowwhiteSpeed = 1000, basketSpeed = 1000;
  30.     private boolean snowwhiteLeft = true, snowwhiteMove = true, basketMove = false;
  31.     private int score;
  32.    
  33.     private int posX, posY;
  34.  
  35.     public static void main(String[] args) {
  36.         new SnowwhiteFrame();
  37.     }
  38.  
  39.     //////////////////////////////////////////////////////////////////////////
  40.     public SnowwhiteFrame() {
  41.         setTitle("Snow White");
  42.         setBounds(50, 50, frameWidth, frameHeight);
  43.         setResizable(false);
  44.         setVisible(true);
  45.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  46.  
  47.         // (1) Total score when closing frame : add WindowListener (anonymous class) to frame
  48.         addWindowListener(new WindowAdapter() {
  49.             public void windowClosing(WindowEvent e) {
  50.                 QuickDialog.show("Final score = " + score);
  51.             }
  52.         });
  53.         contentpane = (JPanel) getContentPane();
  54.         contentpane.setLayout(new BorderLayout());
  55.         AddComponents();
  56.         setSnowwhiteThread();
  57.     }
  58.  
  59.     //////////////////////////////////////////////////////////////////////////
  60.     public void AddComponents() {
  61.         MyImageIcon backgroundImg = new MyImageIcon("background.jpg").resize(frameWidth, frameHeight);
  62.         MyImageIcon snowwhiteImg = new MyImageIcon("snowwhite.png").resize(snowwhiteWidth, snowwhiteHeight);
  63.         MyImageIcon basketImg = new MyImageIcon("apple.png").resize(basketWidth, basketHeight);
  64.  
  65.         drawpane = new JLabel();
  66.         drawpane.setIcon(backgroundImg);
  67.         drawpane.setLayout(null);
  68.  
  69.         snowwhiteLabel = new JLabel(snowwhiteImg);
  70.         snowwhiteLabel.setBounds(snowwhiteCurX, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  71.         drawpane.add(snowwhiteLabel);
  72.  
  73.         basketLabel = new JLabel(basketImg);
  74.         basketLabel.setBounds(basketCurX, basketCurY, basketWidth, basketHeight);
  75.         drawpane.add(basketLabel);
  76.  
  77.         hitSound = new MySoundEffect("blip.wav");
  78.         themeSound = new MySoundEffect("theme.wav");
  79.         themeSound.playLoop();
  80.  
  81.         // (2) Snowwhite's speed : add ItemListener (anonymouse class) to combo
  82.         ItemListener itemListener = new ItemListener() {
  83.             public void itemStateChanged(ItemEvent itemEvent) {
  84.                 int state = itemEvent.getStateChange();
  85.                 if (state == ItemEvent.SELECTED) {
  86.                     if (itemEvent.getItem() == "fast") {
  87.                         System.out.println("select fast");
  88.                         snowwhiteSpeed = 100;
  89.                     } else if (itemEvent.getItem() == "medium") {
  90.                         System.out.println("select medium");
  91.                         snowwhiteSpeed = 500;
  92.                     } else if (itemEvent.getItem() == "slow") {
  93.                         System.out.println("select slow");
  94.                         snowwhiteSpeed = 1000;
  95.                     }
  96.  
  97.                 }
  98.             }
  99.         };
  100.  
  101.         String[] speed = {"fast", "medium", "slow"};
  102.         combo = new JComboBox(speed);
  103.         combo.setSelectedIndex(1);
  104.  
  105.         combo.addItemListener(itemListener);
  106.  
  107.         // (3) Snowwhite's direction : add ItemListener (anonymouse class) to tb[i]
  108.         ItemListener dirListener = new ItemListener() {
  109.             public void itemStateChanged(ItemEvent itemEvent) {
  110.                 int state = itemEvent.getStateChange();
  111.                 if (state == ItemEvent.SELECTED) {
  112.                     if (tb[0].isSelected()) {
  113.                         System.out.println("left");
  114.                         snowwhiteLeft = true;
  115.                     } else if (tb[1].isSelected()) {
  116.                         System.out.println("right");
  117.                         snowwhiteLeft = false;
  118.                     }
  119.                 }
  120.             }
  121.         };
  122.  
  123.         tb = new JToggleButton[2];
  124.         bgroup = new ButtonGroup();
  125.         tb[0] = new JRadioButton("Left");
  126.         tb[0].setName("Left");
  127.         tb[1] = new JRadioButton("Right");
  128.         tb[1].setName("Right");
  129.         tb[0].setSelected(true);
  130.  
  131.         for (int i = 0; i < 2; i++) {
  132.             bgroup.add(tb[i]);
  133.             tb[i].addItemListener(dirListener);
  134.         }
  135.  
  136.         // (4) Basket moves : add ActionListener (anonymous class) to moveButton
  137.         moveButton = new JButton("Move");
  138.         moveButton.addActionListener(new ActionListener() {
  139.             public void actionPerformed(ActionEvent e) {
  140.                 System.out.println("basket move");
  141.  
  142.                 if (!basketMove) {
  143.                     setBasketThread();
  144.                 }
  145.                 basketMove = true;
  146.             }
  147.         });
  148.  
  149.         // (5) Basket stops : add ActionListener (anonymous class) to stopButton
  150.         stopButton = new JButton("Stop");
  151.         stopButton.addActionListener(new ActionListener() {
  152.             public void actionPerformed(ActionEvent e) {
  153.                 System.out.println("basket stop");
  154.                 basketMove = false;
  155.             }
  156.         });
  157.  
  158.         scoreText = new JTextField("0", 5);
  159.         scoreText.setEditable(false);
  160.  
  161.         JPanel control = new JPanel();
  162.         control.setBounds(0, 0, 1000, 50);
  163.         control.add(new JLabel("Snow White Control : "));
  164.         control.add(combo);
  165.         control.add(tb[0]);
  166.         control.add(tb[1]);
  167.         control.add(new JLabel("                 "));
  168.         control.add(new JLabel("Basket Control : "));
  169.         control.add(moveButton);
  170.         control.add(stopButton);
  171.         control.add(new JLabel("                 "));
  172.         control.add(new JLabel("Score : "));
  173.         control.add(scoreText);
  174.         contentpane.add(control, BorderLayout.NORTH);
  175.         contentpane.add(drawpane, BorderLayout.CENTER);
  176.         validate();
  177.     }
  178.  
  179.     //////////////////////////////////////////////////////////////////////////
  180.     public void setSnowwhiteThread() {
  181.         Thread snowwhiteThread = new Thread() {
  182.             public void run() {
  183.                 while (snowwhiteMove) {
  184.                     // (6) Update Snowwhite's location
  185.                     // System.out.println("snow moving");
  186.                     // check bound
  187.                     if (snowwhiteCurX + snowwhiteWidth > frameWidth) {
  188.                         snowwhiteLabel.setBounds(snowwhiteCurX = 0, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  189.                     } else if (snowwhiteCurX < 0) {
  190.                         snowwhiteLabel.setBounds(snowwhiteCurX = frameWidth - snowwhiteWidth, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  191.                     }
  192.  
  193.                     // check left right
  194.                     if (snowwhiteLeft) {
  195.                         snowwhiteLabel.setBounds(snowwhiteCurX -= 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  196.                     } else {
  197.                         snowwhiteLabel.setBounds(snowwhiteCurX += 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  198.                     }
  199.  
  200.                     repaint();
  201.                     collision();      // checked by either Snowwhite or basket
  202.  
  203.                     try {
  204.                         Thread.sleep(snowwhiteSpeed);
  205.                     } catch (InterruptedException e) {
  206.                         e.printStackTrace();
  207.                     }
  208.  
  209.                 } // end while
  210.             } // end run
  211.         }; // end thread creation
  212.         snowwhiteThread.start();
  213.     }
  214.  
  215.     //////////////////////////////////////////////////////////////////////////
  216.     public void setBasketThread() {
  217.         Thread basketThread = new Thread() {
  218.             public void run() {
  219.                 while (basketMove) {
  220.                     // (7) Update basket's location
  221.  
  222.                     Random rand = new Random();
  223.                     int randX = rand.nextInt(frameWidth - basketWidth);
  224.                     int randY = rand.nextInt(frameHeight - basketHeight);
  225.                     basketLabel.setBounds(basketCurX = randX, basketCurY = randY, basketWidth, basketHeight);
  226.  
  227.                     repaint();
  228.  
  229.                     try {
  230.                         Thread.sleep(basketSpeed);
  231.                     } catch (InterruptedException e) {
  232.                         e.printStackTrace();
  233.                     }
  234.                 } // end while
  235.             } // end run
  236.         }; // end thread creation
  237.         basketThread.start();
  238.     }
  239.  
  240.     //////////////////////////////////////////////////////////////////////////
  241.     synchronized public void collision() {
  242.         // (8) Play hit sound & update score
  243.         if (snowwhiteLabel.getBounds().intersects(basketLabel.getBounds())) {
  244.             if(basketCurX != posX && basketCurY != posY) {
  245.                 System.out.println("Snowwhite got apple");
  246.  
  247.                 score++;
  248.                 scoreText.setText(Integer.toString(score));
  249.  
  250.                 /*
  251.             Random rand = new Random();
  252.             int randX = rand.nextInt(frameWidth - basketWidth);
  253.             int randY = rand.nextInt(frameHeight - basketHeight);
  254.             basketLabel.setBounds(basketCurX = randX, basketCurY = randY, basketWidth, basketHeight);
  255.                  */
  256.                
  257.                 hitSound.playOnce();
  258.  
  259.                 posX = basketCurX;
  260.                 posY = basketCurY;
  261.             }
  262.            
  263.         }
  264.     }
  265. }
  266.  
  267. // quick dialog
  268. class QuickDialog {
  269.  
  270.     public static void show(String message) {
  271.         JOptionPane.showMessageDialog(new JFrame(), message, "Quick Dialog",
  272.                 JOptionPane.INFORMATION_MESSAGE);
  273.     }
  274. };
  275.  
  276. // Auxiliary class to resize image
  277. class MyImageIcon extends ImageIcon {
  278.  
  279.     public MyImageIcon(String fname) {
  280.         super(fname);
  281.     }
  282.  
  283.     public MyImageIcon(Image image) {
  284.         super(image);
  285.     }
  286.  
  287.     public MyImageIcon resize(int width, int height) {
  288.         Image oldimg = this.getImage();
  289.         Image newimg = oldimg.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
  290.         return new MyImageIcon(newimg);
  291.     }
  292. };
  293.  
  294. // Auxiliary class to play sound effect (support .wav or .mid file)
  295. class MySoundEffect {
  296.  
  297.     private java.applet.AudioClip audio;
  298.  
  299.     public MySoundEffect(String filename) {
  300.         try {
  301.             java.io.File file = new java.io.File(filename);
  302.             audio = java.applet.Applet.newAudioClip(file.toURL());
  303.         } catch (Exception e) {
  304.             e.printStackTrace();
  305.         }
  306.     }
  307.  
  308.     public void playOnce() {
  309.         audio.play();
  310.     }
  311.  
  312.     public void playLoop() {
  313.         audio.loop();
  314.     }
  315.  
  316.     public void stop() {
  317.         audio.stop();
  318.     }
  319. }
  320.  
  321.  
  322.  
  323. // OLDER VERSION
  324.  
  325. package ex8;
  326.  
  327. import java.awt.*;
  328. import java.awt.event.*;
  329. import java.util.Random;
  330. import javax.swing.*;
  331.  
  332. public class SnowwhiteFrame extends JFrame {
  333.  
  334.     // components
  335.     private JPanel contentpane;
  336.     private JLabel drawpane;
  337.     private JComboBox combo;
  338.     private JToggleButton[] tb;
  339.     private JButton moveButton, stopButton;
  340.     private JTextField scoreText;
  341.     private JLabel snowwhiteLabel, basketLabel;
  342.     private ButtonGroup bgroup;
  343.     private MySoundEffect hitSound, themeSound;
  344.  
  345.     // working variables - adjust the values as you want
  346.     private int frameWidth = 1000, frameHeight = 650;
  347.     private int snowwhiteWidth = 180, snowwhiteHeight = 300;
  348.     private int snowwhiteCurX = 700, snowwhiteCurY = 250;
  349.     private int basketWidth = 100, basketHeight = 100;
  350.     private int basketCurX = 0, basketCurY = 0;
  351.     private int snowwhiteSpeed = 1000, basketSpeed = 1000;
  352.     private boolean snowwhiteLeft = true, snowwhiteMove = true, basketMove = false;
  353.     private int score;
  354.  
  355.     public static void main(String[] args) {
  356.         new SnowwhiteFrame();
  357.     }
  358.  
  359.     //////////////////////////////////////////////////////////////////////////
  360.     public SnowwhiteFrame() {
  361.         setTitle("Snow White");
  362.         setBounds(50, 50, frameWidth, frameHeight);
  363.         setResizable(false);
  364.         setVisible(true);
  365.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  366.  
  367.         // (1) Total score when closing frame : add WindowListener (anonymous class) to frame
  368.         addWindowListener(new WindowAdapter() {
  369.             public void windowClosing(WindowEvent e) {
  370.                 QuickDialog.show("Final score = " + score);
  371.             }
  372.         });
  373.         contentpane = (JPanel) getContentPane();
  374.         contentpane.setLayout(new BorderLayout());
  375.         AddComponents();
  376.         setSnowwhiteThread();
  377.     }
  378.  
  379.     //////////////////////////////////////////////////////////////////////////
  380.     public void AddComponents() {
  381.         MyImageIcon backgroundImg = new MyImageIcon("background.jpg").resize(frameWidth, frameHeight);
  382.         MyImageIcon snowwhiteImg = new MyImageIcon("snowwhite.png").resize(snowwhiteWidth, snowwhiteHeight);
  383.         MyImageIcon basketImg = new MyImageIcon("apple.png").resize(basketWidth, basketHeight);
  384.  
  385.         drawpane = new JLabel();
  386.         drawpane.setIcon(backgroundImg);
  387.         drawpane.setLayout(null);
  388.  
  389.         snowwhiteLabel = new JLabel(snowwhiteImg);
  390.         snowwhiteLabel.setBounds(snowwhiteCurX, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  391.         drawpane.add(snowwhiteLabel);
  392.  
  393.         basketLabel = new JLabel(basketImg);
  394.         basketLabel.setBounds(basketCurX, basketCurY, basketWidth, basketHeight);
  395.         drawpane.add(basketLabel);
  396.  
  397.         hitSound = new MySoundEffect("blip.wav");
  398.         themeSound = new MySoundEffect("theme.wav");
  399.         themeSound.playLoop();
  400.  
  401.         // (2) Snowwhite's speed : add ItemListener (anonymouse class) to combo
  402.         ItemListener itemListener = new ItemListener() {
  403.             public void itemStateChanged(ItemEvent itemEvent) {
  404.                 int state = itemEvent.getStateChange();
  405.                 if (state == ItemEvent.SELECTED) {
  406.                     if (itemEvent.getItem() == "fast") {
  407.                         System.out.println("select fast");
  408.                         snowwhiteSpeed = 300;
  409.                     } else if (itemEvent.getItem() == "medium") {
  410.                         System.out.println("select medium");
  411.                         snowwhiteSpeed = 1000;
  412.                     } else if (itemEvent.getItem() == "slow") {
  413.                         System.out.println("select slow");
  414.                         snowwhiteSpeed = 2000;
  415.                     }
  416.  
  417.                 }
  418.             }
  419.         };
  420.  
  421.         String[] speed = {"fast", "medium", "slow"};
  422.         combo = new JComboBox(speed);
  423.         combo.setSelectedIndex(1);
  424.  
  425.         combo.addItemListener(itemListener);
  426.  
  427.         // (3) Snowwhite's direction : add ItemListener (anonymouse class) to tb[i]
  428.         ItemListener dirListener = new ItemListener() {
  429.             public void itemStateChanged(ItemEvent itemEvent) {
  430.                 int state = itemEvent.getStateChange();
  431.                 if (state == ItemEvent.SELECTED) {
  432.                     if (tb[0].isSelected()) {
  433.                         System.out.println("left");
  434.                         snowwhiteLeft = true;
  435.                     } else if (tb[1].isSelected()) {
  436.                         System.out.println("right");
  437.                         snowwhiteLeft = false;
  438.                     }
  439.                 }
  440.             }
  441.         };
  442.  
  443.         tb = new JToggleButton[2];
  444.         bgroup = new ButtonGroup();
  445.         tb[0] = new JRadioButton("Left");
  446.         tb[0].setName("Left");
  447.         tb[1] = new JRadioButton("Right");
  448.         tb[1].setName("Right");
  449.         tb[0].setSelected(true);
  450.  
  451.         for (int i = 0; i < 2; i++) {
  452.             bgroup.add(tb[i]);
  453.             tb[i].addItemListener(dirListener);
  454.         }
  455.  
  456.         // (4) Basket moves : add ActionListener (anonymous class) to moveButton
  457.         moveButton = new JButton("Move");
  458.         moveButton.addActionListener(new ActionListener() {
  459.             public void actionPerformed(ActionEvent e) {
  460.                 System.out.println("basket move");
  461.                
  462.                 if (!basketMove) {
  463.                     setBasketThread();
  464.                 }
  465.                 else {
  466.                     Random rand = new Random();
  467.                     int randX = rand.nextInt(frameWidth - basketWidth);
  468.                     // int randY = rand.nextInt(frameHeight / 3);
  469.                     basketLabel.setBounds(basketCurX = randX, basketCurY = 0, basketWidth, basketHeight);
  470.                 }
  471.  
  472.                 basketMove = true;
  473.             }
  474.         });
  475.  
  476.         // (5) Basket stops : add ActionListener (anonymous class) to stopButton
  477.         stopButton = new JButton("Stop");
  478.         stopButton.addActionListener(new ActionListener() {
  479.             public void actionPerformed(ActionEvent e) {
  480.                 System.out.println("basket stop");
  481.                 basketMove = false;
  482.             }
  483.         });
  484.  
  485.         scoreText = new JTextField("0", 5);
  486.         scoreText.setEditable(false);
  487.  
  488.         JPanel control = new JPanel();
  489.         control.setBounds(0, 0, 1000, 50);
  490.         control.add(new JLabel("Snow White Control : "));
  491.         control.add(combo);
  492.         control.add(tb[0]);
  493.         control.add(tb[1]);
  494.         control.add(new JLabel("                 "));
  495.         control.add(new JLabel("Basket Control : "));
  496.         control.add(moveButton);
  497.         control.add(stopButton);
  498.         control.add(new JLabel("                 "));
  499.         control.add(new JLabel("Score : "));
  500.         control.add(scoreText);
  501.         contentpane.add(control, BorderLayout.NORTH);
  502.         contentpane.add(drawpane, BorderLayout.CENTER);
  503.         validate();
  504.     }
  505.  
  506.     //////////////////////////////////////////////////////////////////////////
  507.     public void setSnowwhiteThread() {
  508.         Thread snowwhiteThread = new Thread() {
  509.             public void run() {
  510.                 while (snowwhiteMove) {
  511.                     // (6) Update Snowwhite's location
  512.                     // System.out.println("snow moving");
  513.                     // check bound
  514.                     if (snowwhiteCurX + snowwhiteWidth > frameWidth) {
  515.                         snowwhiteLabel.setBounds(snowwhiteCurX = 0, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  516.                     } else if (snowwhiteCurX < 0) {
  517.                         snowwhiteLabel.setBounds(snowwhiteCurX = frameWidth - snowwhiteWidth, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  518.                     }
  519.  
  520.                     // check left right
  521.                     if (snowwhiteLeft) {
  522.                         snowwhiteLabel.setBounds(snowwhiteCurX -= 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  523.                     } else {
  524.                         snowwhiteLabel.setBounds(snowwhiteCurX += 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
  525.                     }
  526.  
  527.                     repaint();
  528.                     collision();      // checked by either Snowwhite or basket
  529.  
  530.                     try {
  531.                         Thread.sleep(snowwhiteSpeed);
  532.                     } catch (InterruptedException e) {
  533.                         e.printStackTrace();
  534.                     }
  535.  
  536.                 } // end while
  537.             } // end run
  538.         }; // end thread creation
  539.         snowwhiteThread.start();
  540.     }
  541.  
  542.     //////////////////////////////////////////////////////////////////////////
  543.     public void setBasketThread() {
  544.         Thread basketThread = new Thread() {
  545.             public void run() {
  546.                 while (basketMove) {
  547.                     // (7) Update basket's location
  548.                     // check bound
  549.                     if (basketCurY + basketHeight > frameHeight) {
  550.                         Random rand = new Random();
  551.                         int randX = rand.nextInt(frameWidth - basketWidth);
  552.                         // int randY = rand.nextInt(frameHeight / 3);
  553.                         basketLabel.setBounds(basketCurX = randX, basketCurY = 0, basketWidth, basketHeight);
  554.                     }
  555.                    
  556.                     System.out.println("basket moving");
  557.                     basketLabel.setBounds(basketCurX, basketCurY += 20, basketWidth, basketHeight);
  558.                    
  559.                    
  560.                     repaint();
  561.  
  562.                     try {
  563.                         Thread.sleep(basketSpeed);
  564.                     } catch (InterruptedException e) {
  565.                         e.printStackTrace();
  566.                     }
  567.                 } // end while
  568.             } // end run
  569.         }; // end thread creation
  570.         basketThread.start();
  571.     }
  572.  
  573.     //////////////////////////////////////////////////////////////////////////
  574.     synchronized public void collision() {
  575.         // (8) Play hit sound & update score
  576.         if (snowwhiteLabel.getBounds().intersects(basketLabel.getBounds())) {
  577.             System.out.println("Snowwhite got apple");
  578.  
  579.             score++;
  580.             scoreText.setText(Integer.toString(score));
  581.  
  582.             Random rand = new Random();
  583.             int randX = rand.nextInt(frameWidth - basketWidth);
  584.             // int randY = rand.nextInt(frameHeight / 3);
  585.             basketLabel.setBounds(basketCurX = randX, basketCurY = 0, basketWidth, basketHeight);
  586.            
  587.             hitSound.playOnce();
  588.         }
  589.     }
  590. }
  591.  
  592. // quick dialog
  593. class QuickDialog {
  594.  
  595.     public static void show(String message) {
  596.         JOptionPane.showMessageDialog(new JFrame(), message, "Quick Dialog",
  597.                 JOptionPane.INFORMATION_MESSAGE);
  598.     }
  599. };
  600.  
  601. // Auxiliary class to resize image
  602. class MyImageIcon extends ImageIcon {
  603.  
  604.     public MyImageIcon(String fname) {
  605.         super(fname);
  606.     }
  607.  
  608.     public MyImageIcon(Image image) {
  609.         super(image);
  610.     }
  611.  
  612.     public MyImageIcon resize(int width, int height) {
  613.         Image oldimg = this.getImage();
  614.         Image newimg = oldimg.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
  615.         return new MyImageIcon(newimg);
  616.     }
  617. };
  618.  
  619. // Auxiliary class to play sound effect (support .wav or .mid file)
  620. class MySoundEffect {
  621.  
  622.     private java.applet.AudioClip audio;
  623.  
  624.     public MySoundEffect(String filename) {
  625.         try {
  626.             java.io.File file = new java.io.File(filename);
  627.             audio = java.applet.Applet.newAudioClip(file.toURL());
  628.         } catch (Exception e) {
  629.             e.printStackTrace();
  630.         }
  631.     }
  632.  
  633.     public void playOnce() {
  634.         audio.play();
  635.     }
  636.  
  637.     public void playLoop() {
  638.         audio.loop();
  639.     }
  640.  
  641.     public void stop() {
  642.         audio.stop();
  643.     }
  644. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement