0

User enters his/her name and lucky number. Color background, the numbers and each sentence in different colors. Use different size font style for text and numbers. All random numbers should be placed randomly on the screen

I want to place the numbers randomly on the screen in different places but have them not go on top (overlap) the sentences placed such as use your intuition, excellent guess etc. I tried adding numbers to the math.random but it only restricts it for that number. A number in that area will always be that one color. i.e. for a in my code, it will always be green in that space. I'm new to coding, i dont know what i would do. Any advice would be greatly appriciated.

I commentd some parts because i was checking for a and b.

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;

public class luckynumbers extends Applet{
    Calendar now = Calendar.getInstance();
    int year = now.get(Calendar.YEAR);
    int month = now.get(Calendar.MONTH) + 1;
    int day = now.get(Calendar.DAY_OF_MONTH);
    int name;
    double lucky;
    int a = (int)(0+Math.random() * 99);
    int b = (int)(Math.random() * 99);
//  int c = (int)(Math.random() * 99);
//  int d = (int)(Math.random() * 99);
//  int e = (int)(Math.random() * 99);
//  int f = (int)(Math.random() * 99);
    int w1 = (int)(0+Math.random() * 100);
    int h1 = (int)(0+Math.random() * 200+200);
    int w2 = (int)(Math.random() * 300+100);
    int h2 = (int)(Math.random() * 400+200);
//    int w3 = (int)(Math.random() * 300);
//    int h3 = (int)(Math.random() * 300);
//    int w4 = (int)(Math.random() * 300);
//    int h4 = (int)(Math.random() * 300);
//    int w5 = (int)(Math.random() * 300);
//    int h5 = (int)(Math.random() * 300);
//    int w6 = (int)(Math.random() * 300);
//    int h6 = (int)(Math.random() * 300);

    public void init(){
        String name;
        String input1 = JOptionPane.showInputDialog("Please, enter your name");
        name = input1;
        String input2 = JOptionPane.showInputDialog("Please, enter your lucky number?");
        lucky = Integer.parseInt(input2);
    }

    public void paint(Graphics g){
        Color blue2 = new Color(156, 207, 222);
        Color pink = new Color(212, 84, 129);
        Color bg = new Color(200, 150, 204); //RGB
        setBackground(bg);
        Font z = new Font("ComicSans", Font.BOLD, 45);
        g.setFont(z);
        g.setColor(Color.blue);
        g.drawString("LUCKY NUMBERS GAME", 50,55); //Text starts at that point
        g.drawString(" by " + name,750,55);

        z = new Font("ComicSans", Font.BOLD, 16);
        g.setColor(Color.red);
        g.drawString("Lucky Number: " + (Math.round(lucky)), 750,350);

        g.setColor(Color.green);
        z = new Font("Impact", Font.ITALIC, 50);
        g.setFont(z);
        g.drawString(a +" ", w1, h1);

        g.setColor(Color.yellow);
        g.drawString(b +" ", w2, h2);
//      
//      g.setColor(Color.black);
//      g.drawString(c +" ", w3, h3);
//      
//      g.setColor(Color.red);
//      g.drawString(d +" ", w4, h4);
//      
//      g.setColor(Color.blue2);
//      g.drawString(e +" ", w5, h5);
//      
//      g.setColor(Color.pink);
//      g.drawString(f +" ", w6, h6);


        g.setColor(Color.red);
        z = new Font("Verdana", Font.ITALIC, 18);
        g.setFont(z);
        g.drawString("Use your intuition! ", 400, 100);

        g.setColor(Color.orange);
        z = new Font("Curlz MT", Font.ITALIC, 20);
        g.setFont(z);
        g.drawString("Try to guess the number! ", 50, 150);

        g.setColor(pink);
        z = new Font("Garamond", Font.BOLD, 32);
        g.setFont(z);
        g.drawString("Excellent guess! ", 280, 260);

        g.setColor(blue2);
        z = new Font("Lucida Sans", Font.ITALIC, 27);
        g.setFont(z);
        g.drawString("Try a larger number! ", 10, 430);


        g.setColor(Color.yellow);
        z = new Font("Arial", Font.BOLD, 20);
        g.setFont(z);
        g.drawString("You can always play again! ", 450, 600);
        z= new Font("Arial", Font.ITALIC, 25);
        g.setFont(z); 
        g.setColor(Color.white);
        g.drawString("Today's Date: "+day+"/"+month+"/"+year,900,500);

    }
}
JayD
  • 1
  • 1
  • You need to know where the text for the sentence is – MadProgrammer Dec 13 '15 at 23:56
  • The locations where i placed them are in my code. But what do I do with them? @MadProgrammer – JayD Dec 14 '15 at 00:12
  • when you are computing position for a random number, you need to check, if the position does collide with a text-position. In this case just get a new random position – JohnnyAW Dec 14 '15 at 00:17
  • No, you need to make some kind of list where the sentences appear, so that when you generate your "random" locations, you can do some "collision detection" and determine if you need to generate a new location. – MadProgrammer Dec 14 '15 at 00:19
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 14 '15 at 20:15

0 Answers0