三、綜合應用題
本題的功能是對圖形進行操作,包括旋轉、移位、放大和剪切。窗口中有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;
}
相關推薦:
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內蒙古 |