hi
i am trying to make a CAD program that read a text file which contain a vertecises and number of triangles, which look something like this"the file" :

8
-2.0 -1.5 -0.5
2.0 -1.5 -0.5
-2.0 1.5 -0.5
2.0 1.5 -0.5
-2.0 -1.5 0.5
2.0 -1.5 0.5
-2.0 1.5 0.5
2.0 1.5 0.5
12
1 3 2
2 3 4
5 6 7
6 8 7
1 2 5
2 6 5
3 7 4
4 7 8
2 4 6
4 8 6
1 5 3
3 5 7

which should make a cuboid if we used the file above.

i have made the GUI part but the inner function is some how jammed.

this is my code "its a bit missed up need to make it work first before cleaning it "

import java.awt.*;      
import java.awt.geom.*; 
import java.awt.event.*;
import javax.swing.*;   
import javax.swing.border.TitledBorder;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import javax.swing.BoxLayout;
import java.lang.Object;
import java.awt.geom.Point2D;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Polygon;











public class SimpleCAD extends JFrame implements ActionListener, ChangeListener {
	public static int size = 500;
	JPanel sliderPanel, buttonPanel;
	JButton button1, button2;
	JSlider slider1, slider2, slider3;
	JMenuItem openItem, quitItem;
	Image bufferImage;
	Graphics bufferGraphics;
	   File myFile = null;
	    String line;
	    int numVerticies, numTriangles, s = 0;
	    int vertex1[] , vertex2[] , vertex3[] , scalef =300;
	    int Xss[][] , Yss[][] ;
	    
	
	DisplayPanel graphPanel;
	
	private DisplayPanel displayPanel;
	 public Point3D points[];
	 public Triangle triangles[]; 

    
	public void actionPerformed(ActionEvent event) {
	// Code to respond to buttons and menu items
 
   
    
    	// myFile is a Java File object which you can get from the file chooser
    	JComponent source = (JComponent)event.getSource();
    

    	if (source == openItem) {

    	   
            JFileChooser chooser = new JFileChooser();
            int retVal = chooser.showOpenDialog(this);
            
            if (retVal == JFileChooser.APPROVE_OPTION) {
    		myFile = chooser.getSelectedFile() ;
    		// myFile is a Java File object which you can get from the file chooser
    		try {
    		    Scanner scanner = new Scanner(myFile);
    		    
    		    // First line has the number of verticies
    		    line = scanner.nextLine();
    		    numVerticies = Integer.parseInt(line);

    		    // Code to initialize any arrays you use to store the coordinates
    		    points = new Point3D[numVerticies];
    		    // Read all vertex coordinates
    		    for (int n = 0; n < numVerticies; ++n) {
    			line = scanner.nextLine();

    			// Break the line into space-delimited parts
    			StringTokenizer strtok = new StringTokenizer(line, " \t");
    			double x = Double.parseDouble( strtok.nextToken() );
    			double y = Double.parseDouble( strtok.nextToken() );
    			double z = Double.parseDouble( strtok.nextToken() );
    			points[n] = new Point3D( x, y, z );
    			// Do something with x, y, z
    			
    			
    		    }
    		    
    		    // Get the number of triangles
    		    line = scanner.nextLine();
    		    numTriangles = Integer.parseInt(line);

    		    // Note triangles are numbered from 1 in the files, but we want
    		    // them to start at 0. vertex1, vertex2, vertex3 are integer
    		    // arrays
    		    triangles = new Triangle[numTriangles];
    		    vertex1 = new int[numTriangles];
    		    vertex2 = new int[numTriangles];
    		    vertex3 = new int[numTriangles];
    		    for (int n = 0; n < numTriangles; ++n) {
    			line = scanner.nextLine();
    			StringTokenizer strtok = new StringTokenizer(line, " \t");
    			vertex1[n] = Integer.parseInt( strtok.nextToken() ) - 1;
    			vertex2[n] = Integer.parseInt( strtok.nextToken() ) - 1;
    			vertex3[n] = Integer.parseInt( strtok.nextToken() ) - 1;
    			triangles[n] = new Triangle(vertex1[n], vertex2[n], vertex3[n]);
    			
    		    }
    		    int jo= 0, yo=0;
    			for (int i=0 ; i<=numTriangles ; i++){
    				Xss [jo][i] = (int) points[ triangles[i].i1 ].x;
    				Xss [jo][i] = (int) points[ triangles[i].i2 ].x;
    			    Xss [jo][i] = (int) points[ triangles[i].i3 ].x;
    			    jo++;
    			}
    			for (int i=0 ; i<=numTriangles ; i++){
    				Yss [yo][i] = (int) points[ triangles[i].i1 ].y;
    				Yss [yo][i] = (int) points[ triangles[i].i2 ].y;
    			    Yss [yo][i] = (int) points[ triangles[i].i3 ].y;
    			    yo++;
    			}
    
    		}
    		// Scanner requires Exception be caught
    		catch (Exception e) {
    		    e.printStackTrace();
    		}
    		

            } 
    	 
    		  
    	  
         }
    	
      }
    	
    
    
 




	public class Triangle{
    	int i1, i2, i3;
    	Triangle( int j1, int j2, int j3 ){ 
    		i1=j1; i2=j2; i3=j3; 
    		}
        }
  
    
    public class Point2D{
    	public int x;
    	public int y;
    	Point2D(){ x=0; y=0; }
    	Point2D( int x, int y ){
    	    this.x= x;
    	    this.y= y;
    	}
    }
    
    public class Point3D{
	public double x;
	public double y;
	public double z;
	Point3D(){ 
		x=0.0; y=0.0; z=0.0; 
		}
	Point3D( double x, double y, double z ){ 
		this.x= x; this.y= y; this.z= z; 
		}
	Point3D( int x, int y, int z ){ 
		this.x= x; this.y= y; this.z= z; 
		}

	public Point2D projectPoint(){
	    Point2D retval = new Point2D();
	    double tmpx = screenPosition.x + x * cosTheta - y * sinTheta;
	    double tmpy = screenPosition.y + x * sinTheta + y * cosTheta * sinPhi
		                           + z * cosPhi ;
	    double temp = viewAngle.z / (screenPosition.z
					 + x * sinTheta * cosPhi
					 + y * cosTheta * cosPhi - z * sinPhi  );

	    retval.x = xScreenOrigin + (int)(modelScale * temp * tmpx );
	    retval.y = yScreenOrigin + (int)(modelScale * temp * tmpy );
	    return retval;
	  }
    }
    
    
    
    private static final double DEGREES_TO_RADIANS = Math.PI / 180.0;

    public int xScreenOrigin = 0;
    public int yScreenOrigin = 0;

    public Point3D screenPosition = new Point3D( 0, 0, 20 );
    public Point3D viewAngle = new Point3D( 0, 0, 180 );

    private double cosTheta = Math.cos( DEGREES_TO_RADIANS * viewAngle.x );
    private double sinTheta = Math.sin( DEGREES_TO_RADIANS * viewAngle.x );
    private double cosPhi   = Math.cos( DEGREES_TO_RADIANS * viewAngle.y );
    private double sinPhi   = Math.sin( DEGREES_TO_RADIANS * viewAngle.y );
    
    private double modelScale = 10.0;

    public void updateAngles(){
	cosTheta = Math.cos( DEGREES_TO_RADIANS * viewAngle.x );
	sinTheta = Math.sin( DEGREES_TO_RADIANS * viewAngle.x );
	cosPhi   = Math.cos( DEGREES_TO_RADIANS * viewAngle.y );
	sinPhi   = Math.sin( DEGREES_TO_RADIANS * viewAngle.y );
    }



	public void stateChanged(ChangeEvent e) {
	// Code to respond to the sliders
    }

    public SimpleCAD() {
    	// Code to set up your JFrame - add menus, buttons, and
    	// JPanels etc
    	
	super("My Simple CAD");
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setSize(1000, 700);
	
	
    slider1 = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
    slider1.setMajorTickSpacing(90);
    slider1.setMinorTickSpacing(30);
    slider1.setPaintTicks(true);
    slider1.setPaintLabels(true);
    slider1.addChangeListener( this ); 
    
    
    slider2 = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
    slider2.setMajorTickSpacing(90);
    slider2.setMinorTickSpacing(30);
    slider2.setPaintTicks(true);
    slider2.setPaintLabels(true);
    slider2.addChangeListener( this ); 
    
    slider3 = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
    slider3.setMajorTickSpacing(90);
    slider3.setMinorTickSpacing(30);
    slider3.setPaintTicks(true);
    slider3.setPaintLabels(true);
    slider3.addChangeListener(this);
    
    
    
    sliderPanel = new JPanel();
    sliderPanel.add(slider1);
    sliderPanel.add(slider2);
    sliderPanel.add(slider3);
    
    slider1.setBorder(new TitledBorder("XY Plane"));
    slider2.setBorder(new TitledBorder("YX Plane"));
    slider3.setBorder(new TitledBorder("XZ Plane"));
    
    
    
    buttonPanel = new JPanel();
    button1 = new JButton ("Bigger");
    button2 = new JButton ("Smaller");
    button1.addActionListener( this ); 
    button2.addActionListener( this ); 
    // need to register an action listener here 
    buttonPanel.add(button1);
    buttonPanel.add(button2);
    


    add(buttonPanel, BorderLayout.PAGE_END);
    add(sliderPanel, BorderLayout.PAGE_START);
    
    
	JMenuBar menuBar = new JMenuBar();
	this.setJMenuBar(menuBar);

	JMenu fileMenu = new JMenu("File");
	menuBar.add(fileMenu);


	openItem = new JMenuItem("Open");
	openItem.addActionListener(this);
	fileMenu.add(openItem);

	
	quitItem = new JMenuItem("Quit");
	quitItem.addActionListener(this);
	fileMenu.add(quitItem);
	
	
	displayPanel = new DisplayPanel();
	add(displayPanel, BorderLayout.CENTER);
	
	
	this.setVisible(true);

    }

    // Program entry point
    public static void main(String[] args) {
	new SimpleCAD();
	
    }

    class DisplayPanel extends JPanel {
    	DisplayPanel methode;
    	

	public DisplayPanel() {
		getContentPane().setBackground(Color.white);
	}

	public void paintComponent( Graphics g ){
	    // Code to draw the transformed triangles
	
		// xdraw and ydraw are size 3 arrays giving the triangle vertex positions
		
		 Graphics2D g2 = (Graphics2D)g;
		    g2.setColor( Color.lightGray );
		    g2.fillRect( 0, 0, getWidth(), getHeight() );
		    g2.setColor( Color.black );
		    g2.translate( getWidth()/2, getHeight()/2 );   // make 0,0 the screen centre
		    g2.scale( 1.0, -1.0 );  // make the  x,y the normal right-handed Cartesian way
		    
		super.paintComponent(g);
		
		
		drawTriangles(g2 );
		
		
	}
		
	  }
    public void paintComponent( Graphics g ){
	    Graphics2D g2 = (Graphics2D)g;
	    g2.setColor( Color.red );

	    
	}
    public void drawTriangles( Graphics2D g2 ){
		
		
		for( int i = 0;i< numTriangles;i++){
		    try{
		    	
		    	Polygon a1Triangle = new Polygon(Xss[i] , Yss[i], 3 );
		    	
		    	g2.setColor(Color.gray);
				g2.fillPolygon(a1Triangle); // Colour in the triangle surface
				g2.setColor(Color.black);
				g2.drawPolygon(a1Triangle);
		    }catch( ArrayIndexOutOfBoundsException e ){
			System.out.println( "out of bounds i = " + i );
		    }
		}
	}
    }

sorry but i am really new to java, i don't know whats wrong with my code?

the inner function is some how jammed.

Can you explain the problem?
Do you get errors? If so please copy and paste them here.
Is the output incorrect? If so, please copy and paste it here and show what it should be.

i don't know whats wrong with my code?

Can you explain what the problem is?

Can you explain the problem?
Do you get errors? If so please copy and paste them here.
Is the output incorrect? If so, please copy and paste it here and show what it should be.

Can you explain what the problem is?

the GUI work fine but once i click open then choose a file its just close the filechooser window and get all these errors

java.lang.NullPointerException
at SimpleCAD.actionPerformed(SimpleCAD.java:116)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

java.lang.NullPointerException
at SimpleCAD.actionPerformed(SimpleCAD.java:116)

There is a null variable at line 116 in your program. Look at that line of code and see what variable is null and then backtrack to see why it is null and change the code so the variable has a valid value.

There is a null variable at line 116 in your program. Look at that line of code and see what variable is null and then backtrack to see why it is null and change the code so the variable has a valid value.

thanks a lot i tracked it down it was in this part

int jo= 0, yo=0;
    		    
    		    Xss = new int[numTriangles][3];
    		    Yss = new int[numTriangles][3];
    		    while(jo<numTriangles){
    			for (int i=0 ; i<3 ; i++){
    				Xss [jo][i] = (int) points[ triangles[i].i1 ].x;
    				Xss [jo][i] = (int) points[ triangles[i].i2 ].x;
    			    Xss [jo][i] = (int) points[ triangles[i].i3 ].x;
    			    jo++;
    			 }
    		    }
    		    while(yo<numTriangles){
    			for (int i=0 ; i<3 ; i++){
    				Yss [yo][i] = (int) points[ triangles[i].i1 ].y;
    				Yss [yo][i] = (int) points[ triangles[i].i2 ].y;
    			    Yss [yo][i] = (int) points[ triangles[i].i3 ].y;
    			    yo++;
    			}
    		   }
    		}

no errors :>

fixed it but still nothing come out in the screen when i import a file

i have attached the gui

nothing come out in the screen when i import a file

Check your logic to see why not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.