新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.Canvas;
在沙县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、网站制作 网站设计制作按需求定制制作,公司网站建设,企业网站建设,成都品牌网站建设,全网营销推广,外贸网站制作,沙县网站建设费用合理。
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;
}
}
/**
* 旋转图像为指定角度
*
* @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);
}
});
}
}
程序实现思路: 在javafx中Node对象有一个effect属性,可以用于实现各种特效。PerspectiveTransform特效可以使Node对象实现透视变换。因此我们可以通过计算透视变换中每个点的位置来实现3D翻转特效。
实现步骤: 1、定义FlipView对象。包含以下属性:
复制代码 代码如下:
//正面视图
public Node frontNode;
//反面视图
public Node backNode;
//是否翻转
boolean flipped = false;
//翻转角度
DoubleProperty time = new SimpleDoubleProperty(Math.PI / 2);
//正面翻转特效
PerspectiveTransform frontEffect = new PerspectiveTransform();
//反面翻转特效
PerspectiveTransform backEffect = new PerspectiveTransform();
create方法返回需要显示的内容:
复制代码 代码如下:
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的值也会随着变换。 2、PerspectiveTransform特效的实现使用了Math.sin()和Math.cos()方法模拟3D角度变换。 具体实现如下:
复制代码 代码如下:
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秒的时间内从3.14/2变换到-3.14/2。
复制代码 代码如下:
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对象的创建:通过构造函数可以很方便的创建FlipView对象.
复制代码 代码如下:
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);
可参考 孙博文 的一本书 分形算法与程序设计: Java实现 里面有3D的内容
貌似要下载 JAVA3D 的msi安装包 解压后得到jar包 JAVA 3D已经被淘汰 可能有点难找
用Java3D编程就行了 你先把那书上关于3D的代码 稍微看一下 编程的思路也就是 先建立一个场景(有光) 然后空间描点 画线 着色 之类的 可以参考具体的Java 3D 的书 貌似大多是英文的
编程也可以参考官方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();
//添加运输组
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);
//添加鼠标事件
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("用时为: "+(time2-time1)/1000000000.0 + " 秒");
}
}