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);
}
}
}
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);
}
}
}
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!