アットウィキロゴ

3次元の練習

import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;

public class ren extends JFrame{

int com[][]=new int[6][6];
int z1[][]=new int[6][6];
int z2[][]=new int[6][6];
  
double c[][]=new double[6][6];
double y1[][]=new double[6][6];
double y2[][]=new double[6][6];    
String[] data=new String[500];    
int datanumber,s,sx,s1,s2;
double[] num=new double[6];
String str;

public static void main(String[] args){
    ren test = new ren();

    test.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){System.exit(0);}
    });

    test.setBounds( 0, 0, 700, 700);
    test.setVisible(true);
  }

ren(){

readfile("data.txt");

for(s=1;s<26;s++){
str=data[s];
String[] x=str.split(">");
for(sx=0;sx<x.length;sx++){
x[sx]=x[sx].replace("<","");
num[sx]=Double.parseDouble(x[sx]);
}
s1=(int)num[0];
s2=(int)num[1];
c[s1][s2]=num[2];
y1[s1][s2]=num[3];
y2[s1][s2]=num[4];
}

for(s1=1;s1<6;s1++){
for(s2=1;s2<6;s2++){
com[s1][s2]=(int)(1000*c[s1][s2]);
z1[s1][s2]=(int)(1000*y1[s1][s2]);
z2[s1][s2]=(int)(1000*y2[s1][s2]);
}
}


}




public void paint(Graphics g){
double x,y,z;
int px,py,qx,qy;
    Graphics2D g2 = (Graphics2D)g;

    BufferedImage readImage = null;
    

    if (readImage == null){
      readImage = new BufferedImage(getWidth(), getHeight(),
        BufferedImage.TYPE_INT_BGR);
    }

    Graphics2D off = readImage.createGraphics();

    off.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

BasicStroke wideStroke = new BasicStroke(2.0f);
    off.setStroke(wideStroke);

off.setPaint(Color.yellow);

x=0;
y=0;
z=500;
px=pointx(x,y,z);
py=pointy(x,y,z);
off.setColor(Color.blue);
off.drawLine(100,600,100+px,600-py);    

x=500;
y=0;
z=0;
px=pointx(x,y,z);
py=pointy(x,y,z);
off.drawLine(100,600,100+px,600-py);

x=0;
y=500;
z=0;
px=pointx(x,y,z);
py=pointy(x,y,z);
off.drawLine(100,600,100+px,600-py);

for(s2=1;s2<6;s2++){
for(s1=1;s1<5;s1++){
x=z1[s1][s2];
y=z2[s1][s2];
z=com[s1][s2];
px=pointx(x,y,z);
py=pointy(x,y,z);
x=z1[s1+1][s2];
y=z2[s1+1][s2];
z=com[s1+1][s2];
qx=pointx(x,y,z);
qy=pointy(x,y,z);
off.drawLine(100+px,600-py,100+qx,600-qy);
}
}

for(s1=1;s1<6;s1++){
for(s2=1;s2<5;s2++){
x=z1[s1][s2];
y=z2[s1][s2];
z=com[s1][s2];
px=pointx(x,y,z);
py=pointy(x,y,z);
x=z1[s1][s2+1];
y=z2[s1][s2+1];
z=com[s1][s2+1];
qx=pointx(x,y,z);
qy=pointy(x,y,z);
off.drawLine(100+px,600-py,100+qx,600-qy);
}
}



    
    if (readImage != null){g2.drawImage(readImage,0,0, this);}

    try {
      boolean result =
       ImageIO.write(readImage, "jpg", new File("sam.jpg"));
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

public static int pointy(double x,double y,double z){
int p2;
double theta,phi;
theta=-0.222222*Math.PI;
phi=0.333333*Math.PI;
p2=(int)(-Math.cos(theta)*Math.cos(phi)*x-Math.sin(theta)*Math.cos(phi)*y+Math.sin(phi)*z);
return p2;
}


public static int pointx(double x,double y,double z){
int p1;
double theta;
theta=-0.222222*Math.PI;
p1=(int)(-Math.sin(theta)*x+Math.cos(theta)*y);
return p1;
}

void readfile(String file){

String str;
BufferedReader br;  

s=0;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"SJIS"));  
while((str = br.readLine()) != null) {  
s=s+1;
data[s]=str;  
}
br.close();
} catch (IOException e) {System.out.println(e);}  

datanumber=s;

}


}
最終更新:2011年07月31日 03:39