本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有四个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。 import java.awt.*; import java

admin2012-12-02  51

问题 本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有四个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。
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_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(ActionEvent event)
      {
         canvas.setRotate();
      }
   });
JRadioButton translateButton
   = new JRadioButton("移位", false);
buttonPanel.add(translateButton);
group.add(translateButton);
translateButton.addActionListener(new
   ActionListener()
   {
      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 Rectangle2@D@Double(-50, -50, 100, 100);
t = new 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;
}

选项

答案第1处:class TransformTestFrame extends JFrame第2处:class TransformPanel extends Jframe第3处:public void paintComponent(Graphics g)

解析 第1处类中公有对象名称应与类名一致;第2处为继承的父类不正确;第3处从下面的super.paintComponent(g);可判断出是应有参数Graphics g。
转载请注明原文地址:https://jikaoti.com/ti/pbkiFFFM
0

最新回复(0)