Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Logo quiz

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Logo quiz

    Hi,
    I'm making a logo quiz just for fun but i do not understand why it is not working. Maybe someone can help me out?
    When i press the "volgende" (which is Dutch for "next" ) button it should go to th next image if the right key is inputted in the textfield.
    So this is my program:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.TextField;
    import javax.swing.JButton;
     
    public class logoprogramma extends Applet implements ActionListener
    {
    	TextField logonaam;
    	JButton volgendeknop = new JButton("volgende");
    	boolean geklikt;
    	boolean klaar;
    	Image plaatje;
    	MediaTracker tr;
    	String sleutel[]={"remia", "burger king", "ebay", "michelin", "pringles", "red bull"}; 				
    	String plaatjes[]={"../plaatjes/Remia.gif", "../plaatjes/burger king.jpg", "../plaatjes/ebay.jpg", "../plaatjes/michelin.jpg", "../plaatjes/pringles.jpg", "../plaatjes/red bull.jpg"}; //<------------------
    	int teller, aantal_vragen;
    	String antwoord="?"; 															
    	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	int breedteScherm = dim.width;
    	int hoogteScherm = dim.height;
    	int plaatsPlaatjeY = (hoogteScherm - 140)/2;
    	int plaatsPlaatjeX = (breedteScherm - 225)/2;
    	int score = teller;
     
     
     
    	public void init()
    	{
     
    		setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
    		logonaam = new TextField(20);
    		this.add(logonaam);	
    		teller = 0;
    		aantal_vragen = 6;												
    		klaar = false;
    		logonaam.addActionListener(this);
    		antwoord = logonaam.getText();
    		this.add(volgendeknop);
    		volgendeknop.addActionListener(this);
    		geklikt = false;
     
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		geklikt = true;
    		repaint();
     
    	}
     
    	public void paint (Graphics gr)
    	{
    		if (geklikt = true){
    		if (antwoord.equalsIgnoreCase(sleutel[teller]))
    			{
    				teller++;
    				score++;
    				if (teller == aantal_vragen) klaar = true;
    			}
     
    		if (klaar)
    		{	
    			gr.drawString("uw score is " + score, plaatsPlaatjeX, plaatsPlaatjeY);
    			gr.setColor(Color.GREEN);
    			gr.fillOval(50, 50, 100, 100);
    			gr.setColor(Color.BLUE);
    			gr.fillOval(81, 85, 8, 8);
    			gr.fillOval(111, 85, 8, 8);
    			gr.drawArc(75, 75, 50, 50, 225, 90);	
    		}
    		else 									
    		{
    			tr = new MediaTracker(this);
    			logonaam.setText("");
    			plaatje = getImage(getCodeBase(), plaatjes[teller]);
    			tr.addImage(plaatje,0);
    			gr.drawImage(plaatje, plaatsPlaatjeX, plaatsPlaatjeY, this);
    		}
     
    	}
    	}
    private static final long serialVersionUId = 1L;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Logo quiz

    it is not working
    Please explain what " it is not working" means.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    big daan1234 (December 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Logo quiz

    Thank you for your advice, I have changed what you asked for.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Logo quiz

    if the right key is inputted in the textfield.
    Can you put the "right key" in the textfield so all that is required is pressing the next button?

    Try debugging the code by adding println statements that print out the values of the variables that are used to control the execution.
    The print out will show you what the computer sees and when it sees it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Logo quiz

    the first key is "remia" the second key is "burger king" third "ebay" fourth key is "michelin" fifth key is "pringles" sixth key is "red bull"

    but here is the edited code which will always give you the right key:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.TextField;
    import javax.swing.JButton;
     
    public class logoprogramma extends Applet implements ActionListener
    {
    	TextField logonaam;
    	JButton volgendeknop = new JButton("volgende");
    	boolean geklikt;
    	boolean klaar;
    	Image plaatje;
    	MediaTracker tr;
    	String sleutel[]={"remia", "burger king", "ebay", "michelin", "pringles", "red bull"}; 				
    	String plaatjes[]={"../plaatjes/Remia.gif", "../plaatjes/burger king.jpg", "../plaatjes/ebay.jpg", "../plaatjes/michelin.jpg", "../plaatjes/pringles.jpg", "../plaatjes/red bull.jpg"}; //<------------------
    	int teller, aantal_vragen;
    	String antwoord="?"; 															
    	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	int breedteScherm = dim.width;
    	int hoogteScherm = dim.height;
    	int plaatsPlaatjeY = (hoogteScherm - 140)/2;
    	int plaatsPlaatjeX = (breedteScherm - 225)/2;
    	int score = teller;
     
     
     
    	public void init()
    	{
     
    		setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
    		logonaam = new TextField(20);
    		this.add(logonaam);	
    		teller = 0;
    		aantal_vragen = 6;												
    		klaar = false;
    		logonaam.addActionListener(this);
    		antwoord = logonaam.getText();
    		this.add(volgendeknop);
    		volgendeknop.addActionListener(this);
    		geklikt = false;
     
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		geklikt = true;
    		repaint();
     
    	}
     
    	public void paint (Graphics gr)
    	{
    		if (geklikt = true){
    		if (antwoord.equalsIgnoreCase(sleutel[teller]))
    			{
    				teller++;
    				score++;
    				if (teller == aantal_vragen) klaar = true;
    			}
     
    		if (klaar)
    		{	
    			gr.drawString("uw score is " + score, plaatsPlaatjeX, plaatsPlaatjeY);
    			gr.setColor(Color.GREEN);
    			gr.fillOval(50, 50, 100, 100);
    			gr.setColor(Color.BLUE);
    			gr.fillOval(81, 85, 8, 8);
    			gr.fillOval(111, 85, 8, 8);
    			gr.drawArc(75, 75, 50, 50, 225, 90);	
    		}
    		else 									
    		{
    			tr = new MediaTracker(this);
    			logonaam.setText(sleutel[teller]);
    			plaatje = getImage(getCodeBase(), plaatjes[teller]);
    			tr.addImage(plaatje,0);
    			gr.drawImage(plaatje, plaatsPlaatjeX, plaatsPlaatjeY, this);
    		}
     
    	}
    	}
    private static final long serialVersionUId = 1L;
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Logo quiz

    Try debugging the code by adding println statements that print out the values of the variables that are used to control the execution.
    The print out will show you what the computer sees and when it sees it.

    One problem is overriding the paint() method for the applet where there are components also being shown. There should be separate components s for the images and for the other components.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    big daan1234 (December 15th, 2012)

  9. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Logo quiz

    tried it like this:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.TextField;
    import javax.swing.JButton;
     
    public class logoprogramma extends Applet implements ActionListener
    {
    	TextField logonaam;
    	JButton volgendeknop = new JButton("volgende");
    	boolean geklikt;
    	boolean klaar;
    	Image plaatje;
    	MediaTracker tr;
    	String sleutel[]={"remia", "burger king", "ebay", "michelin", "pringles", "red bull"}; 				//<-----------------
    	String plaatjes[]={"../plaatjes/Remia.gif", "../plaatjes/burger king.jpg", "../plaatjes/ebay.jpg", "../plaatjes/michelin.jpg", "../plaatjes/pringles.jpg", "../plaatjes/red bull.jpg"}; //<------------------
    	int teller, aantal_vragen;
    	String antwoord="?"; 															//<---------------------------------------
    	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	int breedteScherm = dim.width;
    	int hoogteScherm = dim.height;
    	int plaatsPlaatjeY = (hoogteScherm - 140)/2;
    	int plaatsPlaatjeX = (breedteScherm - 225)/2;
    	double score = teller;
     
     
     
    	public void init()
    	{
     
    		setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
    		logonaam = new TextField(20);
    		this.add(logonaam);	
    		teller = 0;
    		aantal_vragen = 6;												//<-----------------------------------------------
    		klaar = false;
    		logonaam.addActionListener(this);
    		antwoord = logonaam.getText();
    		this.add(volgendeknop);
    		volgendeknop.addActionListener(this);
    		geklikt = false;
     
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		geklikt = true;
     
    		repaint();
     
    	}
     
    	public void paint (Graphics gr)
    	{
    		if (geklikt = true){ System.out.println("button is clicked");
    		if (antwoord.equalsIgnoreCase(sleutel[teller]))
    			{System.out.println("key is right");
    				teller++;
    				score++;
    				if (teller == aantal_vragen) klaar = true;
    			}
    		else {score = score - 1;
    		System.out.println("score minus 1");}
    		if (klaar)
    		{	System.out.println("klaar");
    			gr.drawString("uw score is " + score, plaatsPlaatjeX, plaatsPlaatjeY);
    			gr.setColor(Color.GREEN);
    			gr.fillOval(50, 50, 100, 100);
    			gr.setColor(Color.BLUE);
    			gr.fillOval(81, 85, 8, 8);
    			gr.fillOval(111, 85, 8, 8);
    			gr.drawArc(75, 75, 50, 50, 225, 90);	
    		}
    		else 									//BETEKENT VOLGENDE VRAAG!
    		{	System.out.println("draw image");
    			tr = new MediaTracker(this);
    			logonaam.setText(sleutel[teller]);
    			plaatje = getImage(getCodeBase(), plaatjes[teller]);
    			tr.addImage(plaatje,0);
    			gr.drawImage(plaatje, plaatsPlaatjeX, plaatsPlaatjeY, this);
    		}
     
    	}
    	}
    private static final long serialVersionUId = 1L;
    }

    results that came out were:
    button is clicked
    score minus 1
    draw image
    button is clicked
    score minus 1
    draw image
    button is clicked
    score minus 1
    draw image
    button is clicked
    score minus 1
    draw image
    button is clicked
    score minus 1
    draw image
    button is clicked
    score minus 1
    draw image

    I can't see what is wrong by doing this.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Logo quiz

    What image is being drawn? Print out the image to see. Does it change? SHould it change?

    What about this printout: System.out.println("key is right");
    It's never executed.

    What about this:
    One problem is overriding the paint() method for the applet where there are components also being shown. There should be separate components for the images and for the other components.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Logo quiz

    Quote Originally Posted by Norm View Post
    What about this:
    One problem is overriding the paint() method for the applet where there are components also being shown. There should be separate components for the images and for the other components.
    I don't understand what you mean with this, could you explain?

    The image should change to the next image when the next(volgende) button is pressed and the key is right.
    Last edited by big daan1234; December 15th, 2012 at 10:38 AM. Reason: forgot something

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Logo quiz

    Define a JPanel, override its paintComponent() method, remove the paint() method from the applet, copy the code from the paint() method into the paintComponent method, create an instance of the JPanel and add it to the Applet class instance.

    The image should change to the next image when the next(volgende) button is pressed and the key is right.
    What about this printout: System.out.println("key is right");
    It's never executed.
    Print out the value of teller when the image is drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    big daan1234 (December 15th, 2012)

  14. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Logo quiz

    Thanks it worked!

Similar Threads

  1. AppInventor Quiz App
    By FA2 in forum The Cafe
    Replies: 4
    Last Post: October 10th, 2011, 03:03 PM
  2. [SOLVED] Addition Quiz
    By javaneedhelp in forum Loops & Control Statements
    Replies: 5
    Last Post: October 9th, 2011, 12:55 AM
  3. send image (logo) and text & barcode to the Zebra Printer
    By ehsanlinux in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 11th, 2011, 05:41 AM
  4. Quiz application
    By JonoF in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 10th, 2010, 06:06 AM
  5. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM

Tags for this Thread