Total members 11890 |It is currently Wed Apr 24, 2024 5:31 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






* Project Name:   draw cube 3d java-Cube3D
* Programmer:   msi_333
* Type:   Graphics
* Technology:  Java
* IDE:   NetBeans
* Description:   How 3d cube works?

you use up and down arrows to move toward the cube.

Program is attached, with a picture.


java code
package graphices;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;


public class Draw3D {

/** Creates a new instance of Draw3D */
public Draw3D(int width,int height,Transform3D transformer,ViewWindow window) {
this.width=width;
this.height=height;
this.transformer=transformer;
this.window=window;
}

public void draw3D(Graphics2D g,ArrayList polygons) {
draw_Background(g);//draw the background of the screen


for(int k=0;k<polygons.size();k++) {
drawPol.setTo(((Polygon3D)polygons.get(k)));


Before_Draw(drawPol);//transform+Project to the polygon

myPath=new GeneralPath();//every time there is a new GeneralPath shape


temp=drawPol.getVertex(0);
myPath.moveTo(temp.x,temp.y);

for(int i=1;i<drawPol.getNumVertices();i++) {
temp=drawPol.getVertex(i);
myPath.lineTo(temp.x,temp.y);

}
temp=drawPol.getVertex(0);
myPath.lineTo(temp.x,temp.y);


g.setColor(polygons_colors[k]);
g.draw(myPath);
}






}

public void draw_Background(Graphics2D g ) {

g.setColor(Color.BLACK);
g.fillRect(0,0,width,height);

}
public void Before_Draw(Polygon3D polygon) {

polygon.add(transformer);
polygon.project(window);
}

private final int width;
private final int height;
private final Transform3D transformer;
private final ViewWindow window;
private Polygon3D drawPol=new Polygon3D();
private GeneralPath myPath;
private Color[] polygons_colors=new Color[]{Color.RED,Color.YELLOW,Color.PINK,Color.GREEN,Color.WHITE,
Color.orange,};
private Vector3D temp;
}

java code
package graphices;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
*
* @author user
*/
public class main extends JFrame{

/** Creates a new instance of main */
public main() {
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setIgnoreRepaint(true);


myDevice.setFullScreenWindow(this);
myDevice.setDisplayMode(myMode);
createBufferStrategy(2);
addKeyListener(mover);
setCursor( getToolkit().getDefaultToolkit().createCustomCursor(new ImageIcon(" ").getImage(),new Point(0,0)
,"invisble"));

Loop();

}

/**
* @param args the command line arguments
*/



public static void main(String[] args) {
// TODO code application logic here
main m= new main();

}
public void Loop() {
BufferStrategy myBuffer=getBufferStrategy();
while(true) {

Graphics2D g=(Graphics2D)myBuffer.getDrawGraphics();
draw(g) ;
myBuffer=getBufferStrategy();
g.dispose();

myBuffer.show();
try {


Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

}
public void draw(Graphics2D g2d) {
GeneralPath myP=new GeneralPath();




myDrawer.draw3D(g2d,myCube.getPolygons());







g2d.setColor(Color.WHITE);

g2d.drawString("Press UP / DOWN / ESC ",20.0f,20.0f);
g2d.drawString("Press Q/A Rotation Z ",20.0f,40.0f);
g2d.drawString("Press W/S Rotation X ",20.0f,60.0f);
g2d.drawString("Press E/D Rotation Y ",20.0f,80.0f);
g2d.drawString("Shape: Cube ",20.0f,100.0f);

}
private final int width=800;
private final int height=600;
private GraphicsEnvironment myenv=GraphicsEnvironment.getLocalGraphicsEnvironment();
private GraphicsDevice myDevice=myenv.getDefaultScreenDevice();
private DisplayMode myMode=new DisplayMode(width,height,32,DisplayMode.REFRESH_RATE_UNKNOWN);
private Vector3D[] v=new Vector3D[]{new Vector3D(0, 0, 0),new Vector3D(100, 0, 0),new Vector3D(100, 150,0),new Vector3D(0, 150,0)};
private Polygon3D myPol=new Polygon3D(v);
private Polygon3D drawPol=new Polygon3D();
private Transform3D trans=new Transform3D(0.0f,-100.0f,-400.0f);
private ViewWindow myWindow=new ViewWindow(0,0,width,height, (float)Math.toRadians(75));
private MoveManager mover=new MoveManager(trans);
private Draw3D myDrawer=new Draw3D(width,height,trans,myWindow);
private Cube3D myCube=new Cube3D();

}


Attachment:
File comment: Cube3d screenshot
Cube3D.gif
Cube3D.gif [ 4.57 KiB | Viewed 15607 times ]





Attachments:
File comment: Project source
Cube3D.rar [36.63 KiB]
Downloaded 1861 times

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : draw cube 3d java-Cube 3D
 Draw 3d cube using openGL     -  
 3D-Cube     -  
 cube 3d transformation     -  
 Cube number     -  
 Cube Series     -  
 Draw An Arc in java     -  
 draw Calendar in java     -  
 Draw Flag in Java     -  
 draw Oval in java     -  
 draw text effects in java     -  



Topic Tags

Java Graphics, Java Projects
cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com