Random Text Help

Ok, I've been looking at this code for a couple days and still have no idea where I went wrong. This is a class that is supposed to draw a specified message a certain amount of times at random coordinates in a Jframe. The only problem is it never draws anything. It's almost like the loop is just being completely ignored.

import java.util.Random;

import javax.swing.*;

import java.awt.*;

import java.awt.Graphics2D;

publicclass DrawTextextends JComponent

{

private String text;

privateint amountoftext;

privateint xrand;

privateint yrand;

public DrawText(String t,int b)

{

text = t;

amountoftext = b;

}

publicvoid paintComponent (Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

Random randomx =new Random();

Random randomy =new Random();

for (int i=amountoftext; i>0; i--)

{

int xrand = randomx.nextInt(100)+10;

int yrand = randomy.nextInt(100)+10;

amountoftext = amountoftext - 1;

g2.drawString(text, xrand, yrand);

}

}

}

Oh and the driver for this class that I'm using is here:

import javax.swing.*;

import java.util.*;

publicclass GraphicsTest

{

publicstaticvoid main(String[] a)

{

int width=400;

int height=300;

Scanner input =new Scanner(System.in);

System.out.println("What would you like to draw?");

System.out.print("1. A nifty Right Triangle\n2. A circular Circle\n3. Some neat text\n(Choose a Number)\n");

int selection = input.nextInt();

if (selection == 3)

{

Scanner textin =new Scanner(System.in);

System.out.print("What do you want the text to say? ");

String textinput = textin.nextLine();

System.out.print("How many times should this text appear? ");

int textamount = textin.nextInt();

JFrame textframe=new JFrame();

textframe.setSize(width,height);

textframe.setTitle("Text");

textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DrawText Text=new DrawText(textinput,textamount);

textframe.add(Text);

textframe.setVisible(true);

}

}

}

[4078 byte] By [ThinSpirita] at [2007-10-2 4:03:02]
# 1

When I tried running your code, I got this error messageException in thread "main" java.lang.Error: Do not use javax.swing.JFrame.add()

use javax.swing.JFrame.getContentPane().add() insteadThat pretty much tells you what you need to do. Did you see the error message? You should always post the message when asking for help.

Change the line as followstextframe.getContentPane().add(Text);

atmguya at 2007-7-15 23:25:33 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Weird, I never got that error message at all. I changed what you said and it ran exactly the same as before. It's still not printing the text lie I want it.I'm aiming for it to look something like
ThinSpirita at 2007-7-15 23:25:33 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

I do not have J2sdk 1.5 so I changed your code. Here is what I compiled and ran.import java.util.Random;

import javax.swing.*;

import java.awt.*;

import java.awt.Graphics2D;

public class DrawText extends JComponent

{

private String text;

private int amountoftext;

private int xrand;

private int yrand;

public DrawText(String t, int b)

{

text = t;

amountoftext = b;

}

public void paintComponent (Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

Random randomx = new Random();

Random randomy = new Random();

for (int i=amountoftext; i>0; i--)

{

int xrand = randomx.nextInt(100)+10;

int yrand = randomy.nextInt(100)+10;

amountoftext = amountoftext - 1;

g2.drawString(text, xrand, yrand);

}

}

public static void main(String[] a)

{

int width=400;

int height=300;

/*

Scanner input = new Scanner(System.in);

System.out.println("What would you like to draw?");

System.out.print("1. A nifty Right Triangle\n2. A circular Circle\n3. Some neat text\n(Choose a Number)\n");

int selection = input.nextInt();

*/

int selection = 3;

String textinput = "hello";

if (selection == 3)

{

// Scanner textin = new Scanner(System.in);

// System.out.print("What do you want the text to say? ");

// String textinput = textin.nextLine();

// System.out.print("How many times should this text appear? ");

int textamount = 2;

JFrame textframe=new JFrame();

textframe.setSize(width,height);

textframe.setTitle("Text");

textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DrawText Text=new DrawText(textinput,textamount);

textframe.add(Text);

textframe.setVisible(true);

}

}

}

atmguya at 2007-7-15 23:25:33 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

Thanks for the help, but I discovered the problem I was having. I made the error in my loop. I changed this:

for (int i=amountoftext; i>0; i--)

{

int xrand = randomx.nextInt(100)+10;

int yrand = randomy.nextInt(100)+10;

amountoftext = amountoftext - 1;

g2.drawString(text, xrand, yrand);

}

into this:

for (int i = 0; i < amountoftext; i++)

{

int xrand = randomx.nextInt(width)+10;

int yrand = randomy.nextInt(height)+10;

g2.drawString(text, xrand, yrand);

}

Thanks for the help!

ThinSpirita at 2007-7-15 23:25:33 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...