继续利用所学的知识,画一个时钟,效果如下图.
本例子十分简单,就是用
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR); 获取小时
minute = c.get(Calendar.MINUTE); 获取分钟
second = c.get(Calendar.SECOND); 获取秒
然后画颜色不同的时针,分针,秒针,方向都是指向12点的位置,再调用旋转矩阵,把它们转到响应角度,角度必须计算好,最后时针的角度还得加上minute/60.0f*30.0f(计算当前分针占了百分之几小时),就大功告成啦!
主类:
package com.gl3dgame.clock; //要是想使用默认包,请去掉这行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
public class ClockMain extends JFrame {
GLRender listener=new GLRender();
static FPSAnimator animator=null;
public ClockMain() throws HeadlessException {
super("画时钟:用好旋转");
setSize(600,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GLCapabilities glcaps=new GLCapabilities();
GLCanvas canvas=new GLCanvas(glcaps);
canvas.addGLEventListener(listener);
//canvas.addMouseListener(listener);
getContentPane().add(canvas, BorderLayout.CENTER);
animator=new FPSAnimator(canvas,1,true);
centerWindow(this);
}
private void centerWindow(Component frame) { // 居中窗体
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
frame.setLocation((screenSize.width - frameSize.width) >> 1,
(screenSize.height - frameSize.height) >> 1);
}
public static void main(String[] args) {
final ClockMain app = new ClockMain();
// 显示窗体
SwingUtilities.invokeLater(new Runnable() {
public void run() {
app.setVisible(true);
}
});
// 动画线程开始
SwingUtilities.invokeLater(new Runnable() {
public void run() {
animator.start();
}
});
}
}
GL控制类:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.gl3dgame.clock;
import java.sql.Timestamp;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import java.util.Calendar;
/**
*
* @author Administrator
*/
public class GLRender implements GLEventListener {
private GL gl;
private GLU glu;
float theta = 0.0f;
float minute = 0;
float hour = 0;
float second = 0;
Timestamp ts;
String dateStr1;
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL();
glu = new GLU();
}
public void display(GLAutoDrawable drawable) {
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
second = c.get(Calendar.SECOND);
System.out.println(hour + " " + minute + " " + second);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
//------------画底盘------------//
gl.glPushMatrix();
gl.glBegin(GL.GL_TRIANGLE_FAN);
gl.glColor3f(1.0f, 1.0f, 0.0f);
for (int i = 0; i <= 390; i += 30) {
float p = (float) (i * 3.14 / 180);
gl.glVertex3f((float) Math.sin(p) / 1.5f, (float) Math.cos(p) / 1.5f, 0.0f);
}
gl.glEnd();
//------------画针盘------------//
gl.glBegin(GL.GL_TRIANGLE_FAN);
gl.glColor3f(0.4f, 0.4f, 0.0f);
for (int i = 0; i <= 365; i += 5) {
float p = (float) (i * 3.14 / 180);
gl.glVertex3f((float) Math.sin(p) / 1.6f, (float) Math.cos(p) / 1.6f, 0.0f);
}
gl.glEnd();
//------------画刻点------------//
gl.glBegin(GL.GL_POINTS);
gl.glColor3f(1.0f, 1.0f, 0.0f);
for (int i = 0; i <= 365; i += 30) {
float p = (float) (i * 3.14 / 180);
/*glBegin(GL_POLYGON);
glVertex3f((float)sin(p)/2.2,(float)cos(p)/2.2,0.0f);
glVertex3f((float)sin(p)/2.4,(float)cos(p)/2.4,0.0f);
glVertex3f((float)sin(p+0.02)/2.2,(float)cos(p+0.02)/2.2,0.0f);
glVertex3f((float)sin(p+0.02)/2.4,(float)cos(p+0.02)/2.4,0.0f);
glEnd();*/
gl.glBegin(GL.GL_LINES);
gl.glVertex3f((float) Math.sin(p) / 2.0f, (float) Math.cos(p) / 2.0f, 0.0f);
gl.glVertex3f((float) Math.sin(p) / 1.8f, (float) Math.cos(p) / 1.8f, 0.0f);
gl.glEnd();
}
gl.glEnd();
gl.glPopMatrix();
//------------画秒针------------//
gl.glPushMatrix();//矩阵入栈,防止调用函数变换后改变现有矩阵
gl.glRotatef(second/60*-360.0f, 0.0f, 0.0f, 1.0f);//实现绕坐标轴旋转
/*glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f);
glEnd ();*/
gl.glBegin(GL.GL_LINES);
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glVertex2f(0.0f, -0.1f);
gl.glVertex2f(0.0f, 0.5f);
gl.glEnd();
gl.glPopMatrix();
//------------画分针------------//
gl.glPushMatrix();
gl.glRotatef(minute/60.0f*-360.0f, 0.0f, 0.0f, 1.0f);
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex2f(0.0f, -0.07f);
gl.glVertex2f(0.0f, 0.4f);
gl.glEnd();
gl.glPopMatrix();
//------------画时针------------//
gl.glPushMatrix();
gl.glRotatef((hour/12.0f*-360.0f)-minute/60.0f*30.0f, 0.0f, 0.0f, 1.0f);
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glVertex2f(0.0f, -0.05f);
gl.glVertex2f(0.0f, 0.3f);
gl.glEnd();
gl.glPopMatrix();
//------------圆心------------//
gl.glColor3f(1.0f, 1.0f, 0.0f);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex2f(0.01f, 0.0f);
gl.glVertex2f(0.0f, 0.01f);
gl.glVertex2f(-0.01f, 0.0f);
gl.glVertex2f(0.0f, -0.01f);
gl.glEnd();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
}
本例子并不是什么死例子,,大家可以尽情修改它,,改颜色,改大小,改背景,或者加音乐,多多寻找乐趣,更有助提高.
希望大家多用例子,感谢大家的支持.
分享自己的技术论坛http://www.itneng.com/forum.php