java – square不在我的swing应用中旋转
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – square不在我的swing应用中旋转,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3115字,纯文字阅读大概需要5分钟。
内容图文
我开发了一个小的Swing应用程序,我在其中使用单独的类组件将Square添加到我的JFrame中.现在我想在它的中心旋转这个Square,但我只看到一个完全没有旋转的静态Square.
这是我的代码……
public class Rotation extends JFrame {
Rotation() {
super("Animation of rotation about center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
add(new component());
setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}
}
class component extends JPanel implements ActionListener {
Timer timer;
Rectangle.Double r=new Rectangle.Double(100,100,50,50);
int theta=0;
component() {
timer=new Timer(10,this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
if (theta==360){
theta=0;
theta++;
}
repaint();
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.GRAY);
g2.rotate(theta);
g2.fill(r);
}
}
请有人帮我识别并解决问题.
解决方法:
您的代码中存在很多错误:
>θ应该是双倍的,应该以弧度表示,而不是度数.所以加倍:
private double theta = 0;
>您不会在计时器操作中更改theta值 – 在actionPerformed中执行此操作:
public void actionPerformed ( ActionEvent e )
{
theta += Math.PI / 18;
if ( theta >= Math.PI * 2 )
{
theta = 0;
}
repaint ();
}
>您没有指定应围绕图形上下文旋转的点.这样做(否则你的方块将围绕坐标(0; 0)的开始旋转):
public void paintComponent ( Graphics g )
{
super.paintComponent ( g );
Graphics2D g2 = ( Graphics2D ) g;
g2.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2.rotate ( theta, 125, 125 );
g2.setColor ( Color.GRAY );
g2.fill ( r );
}
>您重写组件的paint方法而不是paintComponent.总是使用paintComponent,因为它针对重绘和其他Swing的东西进行了优化,我真的不想在这里讨论,因为它是一个很大的offtopic.
>使用JPanel作为绘制简单形状的基础组件 – 使用JComponent,因为您实际上并不需要任何JPanel的功能(实际上并没有).
查看最终的工作示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @see https://stackoverflow.com/a/13051142/909085
*/
public class RotationTest extends JFrame
{
public RotationTest ()
{
super ( "Animation of rotation about center" );
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
setSize ( 400, 400 );
add ( new MyComponent () );
setVisible ( true );
}
public static void main ( String args[] )
{
SwingUtilities.invokeLater ( new Runnable ()
{
public void run ()
{
new RotationTest ();
}
} );
}
private class MyComponent extends JComponent implements ActionListener
{
private Timer timer;
private Rectangle.Double r = new Rectangle.Double ( 100, 100, 50, 50 );
private double theta = 0;
public MyComponent ()
{
super ();
timer = new Timer ( 1000 / 24, this );
timer.start ();
}
public void actionPerformed ( ActionEvent e )
{
theta += Math.PI / 18;
if ( theta >= Math.PI * 2 )
{
theta = 0;
}
repaint ();
}
public void paintComponent ( Graphics g )
{
super.paintComponent ( g );
Graphics2D g2 = ( Graphics2D ) g;
g2.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2.rotate ( theta, 125, 125 );
g2.setColor ( Color.GRAY );
g2.fill ( r );
}
}
}
正如你所看到的,我还在paint方法中添加了一个渲染提示,使方形运动变得平滑,并重构了一些代码.
内容总结
以上是互联网集市为您收集整理的java – square不在我的swing应用中旋转全部内容,希望文章能够帮你解决java – square不在我的swing应用中旋转所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。