📄 drawingshapes.java
字号:
//********************************************************************
// DrawingShapes.java Author: XieXing
//
// Application allows user to draw lines, rectangles and ovals and
// choose the color of the drawn shape.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingShapes extends JFrame
{
// 绘图工具工具条
private JToolBar controlsBar;
//按钮组以实现按钮只能单选
private ButtonGroup buttonGroup;
// 颜色设置按钮
private JButton colorButton;
// 填充按钮
private JToggleButton fillButton;
// 绘图区
private DrawingPanel paintPanel;
// 绘图的形状类型
private String[] shapeTypes = { "Line", "Rect", "Oval" ,"Circle" };
//绘图图形选择按钮
private JToggleButton[] button = new JToggleButton[shapeTypes.length];
//图标按钮图像
private Icon[] icons;
//鼠标移动到相应按钮上面上停留时给出的提示说明条
private String tipText[]={"画直线","画矩形","画椭圆","画圆","选择当前绘画的颜色","绘制填充图形"};
// 构造函数
public DrawingShapes()
{
super("微型绘图大师");
// 创建容器
Container contentPane = getContentPane();
// 组件布局设置
contentPane.setLayout( null );
// 建立controlsBar面板
controlsBar = new JToolBar();
controlsBar.setBounds( 0, 0, 600, 54);
controlsBar.setLayout( null );
contentPane.add( controlsBar );
// 建立paintPanel面板
paintPanel = new DrawingPanel();
paintPanel.setBounds( 0, 54, 600, 560 );
paintPanel.setBackground( Color.WHITE );
contentPane.add( paintPanel );
// 绘图按钮组
ButtonGroup buttonGroup = new ButtonGroup();
for(int i=0;i<button.length;i++)
{
icons = new ImageIcon[shapeTypes.length];
icons[i] = new ImageIcon(shapeTypes[i] + ".jpg");
button[i] = new JToggleButton(icons[i]);
button[i].setBounds( 20+80*i, 3, 48, 48 );
button[i].setToolTipText(tipText[i]);
button[i].addActionListener(
new ActionListener() // 匿名内部类
{
// 响应组合框事件,选择绘图形状类型
public void actionPerformed( ActionEvent event )
{
buttonActionPerformed( event );
}
}
);
buttonGroup.add(button[i]);
controlsBar.add(button[i]);
}
// 建立颜色设置按钮colorButton
colorButton = new JButton(new ImageIcon("Color.jpg"));
colorButton.setBounds( 400, 3, 48, 48 );
colorButton.setToolTipText(tipText[4]);
controlsBar.add(colorButton);
colorButton.addActionListener(
new ActionListener() // 匿名内部类
{
// 响应颜色按钮按下
public void actionPerformed( ActionEvent event )
{
colorButtonActionPerformed( event );
}
}
);
// 建立颜色设置按钮colorButton
fillButton = new JToggleButton(new ImageIcon("fill.jpg"));
fillButton.setBounds( 480, 3, 48, 48 );
fillButton.setToolTipText(tipText[5]);
controlsBar.add(fillButton);
fillButton.addActionListener(
new ActionListener() // 匿名内部类
{
// 响应颜色按钮按下
public void actionPerformed( ActionEvent event )
{
fillButtonActionPerformed( event );
}
}
);
// 设置窗口属性
setSize( 600, 500 ); // 设置窗口尺寸
setVisible( true ); // 显示窗口
}
// 颜色设置对话框,下一绘图图形颜色选择
private void fillButtonActionPerformed( ActionEvent event )
{
JToggleButton toggle = (JToggleButton)event.getSource();
if(toggle.isSelected())
{
paintPanel.setfillShape(true);
}
else
{
paintPanel.setfillShape(false);
}
}
// 颜色设置对话框,下一绘图图形颜色选择
private void colorButtonActionPerformed( ActionEvent event )
{
Color selection = JColorChooser.showDialog( this,
"颜色设置", Color.BLACK );
if ( selection != null )
{
colorButton.setBackground( selection );
paintPanel.setCurrentColor( selection );
}
}
// 选择绘图形状类型
private void buttonActionPerformed( ActionEvent event )
{
for(int i=0;i<button.length;i++)
{
if (event.getSource() == button[i])
paintPanel.setCurrentShapeType(i);
}
}
// main方法
public static void main( String args[] )
{
DrawingShapes drawShape = new DrawingShapes();
drawShape.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -