• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

java绘图程序中如何用菜单调用绘图方法

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);
                        br.add(menu1);
                        for (int k = 0; k < menuItems.length; k++) {
                                menu1.add(new JMenuItem(menuItems[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 = new JToggleButton(buttonName); // 实例化按钮
                        // button = new JToggleButton(buttonName);


                        button.addActionListener(buttonListener); // 增加按钮事件处理
                        buttonGroup.add(button); // 增加按钮到按钮组
                        toolBar.add(button); // 增加按钮到工具栏
                }
                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) { // 判断来自于哪个菜单
        // painter.drawShape(i); // 绘制图形
        // }
        // }
        // }
        // }
        class DrawShapeListener implements ActionListener { // 按钮事件处理
                public void actionPerformed(ActionEvent e) {
                        for (int i = 0; i < button.length; i++) {
                                if (e.getSource() == button) { // 判断来自于哪个按钮
                                        painter.drawShape(i); // 绘制图形
                                }
                        }


                }
        }


        public static void main(String[] args) {
                new PainterDemo();
        }
}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

还没学到那,现在看不懂

使用道具 举报

发新帖

发布任务需求已有1031166位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!