在360度移动一条线。
这是我的代码
import java.applet.*; import java.awt.*; import java.util.*; public class HelloWorldApplet extends Applet implements Runnable{ Insets insets; Thread th; int x; Image bakground; public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); g.drawLine(x,10,90,90); } public void init() { bakground=getImage(getCodeBase(),"clock"); } @Override public void start() { if(th==null){ th=new Thread(this); th.start(); } } @Override public void run() { // TODO Auto-generated method stub for (x = 90;x <=450; x+=5) { repaint(); try{ th.sleep(100); } catch(Exception r){ } } } }它旋转但不是360度旋转,线条变得越来越大。 我想像模拟时钟一样为线条设置动画。
Move a line in 360 degree .
here is my code
import java.applet.*; import java.awt.*; import java.util.*; public class HelloWorldApplet extends Applet implements Runnable{ Insets insets; Thread th; int x; Image bakground; public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); g.drawLine(x,10,90,90); } public void init() { bakground=getImage(getCodeBase(),"clock"); } @Override public void start() { if(th==null){ th=new Thread(this); th.start(); } } @Override public void run() { // TODO Auto-generated method stub for (x = 90;x <=450; x+=5) { repaint(); try{ th.sleep(100); } catch(Exception r){ } } } }it rotates but instead of rotating in 360 degree the line become larger and larger. I want to animate the line like an analog Clock.
最满意答案
你可以使用数学的魔力特别是三角学来计算你的线在圆上的终点。 但最简单的方法是使用仿射变换。 看看这个paint()方法
public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); Graphics2D gg = (Graphics2D) g.create(); // create new layer and cast to advanced Graphics2d gg.rotate(Math.toRadians(x), 90,90); // rotate new layer at center position 90,90 around x degrees gg.drawLine(90,90,0,0); // draw line to center position gg.dispose(); // push new layer back }You can use the magic of math particularly trigonometry to calc the end points of your line being on a circle. But the simplest way would be to use affine transformation. Have a look at this paint() method
public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); Graphics2D gg = (Graphics2D) g.create(); // create new layer and cast to advanced Graphics2d gg.rotate(Math.toRadians(x), 90,90); // rotate new layer at center position 90,90 around x degrees gg.drawLine(90,90,0,0); // draw line to center position gg.dispose(); // push new layer back }如何在java小程序中像模拟时钟一样以360度为线条设置动画(How to animate a line in 360 degree like analog clock in java applets)在360度移动一条线。
这是我的代码
import java.applet.*; import java.awt.*; import java.util.*; public class HelloWorldApplet extends Applet implements Runnable{ Insets insets; Thread th; int x; Image bakground; public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); g.drawLine(x,10,90,90); } public void init() { bakground=getImage(getCodeBase(),"clock"); } @Override public void start() { if(th==null){ th=new Thread(this); th.start(); } } @Override public void run() { // TODO Auto-generated method stub for (x = 90;x <=450; x+=5) { repaint(); try{ th.sleep(100); } catch(Exception r){ } } } }它旋转但不是360度旋转,线条变得越来越大。 我想像模拟时钟一样为线条设置动画。
Move a line in 360 degree .
here is my code
import java.applet.*; import java.awt.*; import java.util.*; public class HelloWorldApplet extends Applet implements Runnable{ Insets insets; Thread th; int x; Image bakground; public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); g.drawLine(x,10,90,90); } public void init() { bakground=getImage(getCodeBase(),"clock"); } @Override public void start() { if(th==null){ th=new Thread(this); th.start(); } } @Override public void run() { // TODO Auto-generated method stub for (x = 90;x <=450; x+=5) { repaint(); try{ th.sleep(100); } catch(Exception r){ } } } }it rotates but instead of rotating in 360 degree the line become larger and larger. I want to animate the line like an analog Clock.
最满意答案
你可以使用数学的魔力特别是三角学来计算你的线在圆上的终点。 但最简单的方法是使用仿射变换。 看看这个paint()方法
public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); Graphics2D gg = (Graphics2D) g.create(); // create new layer and cast to advanced Graphics2d gg.rotate(Math.toRadians(x), 90,90); // rotate new layer at center position 90,90 around x degrees gg.drawLine(90,90,0,0); // draw line to center position gg.dispose(); // push new layer back }You can use the magic of math particularly trigonometry to calc the end points of your line being on a circle. But the simplest way would be to use affine transformation. Have a look at this paint() method
public void paint (Graphics g){ super.paint(g); g.setColor(Color.BLACK); Graphics2D gg = (Graphics2D) g.create(); // create new layer and cast to advanced Graphics2d gg.rotate(Math.toRadians(x), 90,90); // rotate new layer at center position 90,90 around x degrees gg.drawLine(90,90,0,0); // draw line to center position gg.dispose(); // push new layer back }
发布评论