Skip to content

Instantly share code, notes, and snippets.

@ishanbhavsar3
Last active January 30, 2022 03:17
Show Gist options
  • Save ishanbhavsar3/aaeff43487833635d0e02bcfec109d34 to your computer and use it in GitHub Desktop.
Save ishanbhavsar3/aaeff43487833635d0e02bcfec109d34 to your computer and use it in GitHub Desktop.
A program where the user guesses images' associations. Built using AWT and javax.swing classof java. Utilises java.net & java.util packages
import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
public class ImageGuesser {
int con, count;
JFrame f;
void fetchEasy() throws MalformedURLException {
f = new JFrame();
URL img = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/2006_Ojiya_balloon_festival_011.jpg/319px-2006_Ojiya_balloon_festival_011.jpg");
ImageIcon image = new ImageIcon(img);
JLabel icon = new JLabel(image);
JLabel text = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(icon, BorderLayout.CENTER);
panel.add(text, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel, "RandomImagePanel", JOptionPane.PLAIN_MESSAGE);
URL img1 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Balloons-41362.png/213px-Balloons-41362.png");
ImageIcon image1 = new ImageIcon(img1);
JLabel icon1 = new JLabel(image1);
JLabel text1 = new JLabel("Please look at this image for 20 seconds.Then press enter.", SwingConstants.CENTER);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(icon1, BorderLayout.CENTER);
panel1.add(text1, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel1, "RandomImagePanel1", JOptionPane.PLAIN_MESSAGE);
URL img8 = new URL("https://upload.wikimedia.org/wikipedia/commons/e/ec/Aeron_chair_JN.jpg");
ImageIcon image8 = new ImageIcon(img8);
JLabel icon8 = new JLabel(image8);
JLabel text8 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel8 = new JPanel();
panel8.setLayout(new BorderLayout());
panel8.add(icon8, BorderLayout.CENTER);
panel8.add(text8, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel8, "RandomImagePanel6", JOptionPane.PLAIN_MESSAGE);
URL img9 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Urval_av_de_bocker_som_har_vunnit_Nordiska_radets_litteraturpris_under_de_50_ar_som_priset_funnits_%282%29.jpg/220px-Urval_av_de_bocker_som_har_vunnit_Nordiska_radets_litteraturpris_under_de_50_ar_som_priset_funnits_%282%29.jpg");
ImageIcon image9 = new ImageIcon(img9);
JLabel icon9 = new JLabel(image9);
JLabel text9 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel9 = new JPanel();
panel9.setLayout(new BorderLayout());
panel9.add(icon9, BorderLayout.CENTER);
panel9.add(text9, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel9, "RandomImagePanel6", JOptionPane.PLAIN_MESSAGE);
URL img10 = new URL("https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Flag_of_India.svg/320px-Flag_of_India.svg.png");
ImageIcon image10 = new ImageIcon(img10);
JLabel icon10 = new JLabel(image10);
JLabel text10 = new JLabel("Please look at this image for 20 seconds.\nThen press enter.", SwingConstants.CENTER);
JPanel panel10 = new JPanel();
panel10.setLayout(new BorderLayout());
panel10.add(icon10, BorderLayout.CENTER);
panel10.add(text10, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel10, "RandomImagePanel6", JOptionPane.PLAIN_MESSAGE);
String ez = JOptionPane.showInputDialog("Input any of the names of the things you saw");
String ez_ = ez.toUpperCase();
if (ez_.equals("TREE") || ez_.equals("FLAG") || ez_.equals("INDIAN FLAG") || ez_.equals("TRICOLOUR") || ez_.equals("CHAIR") || ez_.equals("BALLOON") || ez_.equals("HOT AIR BALLOON") || ez_.equals("")) {
JOptionPane.showMessageDialog(f, "Yay! It's a correct guess.");
} else {
JOptionPane.showMessageDialog(f, "Please replay.");
caller();
count = +1;
}
}
void fetchMedium() throws MalformedURLException {
f = new JFrame();
URL img2 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/ANA_777-300_Taking_off_from_JFK.jpg/220px-ANA_777-300_Taking_off_from_JFK.jpg");
ImageIcon image2 = new ImageIcon(img2);
JLabel icon2 = new JLabel(image2);
JLabel text2 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(icon2, BorderLayout.CENTER);
panel2.add(text2, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel2, "RandomImagePanel", JOptionPane.PLAIN_MESSAGE);
URL img3 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Container_ship_Reecon_Whale_on_Black_Sea_near_Constan%C8%9Ba_Romania.jpg/300px-Container_ship_Reecon_Whale_on_Black_Sea_near_Constan%C8%9Ba_Romania.jpg");
ImageIcon image3 = new ImageIcon(img3);
JLabel icon3 = new JLabel(image3);
JLabel text3 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(icon3, BorderLayout.CENTER);
panel3.add(text3, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel3, "RandomImagePanel", JOptionPane.PLAIN_MESSAGE);
URL img4 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/401_Gridlock.jpg/300px-401_Gridlock.jpg");
ImageIcon image4 = new ImageIcon(img4);
JLabel icon4 = new JLabel(image4);
JLabel text4 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel4 = new JPanel();
panel4.setLayout(new BorderLayout());
panel4.add(icon4, BorderLayout.CENTER);
panel4.add(text4, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel4, "RandomImagePanel", JOptionPane.PLAIN_MESSAGE);
String med = JOptionPane.showInputDialog("Input any of the names of the things you saw");
String med_ = med.toUpperCase();
if (med_.equals("CARS") || med_.equals("TRAFFIC") || med_.equals("CONTAINER SHIP") || med_.equals("CAR") || med_.equals("SHIP") || med_.equals("FREIGHT SHIP") || med_.equals("AEROPLANE") || med_.equals("PLANE")) {
JOptionPane.showMessageDialog(f, "Yay! It's a correct guess.");
} else {
JOptionPane.showMessageDialog(f, "Please replay.");
caller();
count = +1;
}
}
void fetchHard() throws MalformedURLException {
URL img6 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Amiga500_system.jpg/309px-Amiga500_system.jpg");
ImageIcon image6 = new ImageIcon(img6);
JLabel icon6 = new JLabel(image6);
JLabel text6 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel6 = new JPanel();
panel6.setLayout(new BorderLayout());
panel6.add(icon6, BorderLayout.CENTER);
panel6.add(text6, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel6, "RandomImagePanel6", JOptionPane.PLAIN_MESSAGE);
URL img7 = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/IPhone_5.png/254px-IPhone_5.png");
ImageIcon image7 = new ImageIcon(img7);
JLabel icon7 = new JLabel(image7);
JLabel text7 = new JLabel("Please look at this image for 20 seconds.\nThen press enter", SwingConstants.CENTER);
JPanel panel7 = new JPanel();
panel7.setLayout(new BorderLayout());
panel7.add(icon7, BorderLayout.CENTER);
panel7.add(text7, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel7, "RandomImagePanel6", JOptionPane.PLAIN_MESSAGE);
String hard = JOptionPane.showInputDialog("Input any of the names of the following devices you saw");
String hard_ = hard.toUpperCase();
if (hard_.equals("COMPUTER") || hard_.equals("MOBILE PHONE") || hard_.equals("MOBILE") || hard_.equals("PHONE")) {
JOptionPane.showMessageDialog(f, "Yay! It's a correct guess.");
} else {
JOptionPane.showMessageDialog(f, "Please replay.");
caller();
count = +1;
}
}
void caller() throws MalformedURLException {
if (count >= 1) {
JOptionPane.showMessageDialog(f, "Input 0 when asked to enter difficulty level if you wish to to exit the program");
}
String ask = JOptionPane.showInputDialog(f, "Please enter your difficulty level : " +
"\nInput 1 for easy." +
"\nInput 2 for medium." +
"\nInput 3 for hard.");
con = Integer.parseInt(ask);
Src object = new Src();
if (con == 0) {
JOptionPane.showMessageDialog(f, " Thank you for using our program! Goodbye!");
System.exit(0);
}
if (con == 1) {
object.fetchEasy();
} else if (con == 2) {
object.fetchMedium();
} else if (con == 3) {
object.fetchHard();
} else {
JOptionPane.showMessageDialog(f, "Please play again!");
caller();
}
}
public static void main(String[] args) throws MalformedURLException {
ImageGuesser call = new ImageGuesser();
call.caller();
}
}
/*
* Copyright (c) 2022 - 2023 Ishan Ketan Bhavsar & Mohak Rasik Dave
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment