1

I started Java a few days ago, and was experimenting and fooling around with some Java codes in a Java tutorial I borrowed from the library.

It told me how to draw a string onto an applet using Graphics2D from java.awt and change its font color, size, type, etc.

Now I challenged myself to make the string appear in random places in the applet. I tried using Math.random() to change the x-y coordinates of the string but the variable type was different and I found myself puzzled. Is there any way to make the string appear in random places every time I open the applet? (I'm going into moving the string with an .awt button later.)

Here is my code:

package game;
import java.awt.*;

public class Javagame extends javax.swing.JApplet{
    public void paint(Graphics screen) {
        Graphics2D screen2D = (Graphics2D) screen;
        Font font = new Font("Arial Black", Font.PLAIN, 20);
        screen2D.setFont(font);
        screen2D.drawString("Hello World!", 50, 50); /*right now it is set at 50, 50
        but I want random variables. Thanks*/

    }
}
BitLion
  • 103
  • 1
  • 18
  • replace 50, 50 with random values – Nico May 30 '13 at 01:08
  • I tried `Math.random();` already, but could you elaborate a bit more please? I tried it and it gave me the error `The method random() in the type Math is not applicable for the arguments (int, int)` because it is actually supposed to be `Math.random(double, double)`. – BitLion May 30 '13 at 01:12
  • Yeah cast it to int like so: (int) Math.random() * some value, see the answers below. – Nico May 30 '13 at 01:13

3 Answers3

3

You want to use something like:

screen2D.drawString("Hello World!", 
    (int)(Math.random()*width), 
    (int)(Math.random()*height));

Where width and height are the maximum values of X and Y you want. See this related question: "Generating random numbers in a range."

Community
  • 1
  • 1
Jesse Craig
  • 560
  • 5
  • 18
  • Hello, sorry for the late reply, thank you for this information! I put it in and yes, the string now comes out in random locations. Edit: But what was weird is that the string changes location every time I change/resize the window size. Why is that? – BitLion May 30 '13 at 04:09
  • The paint function is called automatically each time a window is resized. The paint function will also automatically be called in many other circumstances. If that is an issue you could choose your values for X and Y outside the paint function so they don't keep changing. – Jesse Craig May 30 '13 at 10:19
2

You could try using the random class:

Random r = new Random();
int x = r.nextInt(100);
int y = r.nextInt(100);

This will generate a random integer between 0 and the value specified.

Andrew Cumming
  • 965
  • 6
  • 14
1

From memory, Math.ramdom() returns a double value 0-1. The position parameters will require a int.

You need to cast the double value to an int.

screen2D.drawString("Hello World!", (int)(Math.round(Math.random() * getWidth()), (int)(Math.round(Math.random() * getWidth()));

Normally, if you do (int)doubleValue, it will simply trim of the trailing decimal values. This might be a good, this might be a bad thing. I tend to round the value first, but that's just me.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366