Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NEW VERSION
- package ex8;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- import javax.swing.*;
- public class SnowwhiteFrame extends JFrame {
- // components
- private JPanel contentpane;
- private JLabel drawpane;
- private JComboBox combo;
- private JToggleButton[] tb;
- private JButton moveButton, stopButton;
- private JTextField scoreText;
- private JLabel snowwhiteLabel, basketLabel;
- private ButtonGroup bgroup;
- private MySoundEffect hitSound, themeSound;
- // working variables - adjust the values as you want
- private int frameWidth = 1000, frameHeight = 650;
- private int snowwhiteWidth = 180, snowwhiteHeight = 300;
- private int snowwhiteCurX = 700, snowwhiteCurY = 250;
- private int basketWidth = 100, basketHeight = 100;
- private int basketCurX = 0, basketCurY = 0;
- private int snowwhiteSpeed = 1000, basketSpeed = 1000;
- private boolean snowwhiteLeft = true, snowwhiteMove = true, basketMove = false;
- private int score;
- private int posX, posY;
- public static void main(String[] args) {
- new SnowwhiteFrame();
- }
- //////////////////////////////////////////////////////////////////////////
- public SnowwhiteFrame() {
- setTitle("Snow White");
- setBounds(50, 50, frameWidth, frameHeight);
- setResizable(false);
- setVisible(true);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- // (1) Total score when closing frame : add WindowListener (anonymous class) to frame
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- QuickDialog.show("Final score = " + score);
- }
- });
- contentpane = (JPanel) getContentPane();
- contentpane.setLayout(new BorderLayout());
- AddComponents();
- setSnowwhiteThread();
- }
- //////////////////////////////////////////////////////////////////////////
- public void AddComponents() {
- MyImageIcon backgroundImg = new MyImageIcon("background.jpg").resize(frameWidth, frameHeight);
- MyImageIcon snowwhiteImg = new MyImageIcon("snowwhite.png").resize(snowwhiteWidth, snowwhiteHeight);
- MyImageIcon basketImg = new MyImageIcon("apple.png").resize(basketWidth, basketHeight);
- drawpane = new JLabel();
- drawpane.setIcon(backgroundImg);
- drawpane.setLayout(null);
- snowwhiteLabel = new JLabel(snowwhiteImg);
- snowwhiteLabel.setBounds(snowwhiteCurX, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- drawpane.add(snowwhiteLabel);
- basketLabel = new JLabel(basketImg);
- basketLabel.setBounds(basketCurX, basketCurY, basketWidth, basketHeight);
- drawpane.add(basketLabel);
- hitSound = new MySoundEffect("blip.wav");
- themeSound = new MySoundEffect("theme.wav");
- themeSound.playLoop();
- // (2) Snowwhite's speed : add ItemListener (anonymouse class) to combo
- ItemListener itemListener = new ItemListener() {
- public void itemStateChanged(ItemEvent itemEvent) {
- int state = itemEvent.getStateChange();
- if (state == ItemEvent.SELECTED) {
- if (itemEvent.getItem() == "fast") {
- System.out.println("select fast");
- snowwhiteSpeed = 100;
- } else if (itemEvent.getItem() == "medium") {
- System.out.println("select medium");
- snowwhiteSpeed = 500;
- } else if (itemEvent.getItem() == "slow") {
- System.out.println("select slow");
- snowwhiteSpeed = 1000;
- }
- }
- }
- };
- String[] speed = {"fast", "medium", "slow"};
- combo = new JComboBox(speed);
- combo.setSelectedIndex(1);
- combo.addItemListener(itemListener);
- // (3) Snowwhite's direction : add ItemListener (anonymouse class) to tb[i]
- ItemListener dirListener = new ItemListener() {
- public void itemStateChanged(ItemEvent itemEvent) {
- int state = itemEvent.getStateChange();
- if (state == ItemEvent.SELECTED) {
- if (tb[0].isSelected()) {
- System.out.println("left");
- snowwhiteLeft = true;
- } else if (tb[1].isSelected()) {
- System.out.println("right");
- snowwhiteLeft = false;
- }
- }
- }
- };
- tb = new JToggleButton[2];
- bgroup = new ButtonGroup();
- tb[0] = new JRadioButton("Left");
- tb[0].setName("Left");
- tb[1] = new JRadioButton("Right");
- tb[1].setName("Right");
- tb[0].setSelected(true);
- for (int i = 0; i < 2; i++) {
- bgroup.add(tb[i]);
- tb[i].addItemListener(dirListener);
- }
- // (4) Basket moves : add ActionListener (anonymous class) to moveButton
- moveButton = new JButton("Move");
- moveButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("basket move");
- if (!basketMove) {
- setBasketThread();
- }
- basketMove = true;
- }
- });
- // (5) Basket stops : add ActionListener (anonymous class) to stopButton
- stopButton = new JButton("Stop");
- stopButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("basket stop");
- basketMove = false;
- }
- });
- scoreText = new JTextField("0", 5);
- scoreText.setEditable(false);
- JPanel control = new JPanel();
- control.setBounds(0, 0, 1000, 50);
- control.add(new JLabel("Snow White Control : "));
- control.add(combo);
- control.add(tb[0]);
- control.add(tb[1]);
- control.add(new JLabel(" "));
- control.add(new JLabel("Basket Control : "));
- control.add(moveButton);
- control.add(stopButton);
- control.add(new JLabel(" "));
- control.add(new JLabel("Score : "));
- control.add(scoreText);
- contentpane.add(control, BorderLayout.NORTH);
- contentpane.add(drawpane, BorderLayout.CENTER);
- validate();
- }
- //////////////////////////////////////////////////////////////////////////
- public void setSnowwhiteThread() {
- Thread snowwhiteThread = new Thread() {
- public void run() {
- while (snowwhiteMove) {
- // (6) Update Snowwhite's location
- // System.out.println("snow moving");
- // check bound
- if (snowwhiteCurX + snowwhiteWidth > frameWidth) {
- snowwhiteLabel.setBounds(snowwhiteCurX = 0, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- } else if (snowwhiteCurX < 0) {
- snowwhiteLabel.setBounds(snowwhiteCurX = frameWidth - snowwhiteWidth, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- }
- // check left right
- if (snowwhiteLeft) {
- snowwhiteLabel.setBounds(snowwhiteCurX -= 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- } else {
- snowwhiteLabel.setBounds(snowwhiteCurX += 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- }
- repaint();
- collision(); // checked by either Snowwhite or basket
- try {
- Thread.sleep(snowwhiteSpeed);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } // end while
- } // end run
- }; // end thread creation
- snowwhiteThread.start();
- }
- //////////////////////////////////////////////////////////////////////////
- public void setBasketThread() {
- Thread basketThread = new Thread() {
- public void run() {
- while (basketMove) {
- // (7) Update basket's location
- Random rand = new Random();
- int randX = rand.nextInt(frameWidth - basketWidth);
- int randY = rand.nextInt(frameHeight - basketHeight);
- basketLabel.setBounds(basketCurX = randX, basketCurY = randY, basketWidth, basketHeight);
- repaint();
- try {
- Thread.sleep(basketSpeed);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } // end while
- } // end run
- }; // end thread creation
- basketThread.start();
- }
- //////////////////////////////////////////////////////////////////////////
- synchronized public void collision() {
- // (8) Play hit sound & update score
- if (snowwhiteLabel.getBounds().intersects(basketLabel.getBounds())) {
- if(basketCurX != posX && basketCurY != posY) {
- System.out.println("Snowwhite got apple");
- score++;
- scoreText.setText(Integer.toString(score));
- /*
- Random rand = new Random();
- int randX = rand.nextInt(frameWidth - basketWidth);
- int randY = rand.nextInt(frameHeight - basketHeight);
- basketLabel.setBounds(basketCurX = randX, basketCurY = randY, basketWidth, basketHeight);
- */
- hitSound.playOnce();
- posX = basketCurX;
- posY = basketCurY;
- }
- }
- }
- }
- // quick dialog
- class QuickDialog {
- public static void show(String message) {
- JOptionPane.showMessageDialog(new JFrame(), message, "Quick Dialog",
- JOptionPane.INFORMATION_MESSAGE);
- }
- };
- // Auxiliary class to resize image
- class MyImageIcon extends ImageIcon {
- public MyImageIcon(String fname) {
- super(fname);
- }
- public MyImageIcon(Image image) {
- super(image);
- }
- public MyImageIcon resize(int width, int height) {
- Image oldimg = this.getImage();
- Image newimg = oldimg.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
- return new MyImageIcon(newimg);
- }
- };
- // Auxiliary class to play sound effect (support .wav or .mid file)
- class MySoundEffect {
- private java.applet.AudioClip audio;
- public MySoundEffect(String filename) {
- try {
- java.io.File file = new java.io.File(filename);
- audio = java.applet.Applet.newAudioClip(file.toURL());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void playOnce() {
- audio.play();
- }
- public void playLoop() {
- audio.loop();
- }
- public void stop() {
- audio.stop();
- }
- }
- // OLDER VERSION
- package ex8;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- import javax.swing.*;
- public class SnowwhiteFrame extends JFrame {
- // components
- private JPanel contentpane;
- private JLabel drawpane;
- private JComboBox combo;
- private JToggleButton[] tb;
- private JButton moveButton, stopButton;
- private JTextField scoreText;
- private JLabel snowwhiteLabel, basketLabel;
- private ButtonGroup bgroup;
- private MySoundEffect hitSound, themeSound;
- // working variables - adjust the values as you want
- private int frameWidth = 1000, frameHeight = 650;
- private int snowwhiteWidth = 180, snowwhiteHeight = 300;
- private int snowwhiteCurX = 700, snowwhiteCurY = 250;
- private int basketWidth = 100, basketHeight = 100;
- private int basketCurX = 0, basketCurY = 0;
- private int snowwhiteSpeed = 1000, basketSpeed = 1000;
- private boolean snowwhiteLeft = true, snowwhiteMove = true, basketMove = false;
- private int score;
- public static void main(String[] args) {
- new SnowwhiteFrame();
- }
- //////////////////////////////////////////////////////////////////////////
- public SnowwhiteFrame() {
- setTitle("Snow White");
- setBounds(50, 50, frameWidth, frameHeight);
- setResizable(false);
- setVisible(true);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- // (1) Total score when closing frame : add WindowListener (anonymous class) to frame
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- QuickDialog.show("Final score = " + score);
- }
- });
- contentpane = (JPanel) getContentPane();
- contentpane.setLayout(new BorderLayout());
- AddComponents();
- setSnowwhiteThread();
- }
- //////////////////////////////////////////////////////////////////////////
- public void AddComponents() {
- MyImageIcon backgroundImg = new MyImageIcon("background.jpg").resize(frameWidth, frameHeight);
- MyImageIcon snowwhiteImg = new MyImageIcon("snowwhite.png").resize(snowwhiteWidth, snowwhiteHeight);
- MyImageIcon basketImg = new MyImageIcon("apple.png").resize(basketWidth, basketHeight);
- drawpane = new JLabel();
- drawpane.setIcon(backgroundImg);
- drawpane.setLayout(null);
- snowwhiteLabel = new JLabel(snowwhiteImg);
- snowwhiteLabel.setBounds(snowwhiteCurX, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- drawpane.add(snowwhiteLabel);
- basketLabel = new JLabel(basketImg);
- basketLabel.setBounds(basketCurX, basketCurY, basketWidth, basketHeight);
- drawpane.add(basketLabel);
- hitSound = new MySoundEffect("blip.wav");
- themeSound = new MySoundEffect("theme.wav");
- themeSound.playLoop();
- // (2) Snowwhite's speed : add ItemListener (anonymouse class) to combo
- ItemListener itemListener = new ItemListener() {
- public void itemStateChanged(ItemEvent itemEvent) {
- int state = itemEvent.getStateChange();
- if (state == ItemEvent.SELECTED) {
- if (itemEvent.getItem() == "fast") {
- System.out.println("select fast");
- snowwhiteSpeed = 300;
- } else if (itemEvent.getItem() == "medium") {
- System.out.println("select medium");
- snowwhiteSpeed = 1000;
- } else if (itemEvent.getItem() == "slow") {
- System.out.println("select slow");
- snowwhiteSpeed = 2000;
- }
- }
- }
- };
- String[] speed = {"fast", "medium", "slow"};
- combo = new JComboBox(speed);
- combo.setSelectedIndex(1);
- combo.addItemListener(itemListener);
- // (3) Snowwhite's direction : add ItemListener (anonymouse class) to tb[i]
- ItemListener dirListener = new ItemListener() {
- public void itemStateChanged(ItemEvent itemEvent) {
- int state = itemEvent.getStateChange();
- if (state == ItemEvent.SELECTED) {
- if (tb[0].isSelected()) {
- System.out.println("left");
- snowwhiteLeft = true;
- } else if (tb[1].isSelected()) {
- System.out.println("right");
- snowwhiteLeft = false;
- }
- }
- }
- };
- tb = new JToggleButton[2];
- bgroup = new ButtonGroup();
- tb[0] = new JRadioButton("Left");
- tb[0].setName("Left");
- tb[1] = new JRadioButton("Right");
- tb[1].setName("Right");
- tb[0].setSelected(true);
- for (int i = 0; i < 2; i++) {
- bgroup.add(tb[i]);
- tb[i].addItemListener(dirListener);
- }
- // (4) Basket moves : add ActionListener (anonymous class) to moveButton
- moveButton = new JButton("Move");
- moveButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("basket move");
- if (!basketMove) {
- setBasketThread();
- }
- else {
- Random rand = new Random();
- int randX = rand.nextInt(frameWidth - basketWidth);
- // int randY = rand.nextInt(frameHeight / 3);
- basketLabel.setBounds(basketCurX = randX, basketCurY = 0, basketWidth, basketHeight);
- }
- basketMove = true;
- }
- });
- // (5) Basket stops : add ActionListener (anonymous class) to stopButton
- stopButton = new JButton("Stop");
- stopButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("basket stop");
- basketMove = false;
- }
- });
- scoreText = new JTextField("0", 5);
- scoreText.setEditable(false);
- JPanel control = new JPanel();
- control.setBounds(0, 0, 1000, 50);
- control.add(new JLabel("Snow White Control : "));
- control.add(combo);
- control.add(tb[0]);
- control.add(tb[1]);
- control.add(new JLabel(" "));
- control.add(new JLabel("Basket Control : "));
- control.add(moveButton);
- control.add(stopButton);
- control.add(new JLabel(" "));
- control.add(new JLabel("Score : "));
- control.add(scoreText);
- contentpane.add(control, BorderLayout.NORTH);
- contentpane.add(drawpane, BorderLayout.CENTER);
- validate();
- }
- //////////////////////////////////////////////////////////////////////////
- public void setSnowwhiteThread() {
- Thread snowwhiteThread = new Thread() {
- public void run() {
- while (snowwhiteMove) {
- // (6) Update Snowwhite's location
- // System.out.println("snow moving");
- // check bound
- if (snowwhiteCurX + snowwhiteWidth > frameWidth) {
- snowwhiteLabel.setBounds(snowwhiteCurX = 0, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- } else if (snowwhiteCurX < 0) {
- snowwhiteLabel.setBounds(snowwhiteCurX = frameWidth - snowwhiteWidth, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- }
- // check left right
- if (snowwhiteLeft) {
- snowwhiteLabel.setBounds(snowwhiteCurX -= 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- } else {
- snowwhiteLabel.setBounds(snowwhiteCurX += 20, snowwhiteCurY, snowwhiteWidth, snowwhiteHeight);
- }
- repaint();
- collision(); // checked by either Snowwhite or basket
- try {
- Thread.sleep(snowwhiteSpeed);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } // end while
- } // end run
- }; // end thread creation
- snowwhiteThread.start();
- }
- //////////////////////////////////////////////////////////////////////////
- public void setBasketThread() {
- Thread basketThread = new Thread() {
- public void run() {
- while (basketMove) {
- // (7) Update basket's location
- // check bound
- if (basketCurY + basketHeight > frameHeight) {
- Random rand = new Random();
- int randX = rand.nextInt(frameWidth - basketWidth);
- // int randY = rand.nextInt(frameHeight / 3);
- basketLabel.setBounds(basketCurX = randX, basketCurY = 0, basketWidth, basketHeight);
- }
- System.out.println("basket moving");
- basketLabel.setBounds(basketCurX, basketCurY += 20, basketWidth, basketHeight);
- repaint();
- try {
- Thread.sleep(basketSpeed);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } // end while
- } // end run
- }; // end thread creation
- basketThread.start();
- }
- //////////////////////////////////////////////////////////////////////////
- synchronized public void collision() {
- // (8) Play hit sound & update score
- if (snowwhiteLabel.getBounds().intersects(basketLabel.getBounds())) {
- System.out.println("Snowwhite got apple");
- score++;
- scoreText.setText(Integer.toString(score));
- Random rand = new Random();
- int randX = rand.nextInt(frameWidth - basketWidth);
- // int randY = rand.nextInt(frameHeight / 3);
- basketLabel.setBounds(basketCurX = randX, basketCurY = 0, basketWidth, basketHeight);
- hitSound.playOnce();
- }
- }
- }
- // quick dialog
- class QuickDialog {
- public static void show(String message) {
- JOptionPane.showMessageDialog(new JFrame(), message, "Quick Dialog",
- JOptionPane.INFORMATION_MESSAGE);
- }
- };
- // Auxiliary class to resize image
- class MyImageIcon extends ImageIcon {
- public MyImageIcon(String fname) {
- super(fname);
- }
- public MyImageIcon(Image image) {
- super(image);
- }
- public MyImageIcon resize(int width, int height) {
- Image oldimg = this.getImage();
- Image newimg = oldimg.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
- return new MyImageIcon(newimg);
- }
- };
- // Auxiliary class to play sound effect (support .wav or .mid file)
- class MySoundEffect {
- private java.applet.AudioClip audio;
- public MySoundEffect(String filename) {
- try {
- java.io.File file = new java.io.File(filename);
- audio = java.applet.Applet.newAudioClip(file.toURL());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void playOnce() {
- audio.play();
- }
- public void playLoop() {
- audio.loop();
- }
- public void stop() {
- audio.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement