想用JFrame在一个时钟,时钟的背景是一个JLabel,但是时钟的针不好搞,怎么处理时钟的绘制和动画效果? 很多方法都尝试过但都不成功,高手指示. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.geom.Ellipse2D;import java.awt.geom.GeneralPath;import java.awt.geom.Line2D;import java.awt.geom.Rectangle2D;import java.util.Calendar;import java.util.Date;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.UIManager;public class Clock extends JComponent{ private static final Color INTEGRAL_COLOR = new Color(0, 128, 128); private int radius; private Calendar currentTime = Calendar.getInstance(); private int offset = 10; public Clock(int radius) { this.radius = radius; } public void setCurrentTime(Date time) { this.currentTime.setTime(time); } private void setCurrentTime(long millis) { this.currentTime.setTimeInMillis(millis); } public Dimension getPreferredSize() { Insets insets = getInsets(); return new Dimension(radius * 2 + offset*2 + insets.left + insets.right, radius * 2 + offset*2 + insets.top + insets.bottom); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Insets insets = getInsets(); g2d.translate(insets.left + radius + offset, insets.top + radius + offset); g2d.scale(1, -1); for (int i = 0; i < 60; i++) { int angle = 90 - i * 6; double pos[] = calcPos(radius, angle); paintMinuteDot(g2d, pos[0], pos[1], i % 5 == 0); } paintHourPointer(g2d); paintMinutePointer(g2d); paintSecondPointer(g2d); paintCenterPoint(g2d); g2d.scale(1, -1); g2d.translate(-insets.left - radius - offset, -insets.top - radius - offset); } private void paintCenterPoint(Graphics2D g2d) { g2d.setColor(Color.BLUE); Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4); g2d.fill(rect); } private void paintMinutePointer(Graphics2D g2d) { int minute = currentTime.get(Calendar.MINUTE); int second = currentTime.get(Calendar.SECOND); double angle = 90 - (minute + second / 60.0) * 6; Shape pointerShape = createPointerShape(radius * 0.8, radius * 0.04, radius * 0.08, angle); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); g2d.draw(pointerShape); } private void paintHourPointer(Graphics2D g2d) { int hour = currentTime.get(Calendar.HOUR); int minute = currentTime.get(Calendar.MINUTE); int second = currentTime.get(Calendar.SECOND); double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30; Shape pointerShape = createPointerShape(radius * 0.6, radius * 0.06, radius * 0.1, angle); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); g2d.draw(pointerShape); } private Shape createPointerShape(double r1, double r2, double r3, double angle) { GeneralPath gp = new GeneralPath(); double[] pos = calcPos(r1, angle); double[] pos1 = calcPos(r2, angle + 90); gp.append(new Line2D.Double(pos[0], pos[1], pos1[0], pos1[1]), true); double[] pos2 = calcPos(r3, angle + 180); gp.lineTo((float)pos2[0], (float)pos2[1]); double[] pos3 = calcPos(r2, angle + 270); gp.lineTo((float)pos3[0], (float)pos3[1]); gp.closePath(); return gp; } private void paintSecondPointer(Graphics2D g2d) { g2d.setColor(Color.BLACK); int second = currentTime.get(Calendar.SECOND); int angle = 90 - second * 6; double pos[] = calcPos(radius * 0.9, angle); double pos1[] = calcPos(radius * 0.2, angle + 180); Line2D line = new Line2D.Double(pos1[0], pos1[1], pos[0], pos[1]); g2d.draw(line); } private void paintMinuteDot(Graphics2D g2d, double x, double y, boolean flag) { g2d.setColor(flag ? Color.RED : Color.BLACK); if (flag) {// Rectangle2D rect = new Rectangle2D.Double( Ellipse2D rect = new Ellipse2D.Double( x - radius * 0.03, y - radius * 0.03, radius * 0.06, radius * 0.06); g2d.fill(rect); } else { //Rectangle2D rect = new Rectangle2D.Double( Ellipse2D rect = new Ellipse2D.Double( x - radius * 0.02, y - radius * 0.02, radius * 0.04, radius * 0.04); g2d.fill(rect); } } private double[] calcPos(double r, double angle) { double radian = Math.toRadians(angle); double x = r * Math.cos(radian); double y = r * Math.sin(radian); return new double[] {x, y}; } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } final Clock clock = new Clock(50); JFrame f = new JFrame("Clock"); f.getContentPane().add(clock, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); new Thread() { public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } clock.setCurrentTime(System.currentTimeMillis()); clock.repaint(); } } }.start(); }} frame.getContentPane().add(label, BorderLayout.CENTER);frame.setGlassPane(clock);clock.setVisible(true); 在GlassPanel中绘画:JPanel jp = (JPanel)this.getGlassPane();this.setGlassPane(jp);Graphics g = jp.getGraphics();g.drawLine(100,100,100,200);这样子为什么不行呢? 如何获得线程结束消息 多线程导入数据 ◎◎◎!!java中如何读取windows注册表的信息 Visual J++ Form Designer问题. 关于setcolor()的问题,究竟为什么???????? JAVA高手请进。。。 如何获得当前的操作系统内正在运行的应用的窗口名称列表?和当前活动窗口的名称。 关于java类名的疑问,100分 在JAVA里,一个类里可以嵌套另一个类吗? 一个比较easy的问题???, 如何获得文件创建日期? 怎样通过JNI的java端调试VC端dll中的函数
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Calendar;
import java.util.Date;import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;public class Clock extends JComponent
{
private static final Color INTEGRAL_COLOR = new Color(0, 128, 128); private int radius;
private Calendar currentTime = Calendar.getInstance(); private int offset = 10; public Clock(int radius)
{
this.radius = radius;
}
public void setCurrentTime(Date time)
{
this.currentTime.setTime(time);
}
private void setCurrentTime(long millis)
{
this.currentTime.setTimeInMillis(millis);
}
public Dimension getPreferredSize()
{
Insets insets = getInsets();
return new Dimension(radius * 2 + offset*2 + insets.left + insets.right,
radius * 2 + offset*2 + insets.top + insets.bottom);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Insets insets = getInsets();
g2d.translate(insets.left + radius + offset, insets.top + radius + offset);
g2d.scale(1, -1);
for (int i = 0; i < 60; i++) {
int angle = 90 - i * 6;
double pos[] = calcPos(radius, angle);
paintMinuteDot(g2d, pos[0], pos[1], i % 5 == 0);
} paintHourPointer(g2d);
paintMinutePointer(g2d);
paintSecondPointer(g2d);
paintCenterPoint(g2d);
g2d.scale(1, -1);
g2d.translate(-insets.left - radius - offset, -insets.top - radius - offset);
}
private void paintCenterPoint(Graphics2D g2d)
{
g2d.setColor(Color.BLUE);
Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4);
g2d.fill(rect);
} private void paintMinutePointer(Graphics2D g2d)
{
int minute = currentTime.get(Calendar.MINUTE);
int second = currentTime.get(Calendar.SECOND);
double angle = 90 - (minute + second / 60.0) * 6;
Shape pointerShape = createPointerShape(radius * 0.8, radius * 0.04, radius * 0.08, angle); g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY);
g2d.draw(pointerShape);
} private void paintHourPointer(Graphics2D g2d)
{
int hour = currentTime.get(Calendar.HOUR);
int minute = currentTime.get(Calendar.MINUTE);
int second = currentTime.get(Calendar.SECOND);
double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30;
Shape pointerShape = createPointerShape(radius * 0.6, radius * 0.06, radius * 0.1, angle); g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY);
g2d.draw(pointerShape);
} private Shape createPointerShape(double r1, double r2, double r3, double angle)
{
GeneralPath gp = new GeneralPath();
double[] pos = calcPos(r1, angle);
double[] pos1 = calcPos(r2, angle + 90);
gp.append(new Line2D.Double(pos[0], pos[1], pos1[0], pos1[1]), true);
double[] pos2 = calcPos(r3, angle + 180);
gp.lineTo((float)pos2[0], (float)pos2[1]);
double[] pos3 = calcPos(r2, angle + 270);
gp.lineTo((float)pos3[0], (float)pos3[1]); gp.closePath(); return gp;
} private void paintSecondPointer(Graphics2D g2d)
{
g2d.setColor(Color.BLACK);
int second = currentTime.get(Calendar.SECOND);
int angle = 90 - second * 6;
double pos[] = calcPos(radius * 0.9, angle);
double pos1[] = calcPos(radius * 0.2, angle + 180); Line2D line = new Line2D.Double(pos1[0], pos1[1], pos[0], pos[1]);
g2d.draw(line);
} private void paintMinuteDot(Graphics2D g2d, double x, double y, boolean flag)
{
g2d.setColor(flag ? Color.RED : Color.BLACK);
if (flag) {
// Rectangle2D rect = new Rectangle2D.Double(
Ellipse2D rect = new Ellipse2D.Double(
x - radius * 0.03, y - radius * 0.03, radius * 0.06, radius * 0.06);
g2d.fill(rect);
}
else {
//Rectangle2D rect = new Rectangle2D.Double(
Ellipse2D rect = new Ellipse2D.Double(
x - radius * 0.02, y - radius * 0.02, radius * 0.04, radius * 0.04);
g2d.fill(rect);
}
} private double[] calcPos(double r, double angle)
{
double radian = Math.toRadians(angle);
double x = r * Math.cos(radian);
double y = r * Math.sin(radian); return new double[] {x, y};
} public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} final Clock clock = new Clock(50);
JFrame f = new JFrame("Clock");
f.getContentPane().add(clock, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
new Thread() {
public void run()
{
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
clock.setCurrentTime(System.currentTimeMillis());
clock.repaint();
}
}
}.start();
}
}
frame.setGlassPane(clock);
clock.setVisible(true);
JPanel jp = (JPanel)this.getGlassPane();
this.setGlassPane(jp);
Graphics g = jp.getGraphics();
g.drawLine(100,100,100,200);这样子为什么不行呢?