旋转一个Ellipse2D对象



我试图根据用户键输入旋转整个Ellipse2D对象。如果用户按下右键,则向右旋转,左键表示向左旋转。旋转角度设置为25。我单独画了一个drawwrotellipse否则它就会一直画原来的那个。我认为我的困惑是发生在图形和形状对象。我试过AffineTransform公司,但也没成功。我只想让它绕中心旋转。谢谢你的帮助!

class Canvas extends JPanel implements java.io.Serializable{
        int x1 = (int) (this.getWidth()/2)+100;
        int y1 = (int) (this.getHeight()/2)+20;
        int x2 = (int) this.getWidth()+100;
        int y2 = (int) this.getHeight()+200;
        @Override
        public void paintComponent(Graphics g){
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.RED);
            drawEllipse(g);
        }
        public void drawEllipse (Graphics g){
            Graphics2D g2d = (Graphics2D) g;
            myShape = new Ellipse2D.Double(x1,y1,x2,y2);
            g2d.draw(myShape);
            this.repaint();
        }
        public void drawRotEllipse (Graphics g){
            g2d.draw(myShape);
            this.repaint();
        }
    }

 private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {                                   
        if (evt.getKeyCode()==39){
            g2d.rotate(Math.toDegrees(rotAngle));
            myCanvas.drawRotEllipse(g2d);
        }
        else if (evt.getKeyCode()==37){
            g2d.rotate(Math.toDegrees(-rotAngle));
            myCanvas.drawRotEllipse(g2d);
        }
    } 
if (evt.getKeyCode()==39)

不要使用幻数。人们只看代码并不知道这意味着什么。

使用API提供的变量:

if (evt.getKeyCode() == KeyEvent.VK_RIGHT)

你的KeyEvent代码不应该做实际的绘制。所有代码应该做的就是设置类的"degrees"属性。然后,setDegrees(...)方法将负责调用repaint()。现在,每当组件被重新绘制时,形状将以其当前旋转度绘制。

下面是一个使用JSlider来改变类的旋转度的例子。

旋转图像。你应该能够改变代码旋转图像,只画你的形状:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class Rotation2 extends JPanel
{
    BufferedImage image;
    int degrees;
    int point = 250;
    public Rotation2(BufferedImage image)
    {
        this.image = image;
        setDegrees( 0 );
        setPreferredSize( new Dimension(600, 600) );
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g.create();
        double radians = Math.toRadians( degrees );
        g2.translate(point, point);
        g2.rotate(radians);
        g2.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        g.setColor(Color.RED);
        g.fillOval(point - 5, point - 5, 10, 10);
    }
    public void setDegrees(int degrees)
    {
        this.degrees = degrees;
        repaint();
    }
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
//                  String path = "mong.jpg";
                    String path = "dukewavered.gif";
                    ClassLoader cl = Rotation2.class.getClassLoader();
                    BufferedImage bi = ImageIO.read(cl.getResourceAsStream(path));
                    final Rotation2 r = new Rotation2(bi);
                    final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
                    slider.addChangeListener(new ChangeListener()
                    {
                        public void stateChanged(ChangeEvent e)
                        {
                            int value = slider.getValue();
                            r.setDegrees( value );
                        }
                    });
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(new JScrollPane(r));
                    f.add(slider, BorderLayout.SOUTH);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
                }
                catch(IOException e)
                {
                    System.out.println(e);
                }
            }
        });
    }
}

相关内容

  • 没有找到相关文章

最新更新