help using graphics class

illegal start of expression error

Page 1 of 1

13 Replies - 2264 Views - Last Post: 26 March 2007 - 01:37 AM Rate Topic: -----

#1 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

help using graphics class

Posted 10 March 2007 - 05:21 AM

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.

Cobrec


import javax.swing.*;
import java.awt.*;
import java.applet.*;
//import java.text.*;

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];
	
	xleft=max_x;
	yleft=0;
	xright=0;
	yright=0;
	ytop=max_y;
	xtop=0;
	xbot=0;
	ybot=0;
	
	minimum=(max_x+max_y);
	maximum =0;
	
//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);
	//}
						
}
	}
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: help using graphics class

#2 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: help using graphics class

Posted 10 March 2007 - 07:45 AM

First - go through and check your braces and make sure they all have matching closing braces.

Second - you can't declare a method inside of another method. In this case you're declaring paint inside of main.

Third - Inside paint, the first thing you should do is call super.paint(g);
http://java.sun.com/...es/painting/#lw

This post has been edited by alcdotcom: 10 March 2007 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 10 March 2007 - 07:04 PM

View Postalcdotcom, on 10 Mar, 2007 - 07:45 AM, said:

First - go through and check your braces and make sure they all have matching closing braces.

Second - you can't declare a method inside of another method. In this case you're declaring paint inside of main.

Third - Inside paint, the first thing you should do is call super.paint(g);
http://java.sun.com/...es/painting/#lw


Ok,

1. I remarked out the main method...

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...
Was This Post Helpful? 0
  • +
  • -

#4 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 10 March 2007 - 07:16 PM

Okay,

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.

Cobrec


import javax.swing.*;
import java.awt.*;
import java.applet.*;
//import java.text.*;

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];
	
	xleft=max_x;
	yleft=0;
	xright=0;
	yright=0;
	ytop=max_y;
	xtop=0;
	xbot=0;
	ybot=0;
	
	minimum=(max_x+max_y);
	maximum =0;
	
//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);
			
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);
	}

						
}

	}
	
	}

Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: help using graphics class

Posted 11 March 2007 - 08:36 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 11 March 2007 - 02:12 PM

View Postalcdotcom, on 11 Mar, 2007 - 08:36 AM, said:

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.....
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: help using graphics class

Posted 11 March 2007 - 02:55 PM

Post the code you have with all of your methods outside of main. We can't help you if you don't show us your most current code.
Was This Post Helpful? 0
  • +
  • -

#8 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 11 March 2007 - 04:12 PM

View Postjayman9, on 11 Mar, 2007 - 02:55 PM, said:

Post the code you have with all of your methods outside of main. We can't help you if you don't show us your most current code.


Ok... I posted the graphics method after the end } for the main method as seen here....

I just don't know where else this would go....

import javax.swing.*;
import java.awt.*;
import java.applet.*;
//import java.text.*;

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];
	
	xleft=max_x;
	yleft=0;
	xright=0;
	yright=0;
	ytop=max_y;
	xtop=0;
	xbot=0;
	ybot=0;
	
	minimum=(max_x+max_y);
	maximum =0;
	
//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 March 2007 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: help using graphics class

Posted 11 March 2007 - 05:10 PM

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

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.

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.

Make those two changes it works great. Good job!! :)
Was This Post Helpful? 0
  • +
  • -

#10 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 12 March 2007 - 12:40 AM

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...

import javax.swing.*;
import java.awt.*;
import java.applet.*;
//import java.text.*;

public class RandomPoints1A extends JApplet {

//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];
	
	xleft=max_x;
	yleft=0;
	xright=0;
	yright=0;
	ytop=max_y;
	xtop=0;
	xbot=0;
	ybot=0;
	
	minimum=(max_x+max_y);
	maximum =0;
	
//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);
	}
}


Did I misinterpret your instructions?????


Cobrec
Was This Post Helpful? 0
  • +
  • -

#11 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: help using graphics class

Posted 18 March 2007 - 11:46 AM

try this
import javax.swing.*;
import java.awt.*;
import java.applet.*;
//import java.text.*;

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 RandomPoints1A()
{
super("random points");
setSize(350, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = getContentPane();
pane.add(canvas);
canvas.validate();
canvas.repaint();
setVisible(true);

//setContentPane(pane);
}

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];
	
	xleft=max_x;
	yleft=0;
	xright=0;
	yright=0;
	ytop=max_y;
	xtop=0;
	xbot=0;
	ybot=0;
	
	minimum=(max_x+max_y);
	maximum =0;
	
//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 + ")");
  // RandomPoints1A r=new RandomPoints1A();

			JOptionPane.showMessageDialog(null, scrollpane, "Random Points", JOptionPane.PLAIN_MESSAGE);					

   RandomPoints1A r=new RandomPoints1A();
}
	}
	}
//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);
	}
}

Was This Post Helpful? 0
  • +
  • -

#12 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 25 March 2007 - 01:59 PM

View Posthorace, on 18 Mar, 2007 - 11:46 AM, said:

try this
import javax.swing.*;
import java.awt.*;
import java.applet.*;
//import java.text.*;

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 RandomPoints1A()
{
super("random points");
setSize(350, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = getContentPane();
pane.add(canvas);
canvas.validate();
canvas.repaint();
setVisible(true);

//setContentPane(pane);
}

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];
	
	xleft=max_x;
	yleft=0;
	xright=0;
	yright=0;
	ytop=max_y;
	xtop=0;
	xbot=0;
	ybot=0;
	
	minimum=(max_x+max_y);
	maximum =0;
	
//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 + ")");
  // RandomPoints1A r=new RandomPoints1A();

			JOptionPane.showMessageDialog(null, scrollpane, "Random Points", JOptionPane.PLAIN_MESSAGE);					

   RandomPoints1A r=new RandomPoints1A();
}
	}
	}
//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);
	}
}


It compiled, but there is no graphics output....


Cobrec
Was This Post Helpful? 0
  • +
  • -

#13 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: help using graphics class

Posted 25 March 2007 - 10:56 PM

if I enter 10, 200, 200, click OK and click OK (to close display of data) I then get a window showing some graphics
Was This Post Helpful? 0
  • +
  • -

#14 cobrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-March 07

Re: help using graphics class

Posted 26 March 2007 - 01:37 AM

Apparently,

the other version was somehow cached... I cleared out the cache and it works fine... My bad. thanks for the help.

Cobrec
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1