import?java.awt.Canvas;

在沙縣等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需求定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營(yíng)銷推廣,外貿(mào)網(wǎng)站制作,沙縣網(wǎng)站建設(shè)費(fèi)用合理。
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.EventQueue;
import?java.awt.Frame;
import?java.awt.Graphics;
import?java.awt.Graphics2D;
import?java.awt.Image;
import?java.awt.RenderingHints;
import?java.awt.event.KeyEvent;
import?java.awt.event.KeyListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.IOException;
import?javax.imageio.ImageIO;
public?class?TestImage?extends?Frame
{
private?static?final?long?serialVersionUID?=?1L;
private?static?boolean?PRESSED?=?false;
private?static?int?pointX?=?0;
private?static?int?pointy?=?200;
private?static?int?RIGHT_GO?=?0;
private?static?int?LEFT_GO?=?0;
private?static?int?DIR?=?0;
private?static?int?ANGLE?=?0;
private?static?int?W?=?50;
private?static?int?H?=?60;
private?_Canvas?canvas?=?null;
public?TestImage?()
{
add?(canvas?=?new?_Canvas?());
setIgnoreRepaint?(true);
requestFocus?();
}
public?class?_Canvas?extends?Canvas?implements?Runnable
{
private?static?final?long?serialVersionUID?=?1L;
private?BufferedImage?bi?=?null;
private?Image?bufferedImage?=?null;
private?Thread?thread?=?null;
private?long?sleepTime?=?10;
public?_Canvas?()
{
try
{
bi?=?ImageIO.read?(new?File?("go.png"));
}
catch?(IOException?e)
{}
setBackground?(Color.BLACK);
requestFocus?();
addKeyListener?(new?KeyListener?()
{
@Override
public?void?keyTyped?(?KeyEvent?e?)
{}
@Override
public?void?keyReleased?(?KeyEvent?e?)
{
RIGHT_GO?=?0;
PRESSED?=?false;
}
@Override
public?void?keyPressed?(?KeyEvent?e?)
{
//?38?40?37?39上下左右
DIR?=?e.getKeyCode?();
PRESSED?=?true;
}
});
}
@Override
public?void?paint?(?Graphics?g?)
{
Graphics2D?g2d?=?(Graphics2D)?g;
g2d.setRenderingHint?(RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage?(rotateImage?(bi.getSubimage?(RIGHT_GO,?LEFT_GO,?W,?H),?ANGLE,?true),?pointX,?pointy,?W,?H,
this);
g2d.dispose?();
}
@Override
public?void?update?(?Graphics?g?)
{
if?(null?==?bufferedImage)
{
bufferedImage?=?createImage?(getWidth?(),?getHeight?());
}
Graphics?bufferedG?=?bufferedImage.getGraphics?();
bufferedG.clearRect?(0,?0,?getWidth?(),?getHeight?());
paint?(bufferedG);
bufferedG.dispose?();
g.drawImage?(bufferedImage,?0,?0,?this);
g.dispose?();
}
public?void?start?()
{
thread?=?new?Thread?(this);
thread.setName?("TestImage");
thread.setPriority?(Thread.MIN_PRIORITY);
thread.start?();
}
public?synchronized?void?stop?()
{
thread?=?null;
notify?();
}
@Override
public?void?run?()
{
Thread?me?=?Thread.currentThread?();
while?(thread?==?me??!isShowing?()?||?getSize?().width?==?0)
{
try
{
Thread.sleep?(555);
}
catch?(InterruptedException?e)
{
return;
}
}
while?(thread?==?me??isShowing?())
{
if?(PRESSED)
{
try
{
if?(DIR?==?39)
{
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?0;
pointX?=?pointX?+?1;
if?(pointX??420)
{
ANGLE?=?90;
pointX--;
pointy--;
W?=?60;
H?=?50;
}
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
}
else?if?(DIR?==?37)
{
pointX?=?pointX?-?1;
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?60;
if?(pointX??0)
{
ANGLE?=?-90;
pointX++;
pointy--;
W?=?60;
H?=?50;
}
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
}
else?if?(DIR?==?38)
{
W?=?50;
H?=?60;
pointy?=?150;
ANGLE?=?0;
RIGHT_GO?=?100;
}
else?if?(DIR?==?40)
{
W?=?50;
H?=?60;
ANGLE?=?0;
pointy?=?200;
RIGHT_GO?=?0;
}
Thread.sleep?(sleepTime);
repaint?();
}
catch?(InterruptedException?e)
{
break;
}
}
else
{
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?0;
pointX?=?pointX?+?1;
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
if?(pointX??500)
{
pointX?=?0;
}
try
{
Thread.sleep?(sleepTime);
repaint?();
}
catch?(InterruptedException?e)
{
break;
}
}
}
thread?=?null;
}
}
/**
?*?旋轉(zhuǎn)圖像為指定角度
?*?
?*?@param?degree
?*?@return
?*/
public?static?BufferedImage?rotateImage?(?final?BufferedImage?image,?final?int?angdeg,?final?boolean?d?)
{
int?w?=?image.getWidth?();
int?h?=?image.getHeight?();
int?type?=?image.getColorModel?().getTransparency?();
BufferedImage?img;
Graphics2D?graphics2d;
(?graphics2d?=?(?img?=?new?BufferedImage?(w,?h,?type)?).createGraphics?()?).setRenderingHint?(
RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate?(d???-Math.toRadians?(angdeg)?:?Math.toRadians?(angdeg),?w?/?2,?h?/?2);
graphics2d.drawImage?(image,?0,?0,?null);
graphics2d.dispose?();
return?img;
}
public?static?void?main?(?String[]?args?)
{
EventQueue.invokeLater?(new?Runnable?()
{
@Override
public?void?run?()
{
final?TestImage?ti?=?new?TestImage?();
ti.setSize?(new?Dimension?(500,?300));
ti.setLocationRelativeTo?(null);
ti.addWindowListener?(new?WindowAdapter?()
{
@Override
public?void?windowClosing?(?WindowEvent?e?)
{
System.exit?(0);
}
@Override
public?void?windowDeiconified?(?WindowEvent?e?)
{
ti.canvas.start?();
}
@Override
public?void?windowIconified?(?WindowEvent?e?)
{
ti.canvas.stop?();
}
});
ti.setResizable?(false);
ti.canvas.start?();
ti.setVisible?(true);
}
});
}
}
程序?qū)崿F(xiàn)思路: 在javafx中Node對(duì)象有一個(gè)effect屬性,可以用于實(shí)現(xiàn)各種特效。PerspectiveTransform特效可以使Node對(duì)象實(shí)現(xiàn)透視變換。因此我們可以通過(guò)計(jì)算透視變換中每個(gè)點(diǎn)的位置來(lái)實(shí)現(xiàn)3D翻轉(zhuǎn)特效。
實(shí)現(xiàn)步驟: 1、定義FlipView對(duì)象。包含以下屬性:
復(fù)制代碼 代碼如下:
//正面視圖
public Node frontNode;
//反面視圖
public Node backNode;
//是否翻轉(zhuǎn)
boolean flipped = false;
//翻轉(zhuǎn)角度
DoubleProperty time = new SimpleDoubleProperty(Math.PI / 2);
//正面翻轉(zhuǎn)特效
PerspectiveTransform frontEffect = new PerspectiveTransform();
//反面翻轉(zhuǎn)特效
PerspectiveTransform backEffect = new PerspectiveTransform();
create方法返回需要顯示的內(nèi)容:
復(fù)制代碼 代碼如下:
private void create() {
time.addListener(new ChangeListener() {
@Override
public void changed(ObservableValue? extends Number arg0,
Number arg1, Number arg2) {
setPT(frontEffect, time.get());
setPT(backEffect, time.get());
}
});
anim.getKeyFrames().addAll(frame1, frame2);
backNode.visibleProperty().bind(
Bindings.when(time.lessThan(0)).then(true).otherwise(false));
frontNode.visibleProperty().bind(
Bindings.when(time.lessThan(0)).then(false).otherwise(true));
setPT(frontEffect, time.get());
setPT(backEffect, time.get());
frontNode.setEffect(frontEffect);
backNode.setEffect(backEffect);
getChildren().addAll(backNode, frontNode);
}
以上代碼需要注意的是: 隨著time值的變化frontEffect和backEffect的值也會(huì)隨著變換。 2、PerspectiveTransform特效的實(shí)現(xiàn)使用了Math.sin()和Math.cos()方法模擬3D角度變換。 具體實(shí)現(xiàn)如下:
復(fù)制代碼 代碼如下:
private void setPT(PerspectiveTransform pt, double t) {
double width = 200;
double height = 200;
double radius = width / 2;
double back = height / 10;
pt.setUlx(radius - Math.sin(t) * radius);
pt.setUly(0 - Math.cos(t) * back);
pt.setUrx(radius + Math.sin(t) * radius);
pt.setUry(0 + Math.cos(t) * back);
pt.setLrx(radius + Math.sin(t) * radius);
pt.setLry(height - Math.cos(t) * back);
pt.setLlx(radius - Math.sin(t) * radius);
pt.setLly(height + Math.cos(t) * back);
}
3、角度變換在1秒的時(shí)間內(nèi)從3.14/2變換到-3.14/2。
復(fù)制代碼 代碼如下:
KeyFrame frame1 = new KeyFrame(Duration.ZERO, new KeyValue(time,
Math.PI / 2, Interpolator.LINEAR));
KeyFrame frame2 = new KeyFrame(Duration.seconds(1),
new EventHandler() {
@Override
public void handle(ActionEvent event) {
flipped = !flipped;
}
}, new KeyValue(time, -Math.PI / 2, Interpolator.LINEAR));
4、FlipView對(duì)象的創(chuàng)建:通過(guò)構(gòu)造函數(shù)可以很方便的創(chuàng)建FlipView對(duì)象.
復(fù)制代碼 代碼如下:
ImageView image1 = new ImageView(new Image(getClass()
.getResourceAsStream("lion1.png")));
ImageView image2 = new ImageView(new Image(getClass()
.getResourceAsStream("lion2.png")));
FlipView flip = new FlipView(image1, image2);
可參考 孫博文 的一本書(shū) 分形算法與程序設(shè)計(jì): Java實(shí)現(xiàn) 里面有3D的內(nèi)容
貌似要下載 JAVA3D 的msi安裝包 解壓后得到j(luò)ar包 JAVA 3D已經(jīng)被淘汰 可能有點(diǎn)難找
用Java3D編程就行了 你先把那書(shū)上關(guān)于3D的代碼 稍微看一下 編程的思路也就是 先建立一個(gè)場(chǎng)景(有光) 然后空間描點(diǎn) 畫(huà)線 著色 之類的 可以參考具體的Java 3D 的書(shū) 貌似大多是英文的
編程也可以參考官方API文檔
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import static java.lang.Math.*;
import com.sun.j3d.utils.behaviors.mouse.*;
public class Ball11 extends Applet
{
private float vert[]=new float[30000];
private Color3f color[]=new Color3f[30000];
public BranchGroup createSceneGraph(){
BranchGroup objRoot=new BranchGroup();//分支組
BoundingSphere bounds=new BoundingSphere(new Point3d(),100.0);
Color3f bgColor=new Color3f(1.0f,1.0f,1.0f);
Background bg=new Background(bgColor);//背景顏色
bg.setApplicationBounds(bounds);//背景范圍
objRoot.addChild(bg);
//添加平行光
DirectionalLight lightD1=new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f direction=new Vector3f(2.5f,8.0f,-1.0f);
Color3f color1=new Color3f(1.0f,0.0f,0.0f);
direction.normalize();
lightD1.setDirection(direction);
lightD1.setColor(color1);
objRoot.addChild(lightD1);
//初始化
pointinit();
//添加運(yùn)輸組
TransformGroup trans=new TransformGroup();
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(trans);
Shape3D shape=new Shape3D();
QuadArray qArray=new QuadArray(30000,QuadArray.COORDINATES|QuadArray.COLOR_3);
qArray.setCoordinates(0,vert);
qArray.setColors(0,color);
PolygonAttributes pa=new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
shape.setGeometry(qArray);
Appearance app=new Appearance();
app.setPolygonAttributes(pa);
shape.setAppearance(app);
trans.addChild(shape);
//添加鼠標(biāo)事件
MouseRotate rotate=new MouseRotate();
rotate.setTransformGroup(trans);
objRoot.addChild(rotate);
rotate.setSchedulingBounds(bounds);
MouseZoom zoom=new MouseZoom();
zoom.setTransformGroup(trans);
objRoot.addChild(zoom);
zoom.setSchedulingBounds(bounds);
MouseTranslate translate=new MouseTranslate();
translate.setTransformGroup(trans);
objRoot.addChild(translate);
translate.setSchedulingBounds(bounds);
objRoot.compile();
return objRoot;
}
public void pointinit(){
int i=0;
for(double a=0;a=2*Math.PI;a+=0.09){
for(double b=0;b=Math.PI;b+=0.1){
vert[i]=(float)Math.sin(b)*(float)cos(a);i++;
vert[i]=(float)Math.sin(a)*(float)sin(b);i++;
vert[i]=(float)Math.cos(b);i++;
vert[i]=(float)Math.sin(b)*(float)cos(a+0.1);i++;
vert[i]=(float)Math.sin(a+0.1)*(float)sin(b);i++;
vert[i]=(float)Math.cos(b+0.1);i++;
vert[i]=(float)Math.sin(b+0.1)*(float)cos(a+0.1);i++;
vert[i]=(float)Math.sin(a+0.1)*(float)sin(b+0.1);i++;
vert[i]=(float)Math.cos(b+0.1);i++;
vert[i]=(float)Math.sin(b+0.1)*(float)cos(a);i++;
vert[i]=(float)Math.sin(a)*(float)sin(b+0.1);i++;
vert[i]=(float)Math.cos(b+0.1);i++;
}
}
for(i=0;i30000;i++){
color[i]=new Color3f(0.4f,0.0f,0.5f);
}
}
public Ball11(){
setLayout(new BorderLayout());
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
Canvas3D c=new Canvas3D(config);
add("Center",c);
BranchGroup scene=createSceneGraph();
SimpleUniverse u=new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args){
long time1=System.nanoTime();
new MainFrame(new Ball11(),400,400);
long time2=System.nanoTime();
System.out.println("用時(shí)為: "+(time2-time1)/1000000000.0 + " 秒");
}
}
分享名稱:關(guān)于java3d動(dòng)畫(huà)代碼的信息
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article29/dsicojh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、做網(wǎng)站、響應(yīng)式網(wǎng)站、網(wǎng)站維護(hù)、手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)