Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a GUI application Implement event handlers Display images in GUI Practice

ID: 3559849 • Letter: C

Question

Create a GUI application Implement event handlers Display images in GUI Practice GridLayout Create a file called MemoryApp.java It includes the main method of a gui application that simulates the game Memory. Requirements: The GUI includes a title: "Memory" It includes 12 square tiles that are arranged in 3 rows and 4 colums The tiles don't touch each other nor do they touch the edges of the window At the beginning all the tiles have the exact same look (all the images are turned over) At the beginning the images are randomly distributed When you click on a tile it 'flips over' (shows / hides the image) Check out Memory.mp4. It shows one possible solution. Create a runnable jar that includes the java files. Submit it via Canvas

Explanation / Answer

/** * @(#)MultiDimArray.java * * MultiDimArray Applet application 2011/2/11 * * Read in 9 images and store in a 3x3 array. * Use a nested for loop to display them. * * Add a button to mix it up * Add a button to put it back in order * Add a button to swap two random images */ import java.awt.*; import java.applet.*; import java.awt.event.*; //for ActionListener button import java.util.Random; public class testing extends Applet implements ActionListener { Image[][] pic; //picture images Image[][] rightPic; //picture images in the correct order Rectangle[][] tile; Image previous[][]; int tileWidth; int tileHeight; TextArea test; Button shuffleButton; Button resetButton; Button swapButton; String buttonPressed; public void init() { //---------------------------Set up the shuffle button---------------------------// shuffleButton=new Button("Shuffle Images"); setLayout(null); shuffleButton.setBounds(350,10,120,50); shuffleButton.addActionListener(this); add(shuffleButton); //---------------------------Set up the reset button---------------------------// resetButton=new Button("Reset Images"); setLayout(null); resetButton.setBounds(350,60,120,50); resetButton.addActionListener(this); add(resetButton); //---------------------------Set up the swap button---------------------------// swapButton=new Button("Swap Button"); setLayout(null); swapButton.setBounds(350,110,120,50); swapButton.addActionListener(this); add(swapButton); //---------------------------Load the pictures in correct order---------------------------// pic=new Image[3][3]; pic[0][0]=getImage(getCodeBase(),"A1.jpg"); pic[0][1]=getImage(getCodeBase(),"A2.jpg"); pic[0][2]=getImage(getCodeBase(),"A3.jpg"); pic[1][0]=getImage(getCodeBase(),"A4.jpg"); pic[1][1]=getImage(getCodeBase(),"A5.jpg"); pic[1][2]=getImage(getCodeBase(),"A6.jpg"); pic[2][0]=getImage(getCodeBase(),"A7.jpg"); pic[2][1]=getImage(getCodeBase(),"A8.jpg"); pic[2][2]=getImage(getCodeBase(),"A9.jpg"); /* * Store the right picture positions by * copying the pic to rightPic array if images */ rightPic=new Image[3][3]; for (int a=0;a