Join 149,491 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,298 people online right now. Registration is fast and FREE... Join Now!
The following code is supposed to establish random points, and then draw them on the screen... this is as far as I have gotten because I keep gettting a illegal start of expression error when I try to complie the graphics portion... that is why it is commented out. I also need help figuring out how to make the decimel rounding work, so that it looks nicer. I am running out of time on this, as I need to have it posted to the site by this time tomorrow, so hopefully there is a generous guru out there that can help me with this code! Thanks in advance.
public class RandomPoints1A extends JApplet { public static void main (String[] args){
//variables int getx,gety,xleft,yleft,xright,yright,xtop,ytop,xbot,ybot; double distance, totaldist=0, minimum, maximum;
//creating the text area JTextArea mytextarea = new JTextArea(30,80); mytextarea.setLineWrap(true); mytextarea.setFont (new Font("Arial",Font.BOLD,18) ); JScrollPane scrollpane = new JScrollPane(mytextarea);
//get user input on number of points String input = JOptionPane.showInputDialog(null,"Please input the number of points you would like to display...","Enter a number",JOptionPane.QUESTION_MESSAGE); int number=Integer.parseInt(input);
//ask the user to determine the maximum size for x and y input = JOptionPane.showInputDialog(null,"What is the maximum value of 'X'","Max value of 'X'",JOptionPane.QUESTION_MESSAGE); int max_x = Integer.parseInt(input);
input = JOptionPane.showInputDialog(null,"What is the maximum value of 'Y'","Max value of 'Y'",JOptionPane.QUESTION_MESSAGE); int max_y = Integer.parseInt(input);
//ask the user if they want to see the data int answer = JOptionPane.showConfirmDialog(null,"Would you like to see the results?","User type", JOptionPane.YES_NO_OPTION);
//creates the arrays based on user input int[]x=new int[number]; int[]y=new int[number];
//loops to create a random number for each element of the x and y arrays
for (int i=0; i<number; i++){ //generate random number for X getx = (int)(Math.random()*max_x); x[i] = getx;
//generate random number for Y gety = (int)(Math.random()*max_y); y[i] = gety;
//shows the random points generated if the user selects "yes" option if (answer == JOptionPane.YES_OPTION) { mytextarea.append("\nPoint #" + (i+1) + " is at coordinates " + "(" + x[i] + "," + y[i] + ")"); }
//finds distances between points if (i > 0) { distance = Math.sqrt((Math.pow(x[i-1] - x[i], 2) + Math.pow(y[i-1] - y[i], 2)));
//String num = "0.00#"; //DecimalFormat df = new DecimalFormat(num); //String pdistance = df.format(distance);
//finds the sum of the distance between points totaldist = totaldist + distance;
//finds minimum and maximum distances between points if (distance < minimum) minimum = distance; if (distance > maximum) maximum = distance;
mytextarea.append("\nThe distance between point " + (i) + " and point " + (i+1) + " is " + distance + "\n"); }
if (i == (number-1)) { mytextarea.append("\nThe minimum distance was " + minimum); mytextarea.append("\nThe maximum distance was " + maximum); mytextarea.append("\nThe sum of the distances is " + totaldist + "\n"); }
//determines left most point if (x[i] < xleft) { xleft = x[i]; yleft = y[i]; }
//determines right most point if (x[i] > xright) { xright = x[i]; yright = y[i]; }
//determines top most point if (y[i] < ytop) { xtop = x[i]; ytop = y[i]; }
//determines bottom most point if (y[i] > ybot) { xbot = x[i]; ybot = y[i]; }
//displays text area if (i == (number-1)) { mytextarea.append("\nThe left most point is (" + xleft + "," + yleft + ")"); mytextarea.append("\nThe right most point is (" + xright + "," + yright + ")"); mytextarea.append("\nThe top most point is (" + xtop + "," + ytop + ")"); mytextarea.append("\nThe bottom most point is (" + xbot + "," + ybot + ")"); JOptionPane.showMessageDialog(null, scrollpane, "Random Points", JOptionPane.PLAIN_MESSAGE);
//Sets up graphics GIVES ILLEGAL START OF EXPRESSION ERROR!!! //public void paint (Graphics g) { //g.setColor(color.BLUE); //g.drawLine(xleft,yleft,xbot,ybot); //g.drawLine(xbot,ybot,xtop,ytop); //g.drawLine(xtop,ytop,xright,yright); //}
2. I followed the example on the link you sent me...
3. I verified the braces...
Unfortunatly, I am still getting illegall start of method, and the program blows up with alot of errors.... Does the paint method need to go towards the front of the program? Logic tells me no, since it is not supposed to paint until after the loop completes....
It has been too long since I messed with any graphics stuff.... I just want to get this thing working right...
I think I almost have it.... except now I get : NO SUCH METHOD MAIN...error. how do I get around this???? I would also like to get the decimel rounding function to work properly.... Thanks.
public class RandomPoints1A extends JApplet { //public static void main (String[] args){
//Sets up graphics GIVES ILLEGAL START OF EXPRESSION ERROR!!! public void paint (Graphics g) {
//variables int getx,gety,xleft,yleft,xright,yright,xtop,ytop,xbot,ybot; double distance, totaldist=0, minimum, maximum;
//creating the text area JTextArea mytextarea = new JTextArea(30,80); mytextarea.setLineWrap(true); mytextarea.setFont (new Font("Arial",Font.BOLD,18) ); JScrollPane scrollpane = new JScrollPane(mytextarea);
//get user input on number of points String input = JOptionPane.showInputDialog(null,"Please input the number of points you would like to display...","Enter a number",JOptionPane.QUESTION_MESSAGE); int number=Integer.parseInt(input);
//ask the user to determine the maximum size for x and y input = JOptionPane.showInputDialog(null,"What is the maximum value of 'X'","Max value of 'X'",JOptionPane.QUESTION_MESSAGE); int max_x = Integer.parseInt(input);
input = JOptionPane.showInputDialog(null,"What is the maximum value of 'Y'","Max value of 'Y'",JOptionPane.QUESTION_MESSAGE); int max_y = Integer.parseInt(input);
//ask the user if they want to see the data int answer = JOptionPane.showConfirmDialog(null,"Would you like to see the results?","User type", JOptionPane.YES_NO_OPTION);
//creates the arrays based on user input int[]x=new int[number]; int[]y=new int[number];
//loops to create a random number for each element of the x and y arrays
for (int i=0; i<number; i++){ //generate random number for X getx = (int)(Math.random()*max_x); x[i] = getx;
//generate random number for Y gety = (int)(Math.random()*max_y); y[i] = gety;
//shows the random points generated if the user selects "yes" option if (answer == JOptionPane.YES_OPTION) { mytextarea.append("\nPoint #" + (i+1) + " is at coordinates " + "(" + x[i] + "," + y[i] + ")"); }
//finds distances between points if (i > 0) { distance = Math.sqrt((Math.pow(x[i-1] - x[i], 2) + Math.pow(y[i-1] - y[i], 2)));
//String num = "0.00#"; //DecimalFormat df = new DecimalFormat(num); //String pdistance = df.format(distance);
//finds the sum of the distance between points totaldist = totaldist + distance;
//finds minimum and maximum distances between points if (distance < minimum) minimum = distance; if (distance > maximum) maximum = distance;
mytextarea.append("\nThe distance between point " + (i) + " and point " + (i+1) + " is " + distance + "\n"); }
if (i == (number-1)) { mytextarea.append("\nThe minimum distance was " + minimum); mytextarea.append("\nThe maximum distance was " + maximum); mytextarea.append("\nThe sum of the distances is " + totaldist + "\n"); }
//determines left most point if (x[i] < xleft) { xleft = x[i]; yleft = y[i]; }
//determines right most point if (x[i] > xright) { xright = x[i]; yright = y[i]; }
//determines top most point if (y[i] < ytop) { xtop = x[i]; ytop = y[i]; }
//determines bottom most point if (y[i] > ybot) { xbot = x[i]; ybot = y[i]; }
//displays text area if (i == (number-1)) { mytextarea.append("\nThe left most point is (" + xleft + "," + yleft + ")"); mytextarea.append("\nThe right most point is (" + xright + "," + yright + ")"); mytextarea.append("\nThe top most point is (" + xtop + "," + ytop + ")"); mytextarea.append("\nThe bottom most point is (" + xbot + "," + ybot + ")"); JOptionPane.showMessageDialog(null, scrollpane, "Random Points", JOptionPane.PLAIN_MESSAGE);
Why did you comment out the main method? Just move your other methods outside of the main method. The error you're getting now is because you're trying to run a java file that has no main method, which is illegal. So, just go back, uncomment the main method, and move all of your methods outside of the main method.
Why did you comment out the main method? Just move your other methods outside of the main method. The error you're getting now is because you're trying to run a java file that has no main method, which is illegal. So, just go back, uncomment the main method, and move all of your methods outside of the main method.
Did you get this to work??? Because apparently, I am missing something obvious... I tried the graphics method outside of the main, and I still get the same error... I don't understand why it keeps giving me the same error NO MATTER WHERE I put the graphics method.... I am at a complete loss.....
public class RandomPoints1A extends JApplet { public static void main (String[] args){
//variables int getx,gety,xleft,yleft,xright,yright,xtop,ytop,xbot,ybot; double distance, totaldist=0, minimum, maximum;
//creating the text area JTextArea mytextarea = new JTextArea(30,80); mytextarea.setLineWrap(true); mytextarea.setFont (new Font("Arial",Font.BOLD,18) ); JScrollPane scrollpane = new JScrollPane(mytextarea);
//get user input on number of points String input = JOptionPane.showInputDialog(null,"Please input the number of points you would like to display...","Enter a number",JOptionPane.QUESTION_MESSAGE); int number=Integer.parseInt(input);
//ask the user to determine the maximum size for x and y input = JOptionPane.showInputDialog(null,"What is the maximum value of 'X'","Max value of 'X'",JOptionPane.QUESTION_MESSAGE); int max_x = Integer.parseInt(input);
input = JOptionPane.showInputDialog(null,"What is the maximum value of 'Y'","Max value of 'Y'",JOptionPane.QUESTION_MESSAGE); int max_y = Integer.parseInt(input);
//ask the user if they want to see the data int answer = JOptionPane.showConfirmDialog(null,"Would you like to see the results?","User type", JOptionPane.YES_NO_OPTION);
//creates the arrays based on user input int[]x=new int[number]; int[]y=new int[number];
//loops to create a random number for each element of the x and y arrays
for (int i=0; i<number; i++){ //generate random number for X getx = (int)(Math.random()*max_x); x[i] = getx;
//generate random number for Y gety = (int)(Math.random()*max_y); y[i] = gety;
//shows the random points generated if the user selects "yes" option if (answer == JOptionPane.YES_OPTION) { mytextarea.append("\nPoint #" + (i+1) + " is at coordinates " + "(" + x[i] + "," + y[i] + ")"); }
//finds distances between points if (i > 0) { distance = Math.sqrt((Math.pow(x[i-1] - x[i], 2) + Math.pow(y[i-1] - y[i], 2)));
//String num = "0.00#"; //DecimalFormat df = new DecimalFormat(num); //String pdistance = df.format(distance);
//finds the sum of the distance between points totaldist = totaldist + distance;
//finds minimum and maximum distances between points if (distance < minimum) minimum = distance; if (distance > maximum) maximum = distance;
mytextarea.append("\nThe distance between point " + (i) + " and point " + (i+1) + " is " + distance + "\n"); }
if (i == (number-1)) { mytextarea.append("\nThe minimum distance was " + minimum); mytextarea.append("\nThe maximum distance was " + maximum); mytextarea.append("\nThe sum of the distances is " + totaldist + "\n"); }
//determines left most point if (x[i] < xleft) { xleft = x[i]; yleft = y[i]; }
//determines right most point if (x[i] > xright) { xright = x[i]; yright = y[i]; }
//determines top most point if (y[i] < ytop) { xtop = x[i]; ytop = y[i]; }
//determines bottom most point if (y[i] > ybot) { xbot = x[i]; ybot = y[i]; }
//displays text area if (i == (number-1)) { mytextarea.append("\nThe left most point is (" + xleft + "," + yleft + ")"); mytextarea.append("\nThe right most point is (" + xright + "," + yright + ")"); mytextarea.append("\nThe top most point is (" + xtop + "," + ytop + ")"); mytextarea.append("\nThe bottom most point is (" + xbot + "," + ybot + ")"); JOptionPane.showMessageDialog(null, scrollpane, "Random Points", JOptionPane.PLAIN_MESSAGE);
} } } //Sets up graphics public void paint (Graphics g) { g.setColor(color.BLUE); g.drawLine(xleft,yleft,xbot,ybot); g.drawLine(xbot,ybot,xtop,ytop); g.drawLine(xtop,ytop,xright,yright); }
}
If you compile this code it still doesn't work... It will wrok fine without the graphics though.... I am so frustrated that I can't see straight. I know there is someone out there that is going to look at this code and fix it so that it works with out making me feel like a complete idiot... As you can tell by now, I am not a super programmer, nor am I looking for easy answers... but at this point, I am lost... I simply want to know what is wrong and how to fix it, so that I can learn from it. I want to learn! I just don't know where to go from here...
Cobrec
This post has been edited by cobrec: 11 Mar, 2007 - 03:14 PM
This first problem is a typo, color should have a capital C Color.BLUE.
The other problems are because you are using variables inside the paint method that you haven't declared. Since you need to use the variable inside main as well as inside the paint method. You should declare them as global variables, so all your methods can use them.
CODE
public class RandomPoints1A extends JApplet {
private static int getx,gety,xleft,yleft,xright,yright,xtop,ytop,xbot,ybot;
public static void main (String[] args){
//variables
double distance, totaldist=0, minimum, maximum;
See how I moved that variable declaration above main, I made it private so only your application has access to them. I also made them static since they need to be used in static methods.
Okay, at least it compiles... that is a start. Thanks for helping me figure out WTF was going on with the program. Only one problem, it doesn't draw anything when I execute the file.... do I need to put the graphics before the text box???? and did you look at the portion on how to fix the decimel? those are the two problems that I have with my code...
//variables private static int getx,gety,xleft,yleft,xright,yright,xtop,ytop,xbot,ybot; public static void main (String[] args){ double distance, totaldist=0, minimum, maximum;
//creates the text area JTextArea mytextarea = new JTextArea(30,80); mytextarea.setLineWrap(true); mytextarea.setFont (new Font("Arial",Font.BOLD,18) ); JScrollPane scrollpane = new JScrollPane(mytextarea);
//get user input on number of points String input = JOptionPane.showInputDialog(null,"Please input the number of points you would like to display...","Enter a number",JOptionPane.QUESTION_MESSAGE); int number=Integer.parseInt(input);
//ask the user to determine the maximum size for x and y input = JOptionPane.showInputDialog(null,"What is the maximum value of 'X'","Max value of 'X'",JOptionPane.QUESTION_MESSAGE); int max_x = Integer.parseInt(input);
input = JOptionPane.showInputDialog(null,"What is the maximum value of 'Y'","Max value of 'Y'",JOptionPane.QUESTION_MESSAGE); int max_y = Integer.parseInt(input);
//ask the user if they want to see the data int answer = JOptionPane.showConfirmDialog(null,"Would you like to see the results?","User type", JOptionPane.YES_NO_OPTION);
//creates the arrays based on user input int[]x=new int[number]; int[]y=new int[number];
//loops to create a random number for each element of the x and y arrays for (int i=0; i<number; i++){
//generate random number for X getx = (int)(Math.random()*max_x); x[i] = getx;
//generate random number for Y gety = (int)(Math.random()*max_y); y[i] = gety;
//shows the random points generated if the user selects "yes" option if (answer == JOptionPane.YES_OPTION) { mytextarea.append("\nPoint #" + (i+1) + " is at coordinates " + "(" + x[i] + "," + y[i] + ")"); }
//finds distances between points if (i > 0) { distance = Math.sqrt((Math.pow(x[i-1] - x[i], 2) + Math.pow(y[i-1] - y[i], 2)));
//String num = "0.00#"; //DecimalFormat df = new DecimalFormat(num); //String pdistance = df.format(distance);
//finds the sum of the distance between points totaldist = totaldist + distance;
//finds minimum and maximum distances between points if (distance < minimum) minimum = distance; if (distance > maximum) maximum = distance;
mytextarea.append("\nThe distance between point " + (i) + " and point " + (i+1) + " is " + distance + "\n"); }
if (i == (number-1)) { mytextarea.append("\nThe minimum distance was " + minimum); mytextarea.append("\nThe maximum distance was " + maximum); mytextarea.append("\nThe sum of the distances is " + totaldist + "\n"); }
//determines left most point if (x[i] < xleft) { xleft = x[i]; yleft = y[i]; }
//determines right most point if (x[i] > xright) { xright = x[i]; yright = y[i]; }
//determines top most point if (y[i] < ytop) { xtop = x[i]; ytop = y[i]; }
//determines bottom most point if (y[i] > ybot) { xbot = x[i]; ybot = y[i]; }
//displays text area if (i == (number-1)) { mytextarea.append("\nThe left most point is (" + xleft + "," + yleft + ")"); mytextarea.append("\nThe right most point is (" + xright + "," + yright + ")"); mytextarea.append("\nThe top most point is (" + xtop + "," + ytop + ")"); mytextarea.append("\nThe bottom most point is (" + xbot + "," + ybot + ")"); JOptionPane.showMessageDialog(null, scrollpane, "Random Points", JOptionPane.PLAIN_MESSAGE); } } } //Sets up graphics public void paint (Graphics g) { super.paint(g); g.setColor(Color.BLUE); g.drawLine(xleft,yleft,xbot,ybot); g.drawLine(xbot,ybot,xtop,ytop); g.drawLine(xtop,ytop,xright,yright); } }
public class RandomPoints1A extends JFrame { JPanel canvas = new JPanel(); Container pane; //variables private static int getx,gety,xleft,yleft,xright,yright,xtop,ytop,xbot,ybot;
public static void main (String[] args){ double distance, totaldist=0, minimum, maximum;
//creates the text area JTextArea mytextarea = new JTextArea(30,80); mytextarea.setLineWrap(true); mytextarea.setFont (new Font("Arial",Font.BOLD,18) ); JScrollPane scrollpane = new JScrollPane(mytextarea);
//get user input on number of points String input = JOptionPane.showInputDialog(null,"Please input the number of points you would like to display...","Enter a number",JOptionPane.QUESTION_MESSAGE); int number=Integer.parseInt(input);
//ask the user to determine the maximum size for x and y input = JOptionPane.showInputDialog(null,"What is the maximum value of 'X'","Max value of 'X'",JOptionPane.QUESTION_MESSAGE); int max_x = Integer.parseInt(input);
input = JOptionPane.showInputDialog(null,"What is the maximum value of 'Y'","Max value of 'Y'",JOptionPane.QUESTION_MESSAGE); int max_y = Integer.parseInt(input);
//ask the user if they want to see the data int answer = JOptionPane.showConfirmDialog(null,"Would you like to see the results?","User type", JOptionPane.YES_NO_OPTION);
//creates the arrays based on user input int[]x=new int[number]; int[]y=new int[number];
//loops to create a random number for each element of the x and y arrays for (int i=0; i<number; i++){
//generate random number for X getx = (int)(Math.random()*max_x); x[i] = getx;
//generate random number for Y gety = (int)(Math.random()*max_y); y[i] = gety;
//shows the random points generated if the user selects "yes" option if (answer == JOptionPane.YES_OPTION) { mytextarea.append("\nPoint #" + (i+1) + " is at coordinates " + "(" + x[i] + "," + y[i] + ")"); }
//finds distances between points if (i > 0) { distance = Math.sqrt(&