package xatu05;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
public class PainterPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
int shape = -1; // 图案类型
Point[] point = new Point[2]; // 记录鼠标拖动的起始点和终点
public PainterPanel() {
super(); // 调用父类构造函数
this.setBackground(Color.white); // 设置背景颜色
point[0] = new Point(-1, -1); // 初始化变量
point[1] = new Point(-1, -1);
addMouseListener(this); // 增加鼠标事件
}
public void mouseReleased(MouseEvent e) { // 鼠标释放事件
point[1] = new Point(e.getX(), e.getY()); // 设置终点位置
repaint(); // 重绘屏幕
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) { // 鼠标按下时事件
point[0] = new Point(e.getX(), e.getY()); // 设置起始点位置
}
public void paint(Graphics g) {
super.paint(g);
switch (shape) { // 根据shape值绘制图形
case 0:
g.drawLine(point[0].x, point[0].y, point[1].x, point[1].y); // 绘线
break;
case 1:
int width = point[1].x - point[0].x;
int height = point[1].y - point[0].y;
g.drawOval(point[0].x, point[0].y, width, height); // 绘椭圆
break;
case 2:
int width1 = point[1].x - point[0].x;
int height1 = point[1].y - point[0].y;
g.fillOval(point[0].x, point[0].y, width1, height1); // 绘实心椭圆
break;
case 3:
width1 = point[1].x - point[0].x;
;
height = point[1].y - point[0].y;
g.drawRect(point[0].x, point[0].y, width1, height); // 绘矩形
break;
case 4:
width1 = point[1].x - point[0].x;
;
height1 = point[1].y - point[0].y;
g.fillRect(point[0].x, point[0].y, width1, height1); // 绘实心矩形
break;
}
}
public void drawShape(int shape) {
this.shape = shape;
}
}/**上面代码没有问题*/package xatu05;import java.awt.*;import java.awt.event.*;import javax.swing.*;//import javax.swing.event.*;import xatu05.PainterPanel;public class PainterDemo extends JFrame { String menuItems[][] = { {}, { "空心椭圆", "实心椭圆" }, { "空心矩形", "实心矩形" }, {} }; public void init() { JMenuBar br = new JMenuBar(); // 创建菜单工具栏 String menu[] = { "直线", "椭圆", "矩形", "多边形" }; for (int i = 0; i < menu.length; i++) { // 用数组创建Menu JMenu menu1 = new JMenu(menu[i]); br.add(menu1); for (int k = 0; k < menuItems[i].length; k++) { menu1.add(new JMenuItem(menuItems[i][k])); //注册菜单事件,增加菜单处理事件如何做 } } super.setJMenuBar(br); } JToggleButton[] button = new JToggleButton[6]; // 按钮组 PainterPanel painter = new PainterPanel(); // 绘图面板 public PainterDemo() { super("Java画图程序"); // 调用父类构造函数 init(); // String[][] menultems = {{"直线"},{"空心椭圆","实心椭圆"}}; String[] buttonName = { "直线", "空心椭圆", "实心椭圆", "空心矩形", "实心矩形", "多边形" }; // 按钮文字 DrawShapeListener buttonListener = new DrawShapeListener();// 按钮事件 JToolBar toolBar = new JToolBar(); // 实例化工具栏 ButtonGroup buttonGroup = new ButtonGroup(); // 实例化按钮组 for (int i = 0; i < button.length; i++) { button[i] = new JToggleButton(buttonName[i]); // 实例化按钮 // button[i] = new JToggleButton(buttonName[i]); button[i].addActionListener(buttonListener); // 增加按钮事件处理 buttonGroup.add(button[i]); // 增加按钮到按钮组 toolBar.add(button[i]); // 增加按钮到工具栏 } Container container = getContentPane(); // 得到窗口容器 container.add(toolBar, BorderLayout.SOUTH); // 增加组件到容器上 container.add(painter, BorderLayout.CENTER); setSize(600, 700);// 设置窗口尺寸 setLocation(500, 70); setVisible(true); // 设置窗口为可视 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序 } // class DrawShapeListener implements ActionListener { // 按钮事件处理 // public void actionPerformed(ActionEvent e) { // for (int i = 0; i < menuItems.length; i++) { // if (e.getSource() == menuItems[i]) { // 判断来自于哪个菜单 // painter.drawShape(i); // 绘制图形 // } // } // } // } class DrawShapeListener implements ActionListener { // 按钮事件处理 public void actionPerformed(ActionEvent e) { for (int i = 0; i < button.length; i++) { if (e.getSource() == button[i]) { // 判断来自于哪个按钮 painter.drawShape(i); // 绘制图形 } } } } public static void main(String[] args) { new PainterDemo(); }}
添加回答
举报
0/150
提交
取消