Welcome to Dream.In.Code
Become a Java Expert!

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!




help using graphics class

2 Pages V  1 2 >  
Reply to this topicStart new topic

help using graphics class, illegal start of expression error

cobrec
10 Mar, 2007 - 04:21 AM
Post #1

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


My Contributions
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


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

}

User is offlineProfile CardPM
+Quote Post

Programmist
RE: Help Using Graphics Class
10 Mar, 2007 - 06:45 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
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/products/jfc/tsc/articles/painting/#lw

This post has been edited by alcdotcom: 10 Mar, 2007 - 06:46 AM
User is offlineProfile CardPM
+Quote Post

cobrec
RE: Help Using Graphics Class
10 Mar, 2007 - 06:04 PM
Post #3

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


My Contributions
QUOTE(alcdotcom @ 10 Mar, 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/products/jfc/tsc/articles/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...


User is offlineProfile CardPM
+Quote Post

cobrec
RE: Help Using Graphics Class
10 Mar, 2007 - 06:16 PM
Post #4

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


My Contributions
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


CODE

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

                        
}

    }
    
    }

User is offlineProfile CardPM
+Quote Post

Programmist
RE: Help Using Graphics Class
11 Mar, 2007 - 07:36 AM
Post #5

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

cobrec
RE: Help Using Graphics Class
11 Mar, 2007 - 01:12 PM
Post #6

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


My Contributions
QUOTE(alcdotcom @ 11 Mar, 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.



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



User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help Using Graphics Class
11 Mar, 2007 - 01:55 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

cobrec
RE: Help Using Graphics Class
11 Mar, 2007 - 03:12 PM
Post #8

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


My Contributions
QUOTE(jayman9 @ 11 Mar, 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.


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

CODE

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 Mar, 2007 - 03:14 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help Using Graphics Class
11 Mar, 2007 - 04:10 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
CODE

    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.

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.

Make those two changes it works great. Good job!! smile.gif
User is offlineProfile CardPM
+Quote Post

cobrec
RE: Help Using Graphics Class
11 Mar, 2007 - 11:40 PM
Post #10

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


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

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
User is offlineProfile CardPM
+Quote Post

horace
RE: Help Using Graphics Class
18 Mar, 2007 - 10:46 AM
Post #11

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
try this
CODE

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(&