virtual_pet
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| virtual_pet [2026/02/26 13:40] – frchris | virtual_pet [2026/02/26 14:00] (current) – [Activity 5] frchris | ||
|---|---|---|---|
| Line 126: | Line 126: | ||
| </ | </ | ||
| ===== Activity 4 ===== | ===== Activity 4 ===== | ||
| + | |||
| + | <file java Food.java> | ||
| + | /** | ||
| + | * Activity 4 part A | ||
| + | * @author (your name) | ||
| + | * @version (a version number or a date) | ||
| + | */ | ||
| + | public class Food | ||
| + | { | ||
| + | /* enter code for instance variables, constructor, | ||
| + | and accessor methods | ||
| + | **/ | ||
| + | } | ||
| + | </ | ||
| + | |||
| <file java Game.java> | <file java Game.java> | ||
| /** | /** | ||
| Line 145: | Line 160: | ||
| { | { | ||
| /** | /** | ||
| - | * Activity 3 | + | * Activity 3 Part B |
| * @author (your name) | * @author (your name) | ||
| * @version (a version number or a date) | * @version (a version number or a date) | ||
| Line 156: | Line 171: | ||
| } | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | <file java VirtualPetGUIRunner.java> | ||
| + | /** | ||
| + | * A GUI class to run the Virtual Pet Lab | ||
| + | * Requires javax.swing and java.awt | ||
| + | * @author Kim Hermans | ||
| + | * Image Credit: J.P. O'Hara & Jamie O'Hara | ||
| + | * @November 2024 | ||
| + | */ | ||
| + | import java.awt.BorderLayout; | ||
| + | import java.awt.Color; | ||
| + | import java.awt.Dimension; | ||
| + | import java.awt.GridBagConstraints; | ||
| + | import java.awt.GridBagLayout; | ||
| + | import java.awt.GridLayout; | ||
| + | import java.awt.event.ActionEvent; | ||
| + | import java.awt.event.ActionListener; | ||
| + | import java.io.IOException; | ||
| + | import java.util.concurrent.Executors; | ||
| + | import java.util.concurrent.ScheduledExecutorService; | ||
| + | import java.util.concurrent.TimeUnit; | ||
| + | import javax.swing.BorderFactory; | ||
| + | import javax.swing.Box; | ||
| + | import javax.swing.BoxLayout; | ||
| + | import javax.swing.ImageIcon; | ||
| + | import javax.swing.JButton; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JLabel; | ||
| + | import javax.swing.JMenu; | ||
| + | import javax.swing.JMenuBar; | ||
| + | import javax.swing.JMenuItem; | ||
| + | import javax.swing.JOptionPane; | ||
| + | import javax.swing.JPanel; | ||
| + | import javax.swing.Timer; | ||
| + | |||
| + | public class VirtualPetGUIRunner | ||
| + | { | ||
| + | // CHANGE THIS VARIABLE VALUE TO TEST AT A DIFFERENT SPEED | ||
| + | private final int INTERVAL_IN_SECONDS = 10; | ||
| + | | ||
| + | private String info; | ||
| + | private int timerSeconds, | ||
| + | private boolean isEating, isPlaying; | ||
| + | private VirtualPet pet3; | ||
| + | private ImageIcon imageHappy, imageSad, imageEat, imagePlay; | ||
| + | private JFrame frame; | ||
| + | private JPanel petPanel, menuPanel, mainPanel, statsPanel, timerPanel; | ||
| + | private JLabel petLabel, | ||
| + | private JButton foodButton, playButton; | ||
| + | private JMenu newPetMenu; | ||
| + | private JMenuItem petRunner3, petRunner4; | ||
| + | private JMenuBar menuBar; | ||
| + | | ||
| + | public VirtualPetGUIRunner(String name) | ||
| + | { | ||
| + | pet3 = new VirtualPet(name); | ||
| + | isPlaying = isEating = false; | ||
| + | | ||
| + | // Automatically calls update after INTERVAL_IN_SECONDS time | ||
| + | ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
| + | scheduler.scheduleAtFixedRate(() -> { pet3.updateStatus(); | ||
| + | | ||
| + | // Calls helper methods to initialize components of GUI | ||
| + | initPetDisplayPanel(); | ||
| + | initTimerPanel(); | ||
| + | initStatsPanel(); | ||
| + | initButtons(); | ||
| + | fillLayout(); | ||
| + | initMenuBar(); | ||
| + | | ||
| + | // Initializes the frame | ||
| + | frame = new JFrame(); | ||
| + | frame.setPreferredSize(new Dimension(350, | ||
| + | frame.setTitle(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | | ||
| + | // Adds all components to the frame and makes visible | ||
| + | frame.add(mainPanel); | ||
| + | frame.setJMenuBar(menuBar); | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | } | ||
| + | | ||
| + | // Initializes the Feed and Play buttons and the panel that contains them | ||
| + | public void initButtons() | ||
| + | { | ||
| + | GridBagConstraints c = new GridBagConstraints(); | ||
| + | | ||
| + | foodButton = new JButton(" | ||
| + | foodButton.addActionListener(new FoodButtonListener()); | ||
| + | foodButton.setPreferredSize(new Dimension(80, | ||
| + | foodButton.setMaximumSize(new Dimension(80, | ||
| + | | ||
| + | playButton = new JButton(" | ||
| + | playButton.addActionListener(new PlayButtonListener()); | ||
| + | playButton.setPreferredSize(new Dimension(80, | ||
| + | playButton.setMaximumSize(new Dimension(80, | ||
| + | | ||
| + | menuPanel = new JPanel(); | ||
| + | menuPanel.setLayout(new BoxLayout(menuPanel, | ||
| + | menuPanel.add(Box.createVerticalStrut(50)); | ||
| + | menuPanel.add(foodButton, | ||
| + | menuPanel.add(Box.createVerticalStrut(10)); | ||
| + | menuPanel.add(playButton, | ||
| + | menuPanel.add(Box.createVerticalStrut(10)); | ||
| + | } | ||
| + | | ||
| + | // Initializes the panel that contains the information text | ||
| + | public void initStatsPanel() | ||
| + | { | ||
| + | info = "" | ||
| + | statsLabel = new JLabel(info); | ||
| + | statsLabel.setBorder(BorderFactory.createEmptyBorder(0, | ||
| + | | ||
| + | messageLabel = new JLabel(" | ||
| + | messageLabel.setForeground(Color.blue); | ||
| + | messageLabel.setBorder(BorderFactory.createEmptyBorder(0, | ||
| + | | ||
| + | statsPanel = new JPanel(new GridLayout(2, | ||
| + | statsPanel.add(messageLabel); | ||
| + | statsPanel.add(statsLabel); | ||
| + | } | ||
| + | | ||
| + | // Initializes the panel that displays the VirtualPet | ||
| + | public void initPetDisplayPanel() | ||
| + | { | ||
| + | imageHappy = new ImageIcon(" | ||
| + | imageSad = new ImageIcon(" | ||
| + | imageEat = new ImageIcon(" | ||
| + | imagePlay = new ImageIcon(" | ||
| + | petLabel = new JLabel(imageHappy); | ||
| + | | ||
| + | petPanel = new JPanel(new BorderLayout()); | ||
| + | petPanel.add(petLabel); | ||
| + | } | ||
| + | | ||
| + | // Initializes the timer that is displayed | ||
| + | public void initTimerPanel() | ||
| + | { | ||
| + | timerSeconds = 0; | ||
| + | timerLabel = new JLabel(" | ||
| + | Timer timer = new Timer(1000, new ActionListener() { | ||
| + | @Override | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | timerSeconds++; | ||
| + | int minutes = timerSeconds / 60; | ||
| + | int remainingSeconds = timerSeconds % 60; | ||
| + | timerLabel.setText(String.format(" | ||
| + | info = "" | ||
| + | statsLabel.setText(info); | ||
| + | if(!isPlaying && !isEating) | ||
| + | { | ||
| + | if((ImageIcon)petLabel.getIcon() != imageSad && (pet3.getEnergyLevel() < 5 || pet3.getHappinessLevel() < 5)) | ||
| + | petLabel.setIcon(imageSad); | ||
| + | else if((ImageIcon)petLabel.getIcon() != imageHappy && (pet3.getEnergyLevel() >= 5 && pet3.getHappinessLevel() >= 5)) | ||
| + | petLabel.setIcon(imageHappy); | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | timer.start(); | ||
| + | timerPanel = new JPanel(); | ||
| + | timerPanel.add(timerLabel); | ||
| + | timerPanel.setLayout(new GridBagLayout()); | ||
| + | } | ||
| + | | ||
| + | // Initializes the Menu Bar that allows you to create a new Virtual Pet | ||
| + | public void initMenuBar() | ||
| + | { | ||
| + | petRunner3 = new JMenuItem(" | ||
| + | petRunner3.addActionListener(new NewPet3Listener()); | ||
| + | petRunner4 = new JMenuItem(" | ||
| + | petRunner4.addActionListener(new NewPet4Listener()); | ||
| + | newPetMenu = new JMenu(" | ||
| + | newPetMenu.add(petRunner3); | ||
| + | newPetMenu.add(petRunner4); | ||
| + | menuBar = new JMenuBar(); | ||
| + | menuBar.add(newPetMenu); | ||
| + | } | ||
| + | | ||
| + | // Organizes the layout for the main panel | ||
| + | public void fillLayout() | ||
| + | { | ||
| + | mainPanel = new JPanel(); | ||
| + | mainPanel.setLayout(new GridBagLayout()); | ||
| + | GridBagConstraints c = new GridBagConstraints(); | ||
| + | c.fill = GridBagConstraints.BOTH; | ||
| + | c.weightx = 60; | ||
| + | c.weighty = 70; | ||
| + | c.gridx = 0; | ||
| + | c.gridy = 0; | ||
| + | mainPanel.add(petPanel, | ||
| + | c.weightx = 40; | ||
| + | c.gridx = 1; | ||
| + | mainPanel.add(menuPanel, | ||
| + | c.weightx = 60; | ||
| + | c.weighty = 30; | ||
| + | c.gridx = 0; | ||
| + | c.gridy = 1; | ||
| + | mainPanel.add(statsPanel, | ||
| + | c.weightx = 40; | ||
| + | c.gridx = 1; | ||
| + | mainPanel.add(timerPanel, | ||
| + | } | ||
| + | |||
| + | // Defines the action when the Feed button is clicked | ||
| + | class FoodButtonListener implements ActionListener | ||
| + | { | ||
| + | public void actionPerformed(ActionEvent e) | ||
| + | { | ||
| + | pet3.feed(); | ||
| + | messageLabel.setText(" | ||
| + | info = "" | ||
| + | statsLabel.setText(info); | ||
| + | animationTimerSeconds = 0; | ||
| + | animationTimerSeconds = 0; | ||
| + | isEating = true; | ||
| + | petLabel.setIcon(imageEat); | ||
| + | // Timer to show eating image for 2 seconds | ||
| + | Timer animationTimer = new Timer(1000, new ActionListener() { | ||
| + | @Override | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | animationTimerSeconds++; | ||
| + | if(animationTimerSeconds >= 2) | ||
| + | { | ||
| + | if(pet3.getEnergyLevel() < 5 || pet3.getHappinessLevel() < 5) | ||
| + | petLabel.setIcon(imageSad); | ||
| + | else | ||
| + | petLabel.setIcon(imageHappy); | ||
| + | isEating = false; | ||
| + | ((Timer) e.getSource()).stop(); | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | animationTimer.start(); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | // Defines the action when the Play button is clicked | ||
| + | class PlayButtonListener implements ActionListener | ||
| + | { | ||
| + | public void actionPerformed(ActionEvent e) | ||
| + | { | ||
| + | pet3.play(); | ||
| + | messageLabel.setText(" | ||
| + | info = "" | ||
| + | statsLabel.setText(info); | ||
| + | animationTimerSeconds = 0; | ||
| + | isPlaying = true; | ||
| + | petLabel.setIcon(imagePlay); | ||
| + | // Timer to show playing image for 2 seconds | ||
| + | Timer animationTimer = new Timer(1000, new ActionListener() { | ||
| + | @Override | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | animationTimerSeconds++; | ||
| + | if(animationTimerSeconds >= 2) | ||
| + | { | ||
| + | if(pet3.getEnergyLevel() < 5 || pet3.getHappinessLevel() < 5) | ||
| + | petLabel.setIcon(imageSad); | ||
| + | else | ||
| + | petLabel.setIcon(imageHappy); | ||
| + | isPlaying = false; | ||
| + | ((Timer) e.getSource()).stop(); | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | animationTimer.start(); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | // Defines the action when a New Pet from Activity 3 is clicked | ||
| + | class NewPet3Listener implements ActionListener | ||
| + | { | ||
| + | public void actionPerformed(ActionEvent e) | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | String input = JOptionPane.showInputDialog(" | ||
| + | VirtualPetGUIRunner init = new VirtualPetGUIRunner(input); | ||
| + | frame.dispose(); | ||
| + | } | ||
| + | catch(Exception err) | ||
| + | { | ||
| + | System.out.println(err); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | // Defines the action when a New Pet from Activity 4 is clicked | ||
| + | class NewPet4Listener implements ActionListener | ||
| + | { | ||
| + | public void actionPerformed(ActionEvent e) | ||
| + | { | ||
| + | // " | ||
| + | // VirtualPetGUIRunner4 in your project | ||
| + | /* | ||
| + | try | ||
| + | { | ||
| + | String input = JOptionPane.showInputDialog(" | ||
| + | VirtualPetGUIRunner4 init = new VirtualPetGUIRunner4(input); | ||
| + | frame.dispose(); | ||
| + | } | ||
| + | catch(Exception err) | ||
| + | { | ||
| + | System.out.println(err); | ||
| + | } | ||
| + | */ | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static void main(String [] args) throws IOException | ||
| + | { | ||
| + | String input = JOptionPane.showInputDialog(" | ||
| + | VirtualPetGUIRunner init = new VirtualPetGUIRunner(input); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ===== Activity 5 ===== | ||
| + | |||
| + | <file java VirtualPet4Runner.java> | ||
| + | import java.util.Scanner; | ||
| + | import java.util.concurrent.Executors; | ||
| + | import java.util.concurrent.ScheduledExecutorService; | ||
| + | import java.util.concurrent.TimeUnit; | ||
| + | |||
| + | public class VirtualPetRunner4 | ||
| + | { | ||
| + | // Prints out menu and returns user choice | ||
| + | public static int getChoice(Scanner input) | ||
| + | { | ||
| + | int selection = 0; | ||
| + | while (selection < 1 || selection > 4) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | return selection; | ||
| + | } | ||
| + | | ||
| + | // Prints out food options and returns user choice | ||
| + | public static int getPantry(Scanner input) | ||
| + | { | ||
| + | int selection = 0; | ||
| + | while (selection < 1 || selection > 5) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | selection = input.nextInt(); | ||
| + | } | ||
| + | return selection; | ||
| + | |||
| + | } | ||
| + | | ||
| + | // Prints out game options and returns user choice | ||
| + | public static int getGame(Scanner input) | ||
| + | { | ||
| + | int selection = 0; | ||
| + | while (selection < 1 || selection > 4) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | selection = input.nextInt(); | ||
| + | } | ||
| + | return selection; | ||
| + | } | ||
| + | | ||
| + | // Displays a picture of the pet | ||
| + | public static void printPet(String emo) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | } | ||
| + | | ||
| + | public static void main(String[] args) | ||
| + | { | ||
| + | // CHANGE THIS VARIABLE VALUE TO TEST AT A DIFFERENT SPEED | ||
| + | final int INTERVAL_IN_SECONDS = 10; | ||
| + | | ||
| + | // Sets up Scanner for user input | ||
| + | Scanner input = new Scanner(System.in); | ||
| + | | ||
| + | VirtualPet4 myPet = new VirtualPet4(" | ||
| + | | ||
| + | // Instantiates Food objects | ||
| + | Food apple = new Food(" | ||
| + | Food cupcake = new Food(" | ||
| + | Food broccoli = new Food(" | ||
| + | Food potato = new Food(" | ||
| + | | ||
| + | // Instantiates Game objects | ||
| + | Game coinToss = new Game(" | ||
| + | Game hoopJumping = new Game(" | ||
| + | Game simonSays = new Game(" | ||
| + | | ||
| + | // Sets up a ScheduledExecutorService object that will call updateStatus | ||
| + | // every 1 minute. | ||
| + | ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
| + | scheduler.scheduleAtFixedRate(() -> { myPet.updateStatus(); | ||
| + | | ||
| + | System.out.println(myPet); | ||
| + | printPet(" | ||
| + | |||
| + | int choice = getChoice(input); | ||
| + | while (choice != 4) | ||
| + | { | ||
| + | if(choice == 1) | ||
| + | { | ||
| + | System.out.println(myPet); | ||
| + | } | ||
| + | else if (choice == 2) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | int food = getPantry(input); | ||
| + | Food f = null; | ||
| + | if (food == 1) | ||
| + | f = apple; | ||
| + | else if (food == 2) | ||
| + | f = cupcake; | ||
| + | else if (food == 3) | ||
| + | f = broccoli; | ||
| + | else if (food == 4) | ||
| + | f = potato; | ||
| + | if (f != null) | ||
| + | { | ||
| + | myPet.feed(f); | ||
| + | System.out.println(" | ||
| + | + " 1 " + f.getName()); | ||
| + | } | ||
| + | } | ||
| + | else if (choice == 3) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | int game = getGame(input); | ||
| + | Game g = null; | ||
| + | if (game == 1) | ||
| + | g = coinToss; | ||
| + | else if (game == 2) | ||
| + | g = hoopJumping; | ||
| + | else if (game == 3) | ||
| + | g = simonSays; | ||
| + | if (g != null) | ||
| + | { | ||
| + | boolean hasWon = myPet.play(g); | ||
| + | System.out.println(" | ||
| + | + " with " + myPet.getName()); | ||
| + | if (hasWon) | ||
| + | | ||
| + | else | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | if (myPet.getEnergyLevel() >= 5 && myPet.getHappinessLevel() >= 5) | ||
| + | printPet(" | ||
| + | else | ||
| + | printPet(" | ||
| + | System.out.println(myPet.getName().toUpperCase()); | ||
| + | choice = getChoice(input); | ||
| + | } | ||
| + | scheduler.shutdown(); | ||
| + | } | ||
| + | } | ||
| + | |||
| </ | </ | ||
virtual_pet.1772131217.txt.gz · Last modified: by frchris
