题目内容
本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有4个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class java3 { public static void ma
提问人:陈俭龙发布时间:2020-06-28
本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有4个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。 import java.awt.*; import java.awt.event.*; 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_ 0N_CLoSE); frame.show; } } class TransformTest extends JFrame { public TransformTestFrame { setTitle("java3"); setSize(WIDTH,HEIGHT); Container contentPane=getContentPane; canvas=new TransformPanel; contentPane.add(canvas,BorderLayout.CEN- TER); 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(ActionEvent event) { canvas.setRotate; } }); JRadioButton trans|ateButton =new JRadioButton("移位",false); buttonPanel.add(translateButton); group.add(translateButton); translateButton.addActionListener(new ActionLiatener public void actionPerformed(ActionEvent event) { canvas.setTranslate; } }); JRadioButton scaleButton =new JRadioButton("放大",false); buttonPanel.add(scaleButton): group.add(scaleButton); scaleButton.addActionListener(new ActionListener { public void actionPerformed(ActionEvent event) { canvas.setScale; } }); JRadioButton shearButton =new JRadioButton("剪切",false); buttonPanel.add(shearButton): group.add(shearButton); shearButton.addActionListener(new ActionListener { public void actionPerformed(ActionEvent event) { 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 = new AffineTransform; setRotate; } public void paintComponent { super.paintComponent(g); Graphics2D g2=(Graphies2D)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,1 5); repaint; } public void setScale { t.setToScale(2.0,1.5): repaint; } public void setShear { t.setToShear(-0.2,O); repaint; } private Rectangle2D square; private AffineTransform. t; }
点击加载更多评论>>