9 Replies - 452 Views - Last Post: 14 October 2012 - 07:49 PM Rate Topic: -----

#1 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Inner Class question

Posted 14 October 2012 - 11:45 AM

Sorry to be bothering again but I have a small question about inner classes.

If I have the code essentially looking like this:
public class TestTriangleType {
public static void GenerateRandomTriangles(int numTriangles){
        //code
    }
    public String TriangleType(Point a, Point b, Point c){
        //code
    }
    private void TestTriangle(){
        //code
    }
public class BasicGraphics extends JFrame{
    public BasicGraphics(){
        //code    
    }
    class WindowCloser extends WindowAdapter{
        //code
    }
    private void Run(){
        //code
    }
    private void DrawFixedObjects(){
        //code
    }
    private void DrawTriangle(){
        //code
    }
    public void paint(Graphics g){
        //code
    }
}
public static void main(String[] args) {
            TestTriangleType.GenerateRandomTriangles(1);
            TestTriangleType test = new TestTriangleType();
            test.TestTriangle();
            TestTriangleType.BasicGraphics bg = new TestTriangleType().new BasicGraphics();
            bg.BasicGraphics();
}



My error comes from the bg.BasicGraphics
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: testtriangletype.TestTriangleType.BasicGraphics.BasicGraphics
at testtriangletype.TestTriangleType.main(TestTriangleType.java:137)

I've have done some reading up on how its supposed to work but I'm still slightly confused as to how to properly use the inner class in the main method, anyone able to help me with this?

Is This A Good Question/Topic? 0
  • +

Replies To: Inner Class question

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Inner Class question

Posted 14 October 2012 - 12:17 PM

Your main method is declared outside of your class, you need to place it inside your class TestTriangleType
Was This Post Helpful? 1
  • +
  • -

#3 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Inner Class question

Posted 14 October 2012 - 01:20 PM

I did that yet I still get the same error.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Inner Class question

Posted 14 October 2012 - 01:21 PM

Post your updated code. We really don't know what you did without it.
Was This Post Helpful? 0
  • +
  • -

#5 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Inner Class question

Posted 14 October 2012 - 01:25 PM

This is the code in its entirety
package testtriangletype;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class TestTriangleType {
    private static int x1, y1, x2, y2, x3, y3, input;
    private static String str;
    private static double sideA, sideB, sideC;
    private Image fImageBuffer;
    private Insets fInsets;
    private Graphics g;
    private static final int WIDTH = 400, HEIGHT = 400, OUTER_MARGIN = 30;
    private static final Color BACKGROUND_COLOR = Color.white, LINE_COLOR = Color.black, TRIANGLE_COLOR = Color.blue;
    private class Point{
        int x, y;
        public Point(int x, int y){
            this.x = x; this.y = y; 
        }
        public void Print(){ 
            System.out.println(toString()); 
        }
        public String toString(){ 
            return("(" + x + ", " + y + ")"); 
        }
    }
    public static void GenerateRandomTriangles(int numTriangles){
        try{
            Random randInt = new Random(0);
            PrintWriter outFile = new PrintWriter("RandomTriangles.dat");
            outFile.println("Prints the (x, y) points of the vertices of a triangle.");
            x1 = randInt.nextInt(150);
            y1 = randInt.nextInt(150);
            do{ x2 = randInt.nextInt(150);
                y2 = randInt.nextInt(150);
                } while ((x1 == x2) && (y1 == y2));
            if (x1 == x2){
                do{ x3 = randInt.nextInt(150);
                } while (x3 == x1);
            y3 = randInt.nextInt(150);
            }
            else if (y1 == y2){
                do{ y3 = randInt.nextInt(150);
                } while (y3 == y1);
            x3 = randInt.nextInt(150);
            }
            else{
                x3 = randInt.nextInt(150);
                y3 = randInt.nextInt(150);
            }
            outFile.printf("(%3d, %3d) (%3d, %3d) (%3d, %3d) %n", x1, y1, x2, y2, x3, y3);
            outFile.close();
            }
            catch(FileNotFoundException e){
                System.out.println(e);
                System.exit(3);
            }
    }
    public String TriangleType(Point a, Point b, Point c){
        if ((a == B)/> && (b == c)){
            return "Triangle is Equilateral.";
        }
        else if (((a == B)/> || (b == c)) && (a != c)){
            return "Triangle is Isosceles.";
        }
        else{ return "Triangle is Scalar."; }
    }
    private void TestTriangle(){
        try{
            Scanner scan = new Scanner(new File("RandomTriangles.dat")).useDelimiter("( , )");
            scan.nextLine();
            while(scan.hasNext()){
                str = scan.next();
                x1 = Integer.parseInt(str.substring(1, 4).trim()); y1 = Integer.parseInt(str.substring(6, 9).trim());
                x2 = Integer.parseInt(str.substring(12, 15).trim()); y2 = Integer.parseInt(str.substring(17, 20).trim());
                x3 = Integer.parseInt(str.substring(23, 26).trim()); y3 = Integer.parseInt(str.substring(28, 31).trim());
                Point a = new Point(x1, y1); Point b = new Point(x2, y2); Point c = new Point(x3, y3);
                sideA = (y2 - y1) / (x3 - x2); sideB = (y3 - y2) / (x2 - x1); sideC = (y3 - y1) / (x3 - x1);
                System.out.println("Point a: " + a + " Point b: " + b + " Point c: " + c + " " +TriangleType(a, b, c));
            }
        }
        catch(FileNotFoundException e){
                System.out.println(e);
                System.exit(3);
        }
    }
    public static void main(String[] args) {
        Scanner keyb = new Scanner(System.in);
        System.out.print("Input 1 to run the program: ");
        input = keyb.nextInt();
        if (1 == input){
            TestTriangleType.GenerateRandomTriangles(1);
            TestTriangleType test = new TestTriangleType();
            test.TestTriangle();
            TestTriangleType.BasicGraphics bg = new TestTriangleType().new BasicGraphics();
            bg.BasicGraphics();
        }
        else
            System.out.println("Invalid input.");
            System.exit(0);
    }
    public class BasicGraphics extends JFrame{
        public BasicGraphics(){
            addWindowListener(new WindowCloser());
            setVisible(true);
            fInsets = getInsets();
            setSize(WIDTH + fInsets.left + fInsets.right, HEIGHT + fInsets.top + fInsets.bottom);
            setTitle("Triangle Graphics");
            setResizable(false);
            if (((fImageBuffer = createImage(WIDTH, HEIGHT)) != null) && ((g = fImageBuffer.getGraphics()) != null)){
                Run();
            }
            else { System.exit(1); }
        }
        class WindowCloser extends WindowAdapter{
            public void windowClosing(WindowEvent e){ System.exit(0); }
        }
        private void Run(){
            DrawFixedObjects();
            DrawTriangle();
        }
        private void DrawFixedObjects(){
            g.clearRect(0, 0, WIDTH, HEIGHT);
            g.setColor(BACKGROUND_COLOR);
            g.fillRect(0, 0, WIDTH, HEIGHT);
            g.setColor(LINE_COLOR);
            repaint();
        }
        private void DrawTriangle(){
            g.setColor(TRIANGLE_COLOR);
            int[] xs = { x1, x2, x3 }, ys = { y1, y2, y3 };
            g.fillPolygon(xs, ys, xs.length);
            g.setColor(Color.blue);
            g.drawPolygon(xs, ys, xs.length);
            repaint();
        }
        public void paint(Graphics g){
            if (fImageBuffer != null){ g.drawImage(fImageBuffer, fInsets.left, fInsets.top, null); }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Inner Class question

Posted 14 October 2012 - 01:40 PM

The statement

bg.BasicGraphics();

is calling the method BasciGraphics() in the class BasicGraphics. In fact, you might be trying to call the BasicGraphics constructor as though it were a regular method, but it's not clear why, and it's an error.

What are you trying to do?
Was This Post Helpful? 0
  • +
  • -

#7 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Inner Class question

Posted 14 October 2012 - 01:44 PM

Im trying to get it to display a triangle so to my understanding it would just need to call the method BasicGraphics() or am I looking at it wrong?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Inner Class question

Posted 14 October 2012 - 02:06 PM

Probably.

The statement new BasicGraphics() does pretty much everything the BasicGraphics class does, but that still won't get you the output you're looking for.

If you remove the System.exit( 0 ) at line 102, you'll at least get a JFrame to play with. Getting more out of it will take more work.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#9 rwhitney92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-October 12

Re: Inner Class question

Posted 14 October 2012 - 02:08 PM

alright ill try working with it some more. thanks for the help
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Inner Class question

Posted 14 October 2012 - 07:49 PM

View PostCasiOo, on 14 October 2012 - 03:17 PM, said:

Your main method is declared outside of your class, you need to place it inside your class TestTriangleType

really not a good idea to include multiple class in a single .java
the only reason to use that is if you want to hide the class definition to the rest of the world
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1