扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.awt.Canvas;
目前创新互联公司已为1000多家的企业提供了网站建设、域名、网站空间、绵阳服务器托管、企业网站设计、凤阳网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
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);
}
});
}
}
import java.awt.*;
import java.applet.*;
public class Wuerfel extends Applet {
double p[][] = new double[9][4]; //绘制正方体的顶点的数组
int x=1, y=2, z=3;
public void init() {
setSize(600, 480);
setBackground(new Color(255,255,255)); //背景色设置为白色
p[1][x] = -100; p[1][y] = -100; p[1][z] = -100; //下列数组为正方体的各个边
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轴旋转速度,参数越大,转速越大
double angle_y = 0.0075; //Y轴旋转速度,参数越大,转速越大
double angle_z = 0.005; //Z轴旋转速度,参数越大,转速越大
Image buffer;
Graphics2D gBuffer;
public void paint(Graphics g) { //绘图方法
if (buffer==null) {//判定程序是否为初始化,即正方体是否已经被绘制
buffer=createImage(this.getSize().width, this.getSize().height);
//获取关于Image的Graphics2D对象,用于绘图
gBuffer=(Graphics2D)buffer.getGraphics();
}
//清空绘图区域
gBuffer.clearRect(0,0,this.getSize().width, this.getSize().height);
//设置图像的表现模式,此处设置图形抗锯齿
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毫秒,此处的作用相当于给旋转正方体一个时间间隔,若不休眠,正方体的旋转式不容易看出的
Thread.sleep(10);
} catch (InterruptedException e) {}
double px, py, pz; //正方体顶点旋转后坐标
for (int i=1;i9;i++) {
px = p[i][x]; //顶点旋转前X坐标
py = p[i][y]; //顶点旋转前Y坐标
pz = p[i][z]; //顶点旋转前Z坐标
//X轴旋转变换
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轴变换后坐标
pz = p[i][z];
//Y轴旋转变换
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]; //变换后坐标
//Z轴旋转变换
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(); //重绘,即重新调用paint方法
}
public void update(Graphics g) {paint(g);}
}
这个类最核心的地方还是在paint方法,paint中最核心的部分又是关于坐标的旋转变换和sleep休眠,整个程序看下来还是不难理解的.
程序通过二维数组存储正方体的顶点坐标,初次绘制,然后调用线程重复旋转变换顶点坐标,然后重绘变换后的顶点坐标.因为有sleep,所以每次时间间隔后重绘的图像会和前一图像连续形成整体旋转动画。
关于旋转速度参数很重要,整个程序可以通过改变它来控制转速。
可以用JAVA中的Calendar。
⑴Calendar是java.util.* 中的一个类,所以在使用前,我们要导入包java.util.*;
//产生一个日历类的对象,我们不能使用new Calendar()来构造实例,因为//Calendar的构造函数都是protected,也就是说,构造函数是保护成员。
⑵关于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;
这些定义是不同记录方法的标识,在此我没有列举完全,感兴趣的话你可以看一下Calendar的源代码。
这些就是Calendar的Field,根据你的不同需求就可以去处不同的Field,不同的记录方法也是不同的的Filed,这样应该是好理解一些。
有了这些,我们就可以取得你想要的任何需要的东西了,如果你想知道今天是一年中的哪一天,就可以用:
int day = calendar.get(Calendar.DAY_OF_YEAR);
参数就是你想取得的Field,所有的这些都在Calendar中定义好了。
结果会告诉我们2005-12-14是2005年的第348天。
然后你想知道30天后是几号呢?
calendar.set(Calendar.DAY_OF_YEAR, day + 30);
在这,你不需要去考虑12-14号加上30天是一月,是不是会出错,不会的,Calendar自己知道那一天是几月几号。
所以你再取得其日期的时候就可以知道其日期了。当然你也可以直接通过Calendar来取得。
其他的我就不一一说了,是一样的道理。这样一来,我们就可以使用Calendar实现日期的随意加减了。
Thread.sleep(40); 隔40毫秒重画(repaint),这么快,当然一闪一闪,你把时间放长或者去掉sleep吧
因为你使用system.out.println这个方法输出个对象的时候,因为只能输出字符串,所以系统就会默认调用类中的toStringf方法,因为这个方法是源自object类的,所以几乎所有的类都有这个方法。而你的代码重写了这个方法,所以输出的时候就调用了这个方法,然后按你的规则进行了输出。
比如
public class Test{
public Test(){}
public void toString(){
System.out.println("----test------");
}
//假设:Test test=new Test();
// System.out.println(test);
//输出的结果就是"----test------"
}
1.GroupLayout layout=new GroupLayout(c);
创建一个GroupLayout的对象,同时创建时需要以一个Container的对象作为参数
2 layout.setAutoCreateContainerGaps(true);
setAutoCreateContainerGaps(boolean)是用来设置是否应该自动创建容器与触到容器边框的组件之间的间隙,参数是一个boolean值,true代表是,false代表否。
3. GroupLayout.ParallelGroup hpg2a=layout.createParallelGroup(GroupLayout.Alignment.LEADING);
ParallelGroup 是GroupLayout的一个内部类,所以要创建他的实例必须是这样的:GroupLayout.ParallelGroup
createParallelGroup(GroupLayout.Alignment alignment)方法使用 Alignment.LEADING 的对齐方式创建并返回一个 ParallelGroup,Alignment.LEADING是GroupLayout的一个field,在这里以这个作为参数。 这句话就是一个简单的语句,不能再简单里,你看里面虽然有很多点,但其实只有一个层次就相当于 A a = b.c(o);
4. hpg2a.addComponent(cb2);
将 Component 对象作为参数添加到此 Group。就是给GroupLayout.ParallelGroup添加一个cb2组件
总结:你对api很不了解,这些东西都是在api里面可以查到的,所以你应该把api下载下来,放在自己的桌面上随时查阅,学java 的时候api文档是必不可少的工具哦,加油!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流