社区服务 统计排行 帮助

广播台

      • 2200阅读
      • 1回复
      冷月萧遥 离线

      级别: 新手上路

      显示用户信息 

      倒序阅读, 只看楼主, 楼主   发表于: 2009-06-19
      更多操作

      java写的画图工具

      本帖被 admin 设置为精华(2009-06-21)
      package mypainter;

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class Main
      {
          public static void main(String[] args){
              Font font=new Font("宋体",Font.PLAIN,12);
              UIManager.put("Menu.font",font);
              UIManager.put("MenuItem.font",font);
              UIManager.put("Button.font",font);
              UIManager.put("Label.font",font);
              UIManager.put("RadioButton.font",font);
              UIManager.put("CheckBox.font",font);
              UIManager.put("ComboBox.font",font);
              
              PaintFrame frame=new PaintFrame();
              frame.setVisible(true);
          }
      }

      class PaintFrame extends JFrame implements ActionListener
      {
          private JPanel leftPanel;
          private JPanel bottomPanel;
          JLabel typeLabel;
          JLabel sizeLabel;
          private JButton redButton;
          private JButton greenButton;
          private JButton blueButton;
          private JButton blackButton;
          private JButton yellowButton;
          private JButton otherButton;
          private JButton eraseButton;

          private JRadioButton lineButton;
          private JRadioButton rectButton;
          private JRadioButton arcButton;
          private JRadioButton ovalButton;
          private ButtonGroup group;

          private JComboBox lineSize;
          private JCheckBox fillFlag;

          private JMenuBar menubar;
          private JMenu fileMenu;
          private JMenu helpMenu;
          private JMenuItem newItem;
          private JMenuItem openItem;
          private JMenuItem closeItem;
          private JMenuItem saveItem;
          private JMenuItem saveAsItem;
          private JMenuItem exitItem;
          private JMenuItem usageItem;
          private JMenuItem aboutItem;

          private int width;
          private int height;
          private int scrWidth;
          private int scrHeight;

          private PaintPanel  paintPanel;

          public PaintFrame(){
              super("我的绘图程序");

              initMenu();

              width=800;
              height=600;
              this.setSize(width,height);
              Toolkit toolkit=this.getToolkit();
              scrWidth=toolkit.getScreenSize().width;
              scrHeight=toolkit.getScreenSize().height;
              this.setLocation((scrWidth-width)/2,(scrHeight-height)/2);
              this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              this.setResizable(false);
              
              Container container=this.getContentPane();
              container.setLayout(new BorderLayout());

              leftPanel=new JPanel();
              leftPanel.setLayout(new GridLayout(8,1));
              typeLabel=new JLabel("请选择图案种类:");
              lineButton=new JRadioButton("直线");
              lineButton.setSelected(true);
              lineButton.addActionListener(this);
              rectButton=new JRadioButton("矩形");
              rectButton.addActionListener(this);
              arcButton=new JRadioButton("椭圆弧");
              arcButton.addActionListener(this);
              ovalButton=new JRadioButton("椭圆");
              ovalButton.addActionListener(this);
              group=new ButtonGroup();
              group.add(lineButton);
              group.add(rectButton);
              group.add(arcButton);
              group.add(ovalButton);
              fillFlag=new JCheckBox("实心");
              fillFlag.addActionListener(this);
              sizeLabel=new JLabel("请指定线宽:");
              lineSize=new JComboBox();
              lineSize.addItem("1");
              lineSize.addItem("3");
              lineSize.addItem("5");
              lineSize.addItem("10");
              lineSize.setEditable(true);
              lineSize.addActionListener(this);
              leftPanel.add(typeLabel);
              leftPanel.add(lineButton);
              leftPanel.add(rectButton);
              leftPanel.add(arcButton);
              leftPanel.add(ovalButton);
              leftPanel.add(fillFlag);
              leftPanel.add(sizeLabel);
              leftPanel.add(lineSize);
              container.add(leftPanel,BorderLayout.WEST);

              bottomPanel=new JPanel();
              bottomPanel.setLayout(new GridLayout(1,7));
              redButton=new JButton();
              redButton.setBackground(Color.RED);
              redButton.addActionListener(this);
              greenButton=new JButton();
              greenButton.setBackground(Color.GREEN);
              greenButton.addActionListener(this);
              blueButton=new JButton();
              blueButton.setBackground(Color.BLUE);
              blueButton.addActionListener(this);
              blackButton=new JButton();
              blackButton.setBackground(Color.BLACK);
              blackButton.addActionListener(this);
              yellowButton=new JButton();
              yellowButton.setBackground(Color.YELLOW);
              yellowButton.addActionListener(this);
              otherButton=new JButton("其它");
              otherButton.addActionListener(this);
              eraseButton=new JButton("擦除");
              eraseButton.addActionListener(this);
              bottomPanel.add(redButton);
              bottomPanel.add(greenButton);
              bottomPanel.add(blueButton);
              bottomPanel.add(blackButton);
              bottomPanel.add(yellowButton);
              bottomPanel.add(otherButton);
              bottomPanel.add(eraseButton);
              container.add(bottomPanel,BorderLayout.SOUTH);

              paintPanel=new PaintPanel();
              container.add(paintPanel,BorderLayout.CENTER);
          }

          private void initMenu(){
              menubar=new JMenuBar();

              fileMenu=new JMenu("文件");
              newItem=new JMenuItem("新建");
              newItem.addActionListener(this);
              openItem=new JMenuItem("打开");
              closeItem=new JMenuItem("关闭");
              saveItem=new JMenuItem("保存");
              saveAsItem=new JMenuItem("另存为");
              exitItem=new JMenuItem("退出");
              exitItem.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                      dispose();
                      System.exit(0);
                  }
              });
              fileMenu.add(newItem);
              fileMenu.add(openItem);
              fileMenu.add(closeItem);
              fileMenu.addSeparator();
              fileMenu.add(saveItem);
              fileMenu.add(saveAsItem);
              fileMenu.addSeparator();
              fileMenu.add(exitItem);

              helpMenu=new JMenu("帮助");
              usageItem=new JMenuItem("用法");
              aboutItem=new JMenuItem("关于");
              aboutItem.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                      JOptionPane.showMessageDialog(null,"<html><body><h2>简易绘图程序</h2><h3>连云港职业技术学院</h3></body></html>","关于MyPainter",JOptionPane.INFORMATION_MESSAGE);
                  }
              });
              helpMenu.add(usageItem);
              helpMenu.add(aboutItem);

              menubar.add(fileMenu);
              menubar.add(helpMenu);
              this.setJMenuBar(menubar);
          }

          public void actionPerformed(ActionEvent e){
              if(e.getSource()==redButton){
                  paintPanel.setColor(Color.RED);
              }
              else if(e.getSource()==greenButton){
                  paintPanel.setColor(Color.GREEN);
              }
              else if(e.getSource()==blueButton){
                  paintPanel.setColor(Color.BLUE);
              }
              else if(e.getSource()==blackButton){
                  paintPanel.setColor(Color.BLACK);
              }
              else if(e.getSource()==yellowButton){
                  paintPanel.setColor(Color.YELLOW);
              }
              else if(e.getSource()==otherButton){
                  paintPanel.setColor(JColorChooser.showDialog(this,"请指定要用的绘图颜色",paintPanel.getColor()));
              }
              else if(e.getSource()==lineButton){
                  paintPanel.setType(1);
              }
              else if(e.getSource()==rectButton){
                  paintPanel.setType(2);
              }
              else if(e.getSource()==arcButton){
                  paintPanel.setType(3);
              }
              else if(e.getSource()==ovalButton){
                  paintPanel.setType(4);
              }
              else if(e.getSource()==lineSize){
                  paintPanel.setLineSize(Integer.parseInt((String)lineSize.getSelectedItem()));
              }
              else if(e.getSource()==fillFlag){
                  paintPanel.setFillFlag(fillFlag.isSelected());
              }
              else if(e.getSource()==eraseButton){
                  paintPanel.clear();
              }
          }
      }

      class PaintPanel extends JPanel
      {
          private int x1;
          private int y1;
          private int x2;
          private int y2;
          private boolean flag=false;

          private int type=1;
          private Color color=Color.GREEN;
          private int size=1;
          private boolean fillFlag=false;
          private boolean clearFlag=false;


          public PaintPanel(){
              this.addMouseListener(new MouseAdapter(){
                  public void mousePressed(MouseEvent e){
                      x1=e.getX();
                      y1=e.getY();
                  }
                  public void mouseReleased(MouseEvent e){
                      x2=e.getX();
                      y2=e.getY();
                      flag=true;
                      repaint();
                  }
              });
          }

          public void paintComponent(Graphics g){
              if(clearFlag){
                  super.paintComponent(g);
                  clearFlag=false;
              }
              if(flag==true){
                  Graphics2D g2d=(Graphics2D)g;
                  g2d.setColor(color);
                  g2d.setStroke(new BasicStroke(size));
                  switch(type){
                      case 1:{
                          g2d.drawLine(x1, y1, x2, y2);
                          break;
                      }
                      case 2:{
                          int left=(x1<x2?x1:x2);
                          int top=(y1<y2?y1:y2);
                          int width=Math.abs(x1-x2);
                          int height=Math.abs(y1-y2);
                          if(fillFlag){
                              g2d.fillRect(left,top, width, height);
                          }
                          else{
                              g2d.drawRect(left,top, width, height);
                          }
                          break;
                      }
                      case 3:{
                          int left=(x1<x2?x1:x2);
                          int top=(y1<y2?y1:y2);
                          int width=Math.abs(x1-x2);
                          int height=Math.abs(y1-y2);
                          int startAngle=0;
                          if(x1<x2&&y1<y2)startAngle=0;
                          if(x1<x2&&y1>y2)startAngle=90;
                          if(x1>x2&&y1<y2)startAngle=270;
                          if(x1>x2&&y1>y2)startAngle=180;

                          if(fillFlag){
                              g2d.fillArc(left,top, width, height,startAngle,90);
                          }
                          else{
                              g2d.drawArc(left,top, width, height,startAngle,90);
                          }
                          break;
                      }
                      case 4:{
                          int left=(x1<x2?x1:x2);
                          int top=(y1<y2?y1:y2);
                          int width=Math.abs(x1-x2);
                          int height=Math.abs(y1-y2);
                          if(fillFlag){
                              g2d.fillOval(left,top, width, height);
                          }
                          else{
                              g2d.drawOval(left,top, width, height);
                          }
                          break;
                      }
                  }
                  flag=false;
              }

          }

          public void clear(){
              clearFlag=true;
              repaint();
          }

          public void setType(int type){
              this.type=type;
          }
          public void setColor(Color color){
              this.color=color;
          }
          public void setLineSize(int size){
              this.size=size;
          }
          
          public void setFillFlag(boolean flag){
              this.fillFlag=flag;
          }

          public Color getColor(){
              return color;
          }
      }
      netstat 离线

      级别: 新手上路

      显示用户信息 

      只看该作者, 沙发   发表于: 2009-10-17
      没有注释,我觉得不太好
      描述
      快速回复

      批量上传需要先选择文件,再选择上传
      认证码:

       回复后跳转到最后一页