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 9 of 9

Thread: Beginner seeking help with quick problem in program

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Beginner seeking help with quick problem in program

    Basically I am very new to using java and have only done a few very simple programs. With the program attached I'm trying to make the test go RED when the person has used 4 of their 5 chances. I've tried a few different if and else if statements and nothing seems to work. To be clear i want it to just be RED when they've used 4 out of 5 chances but plain black text otherwise. Any help is very appreciated
    Attached Files Attached Files


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Beginner seeking help with quick problem in program

    If that attachment is code, cut and paste it into the chat and wrap it in "[.code] [./code]" (taking out the periods)
    More people are willing to read code if it is done like this, few will be willing to download your attachment, even if it is a harmless txt.

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

    Gravs2889 (February 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Beginner seeking help with quick problem in program

    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
     
    public class secretnumb extends Applet implements MouseListener
    {
    	Image randy;
    	int secret = (int)(Math.random()*900+101);
    	int mouseX;
    	int mouseY;
    	int leftClicks=0;
     
     
    	public void init()
    	{
     
    		randy = getImage(getCodeBase(), "randy.jpg");
    		addMouseListener(this);
    		System.out.println("Ran init");
     
    	}
     
    	public void paint(Graphics g)
    	{
    			g.drawLine(100,500,1000,500);  //numb line
    			int y = 100;
    			int x = 1;
    			while (y <= 1000)
    		{
    			g.drawLine(y, 500, y, 475);
    			g.drawString(" " +x, y-8, 525);
    			y = y+100;
    			x = x+1;
    		}
     
    			if (mouseX <100)    //tells you to click on line
    			{
    				g.drawString("Click on the Number Line Please ; )", 550, 300);
    			}
    			else if ( mouseX > 1000 || (mouseY <= 425 || mouseY >= 550) && ( (mouseX <= 99 || mouseX >=1050) || (mouseY <= 100 || mouseY >=200) )  )
    			{
    							g.drawString("Click on the Number Line Please ; )", 550, 300);
    			}
    			g.drawString("You've used  " + leftClicks +"  out of  5 Chances!!", 525 , 100);
    			if (mouseX >= 100 && mouseX <=1050 && mouseY >= 450 && mouseY <=525)
    			{
     
    				leftClicks++;
     
     
    			}
    			else if (leftClicks==4)
    			{
    				g.setColor(Color.red);
    				g.drawString("You've used" + leftClicks+"out of 5 Chances!!",525,400);
    			}
     
     
    			if (leftClicks > 5)
    			{
    				g.drawString("Try Again :P", 300,400);
    				leftClicks = 0;
     
    			}
     
    			if (mouseX > (secret-50)  && mouseX <  (secret + 50)  && mouseY >= 450 && mouseY <= 525) //win
    				{
    					g.setColor(Color.white);
    					g.fillRect(0,0,1366,768);
    					g.drawImage(randy,453,5,this);
    					g.setColor(Color.black);
    					g.drawString("You Win",453,500);
     
     
     
    				}
     
     
     
    			else  if (mouseX > (secret-100)  && mouseX < (secret-50)  && mouseY >= 425 && mouseY <= 550) //close to number
    			{
    				g.drawString("Little Higher", 600,550);
    			}
     
    			 else  if (mouseX > (secret+ 50)  && mouseX <  (secret + 100)  && mouseY >= 425 && mouseY <= 550)
    						{
    							g.drawString("Little Lower", 600, 550);
    						}
     
    			 if(mouseX>(secret-10) && mouseX>=100 && mouseY >=425 && mouseY<=5550)
    					{
    						g.drawString("Left!",460,550);
    					}
     
    			 if(mouseX<(secret-10)&& mouseX<1000 && mouseY>=425 && mouseY<=550)
    					{
    						g.drawString("RIGHT!",460,550);
    					}
     
     
     
    			if (mouseX > (secret+250)  || mouseX <  (secret - 250)  && mouseY >= 425 && mouseY <= 550)
    			{
    				g.drawString("Yikes! You are Far Away", 460, 300);
     
    			}
     
     
    			else	if (mouseX >= 150 && mouseX <=250 && mouseY >= 100 && mouseY <=200) // secret number
    	 		{
    	 					g.drawString("Secret Number is " + secret/100.0, 140 , 225);
     
    			}
     
     
     
     
    	}
     
    	public void mouseExited(MouseEvent me)
    	{
    	}
     
    	public void mouseEntered(MouseEvent me)
    	{
    	}
    	public void mousePressed(MouseEvent me)
    	{
    	}
    	public void mouseReleased(MouseEvent me)
    	{
    	}
    	public void mouseClicked(MouseEvent me)
    	{
     
    			System.out.println("Mouse X Clicked at " + me.getX());
    			System.out.println("Mouse Y Clicked  at " + me.getY());
    			mouseX = me.getX();
    			mouseY = me.getY();
    			repaint();
     
     
    	}
     
    }

  5. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Beginner seeking help with quick problem in program

    need a main method to start!

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

    Default Re: Beginner seeking help with quick problem in program

    Can you elaborate?

  7. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Beginner seeking help with quick problem in program

    Java needs a starting point for the program to begin is linear understanding of your code.
    This starting point is always the main method (to my knowledge)

    Try adding this method in your secretnumb class:

    public static void main(String[] args){
    		init();
     
    }

    On a side note, is this your first program? (pretty big for a first timer)

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

    Gravs2889 (February 23rd, 2012)

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

    Default Re: Beginner seeking help with quick problem in program

    second program, and honestly I've never heard of defining a main method which is probably not a good thing haha. I appreciate your help,essentially would i put that code in
    public class secretnumb extends Applet implements MouseListener
    {
    	Image randy;
    	int secret = (int)(Math.random()*900+101);
    	int mouseX;
    	int mouseY;
    	int leftClicks=0;
    ?

  10. #8
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Beginner seeking help with quick problem in program

    main method can go anywhere in the class scope, so where you have chosen will work, however, most choose to put it at the end (just before the last } )

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

    Default Re: Beginner seeking help with quick problem in program

    I've messed around with it and i'm still unable to get it to work. I guess i'm just confused on why
    			g.drawString("You've used  " + leftClicks +"  out of  5 Chances!!", 525 , 100);
    			if (mouseX >= 100 && mouseX <=1050 && mouseY >= 450 && mouseY <=525)
    			{
     
    				leftClicks++;
     
     
    			}
    			else if (leftClicks==4)
    			{
    				g.setColor(Color.red);
    				g.drawString("You've used" + leftClicks+"out of 5 Chances!!",525,400);
    			}
     
     
    			if (leftClicks > 5)
    			{
    				g.drawString("Try Again :P", 300,400);
    				leftClicks = 0;
     
    			}
    will not work

Similar Threads

  1. Replies: 6
    Last Post: February 21st, 2012, 09:41 PM
  2. Beginner needs help with a program
    By MartinC in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 09:51 AM
  3. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 07:30 PM
  4. Quick beginner radio button question
    By smarmy_cheauffeur in forum AWT / Java Swing
    Replies: 2
    Last Post: October 17th, 2011, 08:20 AM
  5. Quick, beginner-level Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2011, 01:06 PM