【简答题】
本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有4个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。
import java.awt.*;
import java.awt..*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class java3
public static void main(String[]args)
JFrame frame=new TransformTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
class TransformTest extends JFrame
public TransformTestFrame()
setTitle("java3");
setSize(WIDTH,HEIGHT);
Container contentPane=getContentPane();
canvas=new TransformPanel();
contentPane.add(canvas,BorderLayout,CENTER);
JPanel buttonPanel=new JPanel();
ButtonGroup group=new ButtonGroup();
JRadioButton rotateButton
=new JRadioButton("旋转",true);
buttonPanel.add(rotateButton);
group.add(rotateButton);
rotateButton.addActionListener(new
ActionListener()
public void actionPerformed(Action )
canvas.setRotate();
);
JRadioButton translateButton
=new JRadioButton("移位",false);
buttonPanel.add(translateButton);
group.add(translateButton);
translateButton.addActionListener(new
ActionListener()
public void actionPerformed(Action )
canvas.setTranslate();
);
JRadioButton scaleButton
=new JRadioButton("放大",false);
buttonPanel.add(scaleButton);
group.add(scaleButton);
scaleButton.addActionListener(new
ActionListener()
public void actionPerformed(Action )
canvas.setScale();
);
JRadioButton shearButton
=new JRadioButton("剪切",false);
buttonPanel.add(shearButton);
group.add(shearButton);
shearButton.addActionListener(new
ActionListener()
public void actionPerformed(Action )
canvas.setShear();
);
contentPane.add(buttonPanel,BorderLayout.NORTH)
private TransformPanel canvas;
private static final int WIDTH=300;
private static final int HEIGHT=300;
class TransformPanel extend JPanel
public TransformPanel()
square=new Rectangle2D.Double(-50,-50,100,100);
t=flew AffineTransform();
setRotate();
public void paintComponent()
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.translate(getWidth()/2,getHeight()/2);
g2.setPaint(Color.gray);
g2.draw(square);
g2.transform(t);
g2.setPaint(Color.black);
g2.draw(square);
public void setRotate()
t.setToRotation(Math.toRadians(30));
repaint();
public void setTranslate()
t.setToTranslation(20,15);
repaint();
public void setScale()
t.setToScale(2.0,1.5);
repaint();
public void setShear()
t.setToShear(-0.2,0);
repaint();
private Rectangle2D square;
private AffineTransform t;
手机使用
分享
复制链接
新浪微博
分享QQ
微信扫一扫
微信内点击右上角“…”即可分享
反馈
收藏
举报
参考答案:
参考解析: