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?