import?java.awt.Canvas;
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),澤普企業(yè)網(wǎng)站建設(shè),澤普品牌網(wǎng)站建設(shè),網(wǎng)站定制,澤普網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,澤普網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
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);
}
});
}
}
一: 用多線程播放一組圖片, 實(shí)現(xiàn)動(dòng)畫片的效果; 類似于逐幀動(dòng)畫,每個(gè)圖片是動(dòng)畫的一幀
二: 在awt/swing界面里, 可以使用paint方法,去繪制圖形,然后用swing提供的Timer或者多線程技術(shù),去刷新繪制的圖形
三:在JavaFX里, 本身就支持動(dòng)畫,并且封裝了很多動(dòng)畫效果可以直接使用,比如逐幀動(dòng)畫.縮放動(dòng)畫,漸變動(dòng)畫,旋轉(zhuǎn)動(dòng)畫,位置動(dòng)畫等.
強(qiáng)烈推薦使用javaFX來(lái)實(shí)現(xiàn)動(dòng)畫, 因?yàn)閖avaFX是現(xiàn)代化的圖形界面工具,具有簡(jiǎn)單,強(qiáng)大,組件豐富,跨平臺(tái),支持Html5, 支持表格, 支持動(dòng)畫等多種優(yōu)勢(shì)?
下面是一個(gè)javaFX繪制的動(dòng)態(tài)表格
javaFX動(dòng)態(tài)表格
/*顯示一個(gè)立方體*/
#include dos.h
#include math.h
#include conio.h
#include stdio.h
#include stdlib.h
#include graphics.h
#define PI 3.1415926
/*定義按鍵*/
#define ESC 0x11b
/*以下4個(gè)鍵,依次是上 下 左 右*/
#define X_axis_clkwise 0x4800
#define X_axis_Cntclkwise 0x5000
#define Y_axis_clkwise 0x4b00
#define Y_axis_Cntclkwise 0x4d00
/*以下2個(gè)鍵,依次是A, D*/
#define Z_axis_clkwise 0x1e61
#define Z_axis_Cntclkwise 0x2064
#define Distance_forward 0x1177
#define Distance_Backward 0x1f73
/*以下6個(gè)鍵,依次是U, J, I, K, O, L*/
#define X_Delta_Plus 0x1675
#define X_Delta_Minus 0x246a
#define Y_Delta_Plus 0x1769
#define Y_Delta_Minus 0x256b
#define Z_Delta_Plus 0x186f
#define Z_Delta_Minus 0x266c
/*繞X軸旋轉(zhuǎn)矩陣*/
float X_Rotate_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*繞Y軸旋轉(zhuǎn)矩陣*/
float Y_Rotate_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*繞Z軸旋轉(zhuǎn)矩陣*/
float Z_Rotate_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*平移矩陣*/
float Transist_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*透視投影變換矩陣*/
float Perspective_Projection[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1 };
int num;
float *Matrix_Mul(float *pMatrix1, int Num_Row_Matrix1, int Num_Column_Matrix1,
float *pMatrix2, int Num_Row_Matrix2, int Num_Column_Matrix2 ) {
/*實(shí)現(xiàn)兩個(gè)矩陣:
輸入?yún)?shù): *pMatrix1: 指向第一個(gè)矩陣
Num_Row_Matrix1: 第一個(gè)矩陣的行數(shù)
Num_Column_Matrix1: 第一個(gè)矩陣的列數(shù)
余下三個(gè)參數(shù)類推;
return 指向運(yùn)算結(jié)果的float類型指針.*/
int i, j, m, n;
float *pNewMatrix1, *pNewMatrix2, Sum;
if( Num_Column_Matrix1 != Num_Row_Matrix2) {
printf("Invalid Matrixs!\n");
return 0;
}
pNewMatrix1 = malloc(Num_Row_Matrix1 * Num_Column_Matrix2 * 4);
/*申請(qǐng)內(nèi)存空間, Size(/bytes) = 第一個(gè)矩陣的行數(shù) * 第二個(gè)矩陣的列數(shù) * 4(= sizeof(float))*/
pNewMatrix2 = pNewMatrix1;
/*具體算法詳見如下代碼*/
for( i = 0; i Num_Row_Matrix1; i++) {
for( n = 0; n Num_Column_Matrix2; n++) {
Sum = 0;
for( j = 0; j Num_Column_Matrix1; j++)
Sum += (*(pMatrix1+i*Num_Column_Matrix1+j)) * (*(pMatrix2+j*Num_Column_Matrix2+n));
*(pNewMatrix1++) = Sum;
}
}
return pNewMatrix2;
}
/*轉(zhuǎn)換成齊次坐標(biāo)矩陣*/
void Matrix_Convertion(float *pMatrix, int Num_Row) {
int i, j;
for(i = 0; i Num_Row; i++) {
if((*(pMatrix+i*4+3)) != 0) {
*(pMatrix+i*4) = (*(pMatrix+i*4)) / (*(pMatrix+i*4+3));
*(pMatrix+i*4+1) = (*(pMatrix+i*4+1)) / (*(pMatrix+i*4+3));
*(pMatrix+i*4+2) = (*(pMatrix+i*4+2)) / (*(pMatrix+i*4+3));
}
}
}
/*取得投影坐標(biāo)*/
float *Get_X_Y(float *pMatrix, int Num_Row) {
int i, j, Num;
float *pNewMatrix;
Num = 0;
for(i = 0; i Num_Row; i++) {
if((*(pMatrix+i*4+3)) != 0)
Num++;
}
pNewMatrix = malloc(Num * 2 * 4);
/*存放格式,{(x1, y1),(x2, y2), ... ,(xn, yn)}*/
for(i = 0; i Num; i++) {
if((*(pMatrix+i*4+3)) != 0) {
*(pNewMatrix+i*2) = (*(pMatrix+i*4))+300; /*顯示在屏幕中心, x = 300*/
*(pNewMatrix+i*2+1) = (*(pMatrix+i*4+1))+200; /*顯示在屏幕中心, y = 200*/
}
}
return pNewMatrix;
}
/*設(shè)置旋轉(zhuǎn)矩陣, Rotate around aixs labled with X or Y or Z*/
void SetMatrix_X(float X_Angle) {
float CosX, SinX;
SinX = sin(X_Angle * PI /128);
CosX = cos(X_Angle * PI /128);
X_Rotate_Matrix[1][1] = CosX;
X_Rotate_Matrix[1][2] = SinX;
X_Rotate_Matrix[2][1] = -1 * SinX;
X_Rotate_Matrix[2][2] = CosX;
}
void SetMatrix_Y(float Y_Angle) {
float CosY, SinY;
SinY = sin(Y_Angle * PI /128);
CosY = cos(Y_Angle * PI /128);
Y_Rotate_Matrix[0][0] = CosY;
Y_Rotate_Matrix[0][2] = -1 * SinY;
Y_Rotate_Matrix[2][0] = SinY;
Y_Rotate_Matrix[2][2] = CosY;
}
void SetMatrix_Z(float Z_Angle) {
float CosZ, SinZ;
SinZ = sin(Z_Angle * PI /128);
CosZ = cos(Z_Angle * PI /128);
Z_Rotate_Matrix[0][0] = CosZ;
Z_Rotate_Matrix[0][1] = SinZ;
Z_Rotate_Matrix[1][0] = -1 * SinZ;
Z_Rotate_Matrix[1][1] = CosZ;
}
/*類同*/
void Set_Transist_Matrix(float X, float Y,float Z) {
Transist_Matrix[3][0] = X;
Transist_Matrix[3][1] = Y;
Transist_Matrix[3][2] = Z;
}
/*類同*/
void Set_Perspective_Projection(float k) {
Perspective_Projection[2][3] = -1/k;
}
/*初始化圖形驅(qū)動(dòng)*/
void InitGraph(void) {
int gd=DETECT,gm;
initgraph(gd,gm,"E:\\TC");
}
/*生成立方體*/
float *Cube(void) {
int i, j, k;
float *pPoints1, *pPoints2;
num = 0;
for( i = -50; i = 50; i += 20)
for( j = -50; j = 50; j += 20)
for( k = -50; k = 50; k += 20)
num++;
pPoints1 = malloc( num * 4 * 4 );
pPoints2 = pPoints1;
for( i = -50; i = 50; i += 20)
for( j = -50; j = 50; j += 20)
for( k = -50; k = 50; k += 20) {
*(pPoints1++) = i;
*(pPoints1++) = j;
*(pPoints1++) = k;
*(pPoints1++) = 1;
}
return pPoints2;
}
/*Functions used for drawing Clearing*/
void Plot_NewPoints(float *pPoints) {
int i;
for(i=0;inum;i++)
putpixel( (int) (*(pPoints+i*2)), (int) (*(pPoints+i*2+1)), 7);
}
void Clear_OldPoints(float *pPoints) {
int i;
for(i=0;inum;i++)
putpixel( (int) (*(pPoints+i*2)), (int) (*(pPoints+i*2+1)), 0);
}
/*Function used for controlling*/
void Operate(int Switch, float *Ang_Rot_X, float *Ang_Rot_Y, float *Ang_Rot_Z,
float *X_Delta, float *Y_Delta, float *Z_Delta,float *Distance) {
switch(Switch) {
case X_axis_clkwise: (*Ang_Rot_X)--; break;
case X_axis_Cntclkwise: (*Ang_Rot_X)++; break;
case Y_axis_clkwise: (*Ang_Rot_Y)--; break;
case Y_axis_Cntclkwise: (*Ang_Rot_Y)++; break;
case Z_axis_clkwise: (*Ang_Rot_Z)--; break;
case Z_axis_Cntclkwise: (*Ang_Rot_Z)++; break;
case X_Delta_Plus: (*X_Delta)--; break;
case X_Delta_Minus: (*X_Delta)++; break;
case Y_Delta_Plus: (*Y_Delta)--; break;
case Y_Delta_Minus: (*Y_Delta)++; break;
case Z_Delta_Plus: (*Z_Delta)++; break;
case Z_Delta_Minus: (*Z_Delta)--; break;
case Distance_forward: (*Distance)++; break;
case Distance_Backward: (*Distance)--; break;
default: (*Ang_Rot_Y)++; break;
}
}
int main() {
int i, j, Key;
float *pMatrix1, *pMatrix2;
float *pBasePoints;
float *pPerspectivePoints;
float Ang_Rot_Xaxis, Ang_Rot_Yaxis, Ang_Rot_Zaxis;
float X_Delta, Y_Delta, Z_Delta;
float Distance;
clrscr();
InitGraph();
/*Varieties initialized*/
pBasePoints = Cube();
Ang_Rot_Xaxis = 0;
Ang_Rot_Yaxis = 0;
Ang_Rot_Zaxis = 0;
X_Delta = 0;
Y_Delta = 0;
Z_Delta = 0;
Distance = 200;
Key = 0;
while(Key != ESC) {
if( bioskey(1) )
Key = bioskey(0);
Operate(Key, Ang_Rot_Xaxis, Ang_Rot_Yaxis, Ang_Rot_Zaxis,
X_Delta, Y_Delta, Z_Delta, Distance);
SetMatrix_X(Ang_Rot_Xaxis);
SetMatrix_Y(Ang_Rot_Yaxis);
SetMatrix_Z(Ang_Rot_Zaxis);
Set_Transist_Matrix(X_Delta, Y_Delta, Z_Delta);
Set_Perspective_Projection(Distance);
/*The following may be known by you
pay your attention specially to the pair of malloc free */
pMatrix1 = Matrix_Mul( (float*)X_Rotate_Matrix, 4, 4, (float*)Y_Rotate_Matrix, 4, 4);
pMatrix2 = Matrix_Mul( pMatrix1, 4, 4, (float*)Z_Rotate_Matrix, 4, 4);
free(pMatrix1);
pMatrix1 = Matrix_Mul( pMatrix2, 4, 4, (float*)Transist_Matrix, 4, 4);
free(pMatrix2);
pMatrix2 = Matrix_Mul( pMatrix1, 4, 4, (float*)Perspective_Projection, 4, 4);
free(pMatrix1);
pMatrix1 = Matrix_Mul( pBasePoints, num, 4, pMatrix2, 4, 4);
free(pMatrix2);
Matrix_Convertion( pMatrix1, num);
pPerspectivePoints = Get_X_Y(pMatrix1, num);
Plot_NewPoints(pPerspectivePoints);
delay(5000);
Clear_OldPoints(pPerspectivePoints);
free(pPerspectivePoints);
free(pMatrix1);
}
free(pBasePoints);
closegraph();
return 0;
}
java可以做動(dòng)畫,但是不適合做動(dòng)畫。每個(gè)東西都有其使用的領(lǐng)域。
好比java適合寫應(yīng)用程序,C適合寫嵌入式程序,flash適合做動(dòng)畫,
重寫paint方法,來(lái)實(shí)現(xiàn)將自己定義的圖片繪制到組件中,然后啟動(dòng)一個(gè)線程來(lái)控制paint方法。 示例: ××××××××××××××××××××××××××× import javax.swing.*; import java.awt.*;class MyPanel extends JPanel implements Runnable {private Image img;private int i=0;private int j=0;public MyPanel(){img=new ImageIcon("1.png").getImage();}public void paint(Graphics g){g.drawImage(img,0,0,60,104,i*60,j*104,i*60+60,j*104+104,this);}public void run(){while(true){while(j {while(i {try{Thread.sleep(300);}catch(Exception e){}this.repaint();i++;}j++;i=0;}i=0;j=0;}} }public class test extends JFrame {private MyPanel p;public test(){p=new MyPanel();this.add(p,BorderLayout.CENTER);this.setBounds(300,200,300,300);this.setTitle("人物行走圖");new Thread(p).start();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String args[]){new test();} }××××××××××××××××××××××× 將以上源碼保存為:test.java,編譯,然后下載下面的圖片 將下載的圖片改名為1.png" target="_blank"
,然后將其和編譯后生成的class文件放在同一文件夾下,然后運(yùn)行就可以了·~~
效果圖
參考代碼
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?MoveTextFrame?extends?JFrame?{
JLabel?jl;//文字標(biāo)簽
int?speed=2;//移動(dòng)速度
public?MoveTextFrame()?{
jl?=?new?JLabel("文字動(dòng)畫");
jl.setForeground(Color.RED);
add(jl);
setSize(380,?100);//窗口大小
setLocationRelativeTo(null);//窗口居中
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//設(shè)置定時(shí)器,?每隔25毫秒,改變一次文字標(biāo)簽的位置
Timer?t?=?new?Timer(25,?new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
int?x?=?jl.getX()+speed;//計(jì)算移動(dòng)后的位置
if(x=390){//如果超過就指定像素,就重新從左邊開水移動(dòng)
x=-30;
}
jl.setLocation(x,?jl.getY());//更新位置
//repaint();
}
});
t.start();
}
public?static?void?main(String[]?args)?{
new?MoveTextFrame();
}
}