- The view can be controlled by dragging the mouse over the JFrame.
- The first part in the main method labelled DYNAMIC can be edited to change the JFrame size (i.e, the view size) and the opacity of the objects.
- Run the code in a file named vrjar
- The jar can be downloaded from here (requires JRE 7 that can be downloaded here)
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class vrjar {
@SuppressWarnings("serial")
public static void main(String[] args) throws InterruptedException {
//Dynamic
final int s = 300;
final int op = 20;
//Statics
final JFrame frm = new JFrame("3DViewer");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(s + 15, s + 39);
frm.setLayout(null);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final pointcolor a[] = new pointcolor[(s + 1) * (s + 1) * (s + 1)];
final int ct[] =
{0};
for (int i = 0; i < s; i++) {
a[ct[0]] = new pointcolor(s / 2, s / 2, i, Color.green);
ct[0]++;
a[ct[0]] = new pointcolor(s / 2, i, s / 2, Color.red);
ct[0]++;
a[ct[0]] = new pointcolor(i, s / 2, s / 2, Color.blue);
ct[0]++;
}
for (int i = s / 2 - 50; i < s / 2 + 50; i++) {
for (int j = s / 2 - 50; j < s / 2 + 50; j++) {
a[ct[0]] = new pointcolor(i, j, s / 2 + 50, Color.blue);
ct[0]++;
a[ct[0]] = new pointcolor(i, j, s / 2 - 50, Color.blue);
ct[0]++;
}
}
for (int i = s / 2 - 50; i < s / 2 + 50; i++) {
for (int j = s / 2 - 50; j < s / 2 + 50; j++) {
a[ct[0]] = new pointcolor(i, s / 2 + 50, j, Color.green);
ct[0]++;
a[ct[0]] = new pointcolor(i, s / 2 - 50, j, Color.green);
ct[0]++;
}
}
for (int i = s / 2 - 50; i < s / 2 + 50; i++) {
for (int j = s / 2 - 50; j < s / 2 + 50; j++) {
a[ct[0]] = new pointcolor(s / 2 +
50, i, j, Color.red);
ct[0]++;
a[ct[0]] = new pointcolor(s / 2 -
50, i, j, Color.red);
ct[0]++;
}
}
for(int i=s/2-50;i<s/2+50;i++){
a[ct[0]]=new pointcolor(s/2-50,s/2-50,i,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2+50,s/2-50,i,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2-50,s/2+50,i,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2+50,s/2+50,i,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2-50,i,s/2-50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2+50,i,s/2-50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2-50,i,s/2+50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(s/2+50,i,s/2+50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(i,s/2-50,s/2-50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(i,s/2+50,s/2-50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(i,s/2-50,s/2+50,Color.black);ct[0]++;
a[ct[0]]=new pointcolor(i,s/2+50,s/2+50,Color.black);ct[0]++;
}
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++) {
for (int k = 0; k < s; k++) {
double x = i - s / 2;
double y = j - s / 2;
double z = k - s / 2;
double acc = 2;
double lhs = x*x+y*y+z*z;
double rhs = 2500;
if (Math.floor((lhs) / Math.pow(10, acc)) * Math.pow(10, acc) == Math.floor((rhs) / Math.pow(10, acc)) * Math.pow(10, acc)) {
a[ct[0]] = new pointcolor(i, j, k, Color.yellow);
ct[0]++;
}
}
}
}
final JPanel pnl[] = {new JPanel()};
final Point loc[] = {new Point(0, 0)};
final double xthet[] = {0};
final double ythet[] = {0};
final int sens = 1;
frm.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent arg0) {
loc[0] = MouseInfo.getPointerInfo().getLocation();
}
@Override
public void mouseDragged(MouseEvent arg0) {
frm.remove(pnl[0]);
double xthl = xthet[0];
double ythl = ythet[0];
if (Math.abs(loc[0].y - arg0.getYOnScreen())
> Math.abs(loc[0].x - arg0.getXOnScreen())) {
xthl = Math.toRadians(360 + (loc[0].y - arg0.getYOnScreen())
% 360 / sens) + xthet[0];
}
else {
ythl = Math.toRadians(360 + (-loc[0].x + arg0.getXOnScreen())
% 360 / sens) + ythet[0];
}
final double xth = xthl;
final double yth = ythl;
loc[0] = arg0.getLocationOnScreen();
xthet[0] = xth;
ythet[0] = yth;
//final double loc[][]=new double[s+100][s+100];
pnl[0] = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.black);
for (int i = 0; i < ct[0]; i++) {
double x = a[i].x - s / 2;
double y = a[i].y - s / 2;
double z = a[i].z - s / 2;
//X-Yaw
double ytemp = y;
y = Math.cos(xth) * y - Math.sin(xth) * z;
z = Math.cos(xth) * z + Math.sin(xth) * ytemp;
//Y-Pitch
double xtemp = x;
x = x * Math.cos(yth) + z * Math.sin(yth);
z = z * Math.cos(yth) - xtemp * Math.sin(yth);
g.setColor(new Color(0, 0, 0, 1f));
double d = (z + s) / 360;
if (a[i].x == s - 1 && a[i].y == s / 2 && a[i].z == s / 2) {
g.drawString("X", (int)(x * d + s / 2), (int)(y * d + s / 2));
}
if (a[i].x == s / 2 && a[i].y == s - 1 && a[i].z == s / 2) {
g.drawString("Y", (int)(x * d + s / 2), (int)(y * d + s / 2));
}
if (a[i].x == s / 2 && a[i].y == s / 2 && a[i].z == s - 1) {
g.drawString("Z", (int)(x * d + s / 2), (int)(y * d + s / 2));
}
/*if (z>=loc[(int) (x+s/2)][(int)
(y+s/2)]) {
loc[(int)
(x+s/2)][(int) (y+s/2)]=z;*/
g.setColor(new Color(a[i].col.getRed(),
a[i].col.getGreen(),
a[i].col.getBlue(),
op));
g.fillRect((int)(x * d + s / 2), (int)(y * d + s / 2), 2, 2);
//}
}
}
};
pnl[0].setSize((int)(s * Math.sqrt(3)), (int)(s * Math.sqrt(3)));
frm.add(pnl[0]);
frm.invalidate();
frm.validate();
frm.repaint();
}
});
frm.setVisible(true);
try {
Robot
r=new Robot();
r.mouseMove(frm.getLocation().x+40, frm.getLocation().y+40);
r.mousePress(InputEvent.BUTTON2_DOWN_MASK);
r.mouseMove(frm.getLocation().x+30, frm.getLocation().y+30);
r.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
}
catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
No comments:
Post a Comment