Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Question Adding Images/Colours into a Java Quiz

    Hello,

    I am making a quiz in Java. I need to be able to add images and colours into my quiz, to make the GUI look more appealing. At the moment, the JOptionPane that I am using looks very plain and boring. I want to be able to have my quiz running the same as it is at the moment, but I want to be able to import the images, add colours, and add Here is a copy of my quiz code:

    Java Code:
    package quiz;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.util.Scanner;
    
    import javax.swing.AbstractButton;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    import java.applet.AudioClip;
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import java.util.Timer;
    
    public class Main {
    	
    	BufferedImage img = null; 
    	BufferedImage img2 = null;
    	 
    	Image dbImage;
    	Graphics dbg;
    	 
    	private ImageIcon image;
    	private JLabel label;
    
    		public static void main(String[] args) {
    
    			int score = 0;
    			
    			int seconds = 0;
    
    			int loop1 = 0;
    			int loop2 = 0;
    			int loop3 = 0;
    			int loop4 = 0;
    			int loop5 = 0;
    			int loop6 = 0;
    			int loop7 = 0;
    			int loop8 = 0;
    			int loop9 = 0;
    			int loop10 = 0;
    			int loop11 = 0;
    
    	String name = JOptionPane.showInputDialog(null, "What is your name? ");
    	while (loop1 < 100){
    	int start = JOptionPane.showConfirmDialog(null, "Hello " + name + ". Welcome to the Game Show. In this quiz, you will be given $1 to start with. There will be 20 multiple choice questions. \n" +
    	"You will have 15 seconds to answer each question. Answer each question with the letter (A, B, C or D) corresponding with that answer. \n If you fail to answer in that time, or if you give an" +
    	" incorrect answer, you will be sent home with $1. \n If you give a correct answer, your money will double. \n After each question, you will be given the choice to either leave the game show with" +
    	"half of the money you have made, or continue to the next question. \n You will have 15 seconds to make this decision. If you do not give an answer, you will automatically proceed to the next question. \n" +
    	"You cannot choose to leave the game show after you proceed to the next question.\n \n Would you like to begin? Yes or no? ");
    	if(start == JOptionPane.NO_OPTION) {
    	JOptionPane.showMessageDialog(null, "Ok. Get ready, and when you feel you are ready to begin, click 'Ok'.");
    	loop1++;
    	break;
    	}
    	if(start == JOptionPane.YES_OPTION) {
    	break;
    	}
    	else{
    	System.exit(0);
    	break;
    	}
    	}
    	while (loop2 < 1){
    	String Q1 = JOptionPane.showInputDialog(null, "Question 1: What was the original name of the Java programming language? \n A) Coffee   B) JCode   C) Oak   D) Green");
    	if(Q1.equalsIgnoreCase("C")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score += 1;
    	break;
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop2++;
    	}
    	while (loop3 < 1){
    	String Q2 = JOptionPane.showInputDialog(null, "Question 2: Who invented the internet? \n A) Theodore J. Nettick and G. Riley   B) Robert E. Kahn and Vint Cerf   C) J.Downs   D) Edward Webington and Mark Smith");
    	if(Q2.equalsIgnoreCase("B")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score += 1;
    	break;
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop3++;
    	}
    	while (loop4 < 1){
    	String Q3 = JOptionPane.showInputDialog(null, "Question 3: What hardware component of a computer is commonly referred to as the brain? \n A) Hard Drive   B) Motherboard   C) Graphics Card   D) CPU");
    	if(Q3.equalsIgnoreCase("D")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score += 1;
    	break;
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop4++;
    	}
    	while (loop5 < 1){
    	String Q4 = JOptionPane.showInputDialog(null, "Question 4: What was the name of the world's first computer? \n A) GENCOM   B) ENIAC   C) ECOD   D) ECOM");
    	if(Q4.equalsIgnoreCase("B")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score += 1;
    	break;
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop5++;
    	}
    	while (loop6 < 1){
    	String Q5 = JOptionPane.showInputDialog(null, "Question 5: What is the name of the world's most powerful computer? \n A) Titan   B) Mira   C) Bien-do 8   D) Tianhe-2");
    	if(Q5.equalsIgnoreCase("D")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score += 1;
    	break;
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop6++;
    	}
    	while (loop7 < 1){
    	String Q6 = JOptionPane.showInputDialog(null, "Question 6: How many calculations per second can the Tianhe-2 supercomputer complete? \n A) 33.86 quadrillion   B) 10.67 billion   C) 5.48 trillion   D) 96.98 quintillion");
    	if(Q6.equalsIgnoreCase("A")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score += 1;
    	break;
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop7++;
    	}
    	}
    }
    Here is a screenshot of how I have added the images into the src folder:
    Adding Images/Colours into a Java Quiz-screen-shot-2014-03-26-12.27.49-pm.png

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding Images/Colours into a Java Quiz

    Quote Originally Posted by JavaPr0grammer View Post
    I am making a quiz in Java. I need to be able to add images and colours into my quiz, to make the GUI look more appealing. At the moment, the JOptionPane that I am using looks very plain and boring. I want to be able to have my quiz running the same as it is at the moment, but I want to be able to import the images, add colours, and add
    A JOptionPane can display more than just Strings; it can display icons and other Components (such as JLabels and JPanels etc.) Imagine a fancy looking JPanel and you make your JOptionPane display it ... read the API documentation how to do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: Adding Images/Colours into a Java Quiz

    Thanks Jos, I've been reading through a lot of API about JOptionPanes, Icons, JLabels, JColorChooser, and JButton, and I've tried to design a JOptionPane to pop up at the end of my quiz with an icon (with text and one of the pictures that I have imported). It's just not working. Can you please show me how you would go about doing this? I would really appreciate it. Thanks!

Similar Threads

  1. error when adding Images
    By Daryn in forum New To Java
    Replies: 5
    Last Post: 01-18-2014, 01:27 AM
  2. adding images
    By alexix in forum New To Java
    Replies: 3
    Last Post: 03-04-2013, 10:36 PM
  3. adding different images using if statements
    By JoePenguin in forum New To Java
    Replies: 3
    Last Post: 02-16-2012, 03:54 AM
  4. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 06:19 AM
  5. [new] need help on GUI adding images
    By counterfox in forum AWT / Swing
    Replies: 5
    Last Post: 03-19-2009, 10:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •