import?java.awt.Canvas;
目前創(chuàng)新互聯(lián)公司已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、鳳陽網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
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);
}
});
}
}
import java.awt.*;
import java.applet.*;
public class Wuerfel extends Applet {
double p[][] = new double[9][4]; //繪制正方體的頂點(diǎn)的數(shù)組
int x=1, y=2, z=3;
public void init() {
setSize(600, 480);
setBackground(new Color(255,255,255)); //背景色設(shè)置為白色
p[1][x] = -100; p[1][y] = -100; p[1][z] = -100; //下列數(shù)組為正方體的各個邊
p[2][x] = +100; p[2][y] = -100; p[2][z] = -100;
p[3][x] = +100; p[3][y] = -100; p[3][z] = +100;
p[4][x] = -100; p[4][y] = -100; p[4][z] = +100;
p[5][x] = -100; p[5][y] = +100; p[5][z] = -100;
p[6][x] = +100; p[6][y] = +100; p[6][z] = -100;
p[7][x] = +100; p[7][y] = +100; p[7][z] = +100;
p[8][x] = -100; p[8][y] = +100; p[8][z] = +100;
}
double angle_x = 0.01; //X軸旋轉(zhuǎn)速度,參數(shù)越大,轉(zhuǎn)速越大
double angle_y = 0.0075; //Y軸旋轉(zhuǎn)速度,參數(shù)越大,轉(zhuǎn)速越大
double angle_z = 0.005; //Z軸旋轉(zhuǎn)速度,參數(shù)越大,轉(zhuǎn)速越大
Image buffer;
Graphics2D gBuffer;
public void paint(Graphics g) { //繪圖方法
if (buffer==null) {//判定程序是否為初始化,即正方體是否已經(jīng)被繪制
buffer=createImage(this.getSize().width, this.getSize().height);
//獲取關(guān)于Image的Graphics2D對象,用于繪圖
gBuffer=(Graphics2D)buffer.getGraphics();
}
//清空繪圖區(qū)域
gBuffer.clearRect(0,0,this.getSize().width, this.getSize().height);
//設(shè)置圖像的表現(xiàn)模式,此處設(shè)置圖形抗鋸齒
gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//以下為匯整整個正方體
gBuffer.drawLine((int)(p[1][x])+200,(int)(p[1][y])+200,(int)(p[2][x])+200,(int)(p[2][y])+200);
gBuffer.drawLine((int)(p[2][x])+200,(int)(p[2][y])+200,(int)(p[3][x])+200,(int)(p[3][y])+200);
gBuffer.drawLine((int)(p[3][x])+200,(int)(p[3][y])+200,(int)(p[4][x])+200,(int)(p[4][y])+200);
gBuffer.drawLine((int)(p[4][x])+200,(int)(p[4][y])+200,(int)(p[1][x])+200,(int)(p[1][y])+200);
gBuffer.drawLine((int)(p[5][x])+200,(int)(p[5][y])+200,(int)(p[6][x])+200,(int)(p[6][y])+200);
gBuffer.drawLine((int)(p[6][x])+200,(int)(p[6][y])+200,(int)(p[7][x])+200,(int)(p[7][y])+200);
gBuffer.drawLine((int)(p[7][x])+200,(int)(p[7][y])+200,(int)(p[8][x])+200,(int)(p[8][y])+200);
gBuffer.drawLine((int)(p[8][x])+200,(int)(p[8][y])+200,(int)(p[5][x])+200,(int)(p[5][y])+200);
gBuffer.drawLine((int)(p[1][x])+200,(int)(p[1][y])+200,(int)(p[5][x])+200,(int)(p[5][y])+200);
gBuffer.drawLine((int)(p[2][x])+200,(int)(p[2][y])+200,(int)(p[6][x])+200,(int)(p[6][y])+200);
gBuffer.drawLine((int)(p[3][x])+200,(int)(p[3][y])+200,(int)(p[7][x])+200,(int)(p[7][y])+200);
gBuffer.drawLine((int)(p[4][x])+200,(int)(p[4][y])+200,(int)(p[8][x])+200,(int)(p[8][y])+200);
g.drawImage (buffer, 0, 0, this);//完成整個空間的繪制
try {
//線程休眠10毫秒,此處的作用相當(dāng)于給旋轉(zhuǎn)正方體一個時間間隔,若不休眠,正方體的旋轉(zhuǎn)式不容易看出的
Thread.sleep(10);
} catch (InterruptedException e) {}
double px, py, pz; //正方體頂點(diǎn)旋轉(zhuǎn)后坐標(biāo)
for (int i=1;i9;i++) {
px = p[i][x]; //頂點(diǎn)旋轉(zhuǎn)前X坐標(biāo)
py = p[i][y]; //頂點(diǎn)旋轉(zhuǎn)前Y坐標(biāo)
pz = p[i][z]; //頂點(diǎn)旋轉(zhuǎn)前Z坐標(biāo)
//X軸旋轉(zhuǎn)變換
p[i][y] = py*Math.cos(angle_x)-pz*Math.sin(angle_x);
p[i][z] = py*Math.sin(angle_x)+pz*Math.cos(angle_x);
py = p[i][y]; //X軸變換后坐標(biāo)
pz = p[i][z];
//Y軸旋轉(zhuǎn)變換
p[i][x] = px*Math.cos(angle_y)+pz*Math.sin(angle_y);
p[i][z] =-px*Math.sin(angle_y)+pz*Math.cos(angle_y);
px = p[i][x]; //變換后坐標(biāo)
//Z軸旋轉(zhuǎn)變換
p[i][x] = px*Math.cos(angle_z)-py*Math.sin(angle_z);
p[i][y] = py*Math.cos(angle_z)+px*Math.sin(angle_z);
}
repaint(); //重繪,即重新調(diào)用paint方法
}
public void update(Graphics g) {paint(g);}
}
這個類最核心的地方還是在paint方法,paint中最核心的部分又是關(guān)于坐標(biāo)的旋轉(zhuǎn)變換和sleep休眠,整個程序看下來還是不難理解的.
程序通過二維數(shù)組存儲正方體的頂點(diǎn)坐標(biāo),初次繪制,然后調(diào)用線程重復(fù)旋轉(zhuǎn)變換頂點(diǎn)坐標(biāo),然后重繪變換后的頂點(diǎn)坐標(biāo).因為有sleep,所以每次時間間隔后重繪的圖像會和前一圖像連續(xù)形成整體旋轉(zhuǎn)動畫。
關(guān)于旋轉(zhuǎn)速度參數(shù)很重要,整個程序可以通過改變它來控制轉(zhuǎn)速。
可以用JAVA中的Calendar。
⑴Calendar是java.util.* 中的一個類,所以在使用前,我們要導(dǎo)入包java.util.*;
//產(chǎn)生一個日歷類的對象,我們不能使用new Calendar()來構(gòu)造實例,因為//Calendar的構(gòu)造函數(shù)都是protected,也就是說,構(gòu)造函數(shù)是保護(hù)成員。
⑵關(guān)于Calendar的Filed
在Calendar中定義了日歷的各個成員,比如:年,月,日,包括不同的記錄方法,比如:同一天(2005-12-14)來說,在記錄的時候,我們可以說是一周中的周三,是12月的第14天,是2005年的第****(你可以自己算一下)天。這就是不同的記錄方法,所以在Calendar中就有了不同的定義:
public final static int ERA = 0;
public final static int YEAR = 1;
public final static int MONTH = 2;
public final static int WEEK_OF_YEAR = 3;
public final static int WEEK_OF_MONTH = 4;
public final static int DATE = 5;
public final static int DAY_OF_MONTH = 5;
public final static int DAY_OF_YEAR = 6;
public final static int DAY_OF_WEEK = 7;
public final static int DAY_OF_WEEK_IN_MONTH = 8;
public final static int AM_PM = 9;
public final static int HOUR = 10;
public final static int HOUR_OF_DAY = 11;
public final static int MINUTE = 12;
public final static int SECOND = 13;
這些定義是不同記錄方法的標(biāo)識,在此我沒有列舉完全,感興趣的話你可以看一下Calendar的源代碼。
這些就是Calendar的Field,根據(jù)你的不同需求就可以去處不同的Field,不同的記錄方法也是不同的的Filed,這樣應(yīng)該是好理解一些。
有了這些,我們就可以取得你想要的任何需要的東西了,如果你想知道今天是一年中的哪一天,就可以用:
int day = calendar.get(Calendar.DAY_OF_YEAR);
參數(shù)就是你想取得的Field,所有的這些都在Calendar中定義好了。
結(jié)果會告訴我們2005-12-14是2005年的第348天。
然后你想知道30天后是幾號呢?
calendar.set(Calendar.DAY_OF_YEAR, day + 30);
在這,你不需要去考慮12-14號加上30天是一月,是不是會出錯,不會的,Calendar自己知道那一天是幾月幾號。
所以你再取得其日期的時候就可以知道其日期了。當(dāng)然你也可以直接通過Calendar來取得。
其他的我就不一一說了,是一樣的道理。這樣一來,我們就可以使用Calendar實現(xiàn)日期的隨意加減了。
Thread.sleep(40); 隔40毫秒重畫(repaint),這么快,當(dāng)然一閃一閃,你把時間放長或者去掉sleep吧
因為你使用system.out.println這個方法輸出個對象的時候,因為只能輸出字符串,所以系統(tǒng)就會默認(rèn)調(diào)用類中的toStringf方法,因為這個方法是源自object類的,所以幾乎所有的類都有這個方法。而你的代碼重寫了這個方法,所以輸出的時候就調(diào)用了這個方法,然后按你的規(guī)則進(jìn)行了輸出。
比如
public?class?Test{
public?Test(){}
public?void?toString(){
System.out.println("----test------");
}
//假設(shè):Test?test=new?Test();
//???System.out.println(test);?
//輸出的結(jié)果就是"----test------"
}
1.GroupLayout layout=new GroupLayout(c);
創(chuàng)建一個GroupLayout的對象,同時創(chuàng)建時需要以一個Container的對象作為參數(shù)
2 layout.setAutoCreateContainerGaps(true);
setAutoCreateContainerGaps(boolean)是用來設(shè)置是否應(yīng)該自動創(chuàng)建容器與觸到容器邊框的組件之間的間隙,參數(shù)是一個boolean值,true代表是,false代表否。
3. GroupLayout.ParallelGroup hpg2a=layout.createParallelGroup(GroupLayout.Alignment.LEADING);
ParallelGroup 是GroupLayout的一個內(nèi)部類,所以要創(chuàng)建他的實例必須是這樣的:GroupLayout.ParallelGroup
createParallelGroup(GroupLayout.Alignment alignment)方法使用 Alignment.LEADING 的對齊方式創(chuàng)建并返回一個 ParallelGroup,Alignment.LEADING是GroupLayout的一個field,在這里以這個作為參數(shù)。 這句話就是一個簡單的語句,不能再簡單里,你看里面雖然有很多點(diǎn),但其實只有一個層次就相當(dāng)于 A a = b.c(o);
4. hpg2a.addComponent(cb2);
將 Component 對象作為參數(shù)添加到此 Group。就是給GroupLayout.ParallelGroup添加一個cb2組件
總結(jié):你對api很不了解,這些東西都是在api里面可以查到的,所以你應(yīng)該把a(bǔ)pi下載下來,放在自己的桌面上隨時查閱,學(xué)java 的時候api文檔是必不可少的工具哦,加油!