Exemple #1
0
    @Override
    public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub

      Object source = e.getSource();
      if (source == ok) {
        response = APPLY_OPTION;
        this.setVisible(false);
      } else if (source == cancel) {
        response = CANCEL_OPTION;
        this.setVisible(false);
      } else if (source == sizeCombo) {
        // get the number from the source
        JComboBox number = (JComboBox) source;
        String numberItem = (String) number.getSelectedItem();
        Font temp = example.getFont();
        // then set the font
        int newSize = Integer.parseInt(numberItem);
        example.setFont(new Font(temp.getFamily(), temp.getStyle(), newSize));
      } else if (source == fontCombo) {
        JComboBox font = (JComboBox) source;
        String s = (String) font.getSelectedItem();
        Font tmp = example.getFont();
        example.setFont(new Font(s, tmp.getStyle(), tmp.getSize()));
      } else if (source == foreground) {
        Color tmp = JColorChooser.showDialog(this, "Choose text color", example.getForeground());
        MenuBar.shapeLBG.setBackground(tmp);
        if (tmp != null) example.setForeground(tmp);
      }
    }
 public void actionPerformed(ActionEvent e) {
   Color color =
       JColorChooser.showDialog(InnereKlassemitmainMethode.this, "Eine Farbe wählen", Color.red);
   if (color != null) {
     g.setColor(color);
     status.setText("Farbe mit JColorChooser abgeändert");
   }
 }
  protected void attachTo(Component jc) {
    if (extListener != null && extListener.accept(jc)) {
      extListener.startListeningTo(jc, extNotifier);
      listenedTo.add(jc);
      if (wizardPage.getMapKeyFor(jc) != null) {
        wizardPage.maybeUpdateMap(jc);
      }
      return;
    }
    if (isProbablyAContainer(jc)) {
      attachToHierarchyOf((Container) jc);
    } else if (jc instanceof JList) {
      listenedTo.add(jc);
      ((JList) jc).addListSelectionListener(this);
    } else if (jc instanceof JComboBox) {
      ((JComboBox) jc).addActionListener(this);
    } else if (jc instanceof JTree) {
      listenedTo.add(jc);
      ((JTree) jc).getSelectionModel().addTreeSelectionListener(this);
    } else if (jc instanceof JToggleButton) {
      ((AbstractButton) jc).addItemListener(this);
    } else if (jc
        instanceof JFormattedTextField) { // JFormattedTextField must be tested before JTextCompoent
      jc.addPropertyChangeListener("value", this);
    } else if (jc instanceof JTextComponent) {
      listenedTo.add(jc);
      ((JTextComponent) jc).getDocument().addDocumentListener(this);
    } else if (jc instanceof JColorChooser) {
      listenedTo.add(jc);
      ((JColorChooser) jc).getSelectionModel().addChangeListener(this);
    } else if (jc instanceof JSpinner) {
      ((JSpinner) jc).addChangeListener(this);
    } else if (jc instanceof JSlider) {
      ((JSlider) jc).addChangeListener(this);
    } else if (jc instanceof JTable) {
      listenedTo.add(jc);
      ((JTable) jc).getSelectionModel().addListSelectionListener(this);
    } else {
      if (logger.isLoggable(Level.FINE)) {
        logger.fine(
            "Don't know how to listen to a "
                + // NOI18N
                jc.getClass().getName());
      }
    }

    if (accept(jc) && !(jc instanceof JPanel)) {
      jc.addPropertyChangeListener("name", this);
      if (wizardPage.getMapKeyFor(jc) != null) {
        wizardPage.maybeUpdateMap(jc);
      }
    }

    if (logger.isLoggable(Level.FINE) && accept(jc)) {
      logger.fine("Begin listening to " + jc); // NOI18N
    }
  }
 private static void createAndShowGUI() {
   JFrame frame = new JFrame(Test6827032.class.getName());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   cc = new JColorChooser();
   cc.setDragEnabled(true);
   frame.add(cc);
   frame.pack();
   frame.setVisible(true);
 }
    public void actionPerformed(ActionEvent e) {
      JColorChooser colorChooser = BasicComponentFactory.createColorChooser(bufferedColorModel);
      ActionListener okHandler = new OKHandler(trigger);
      JDialog dialog =
          JColorChooser.createDialog(parent, "Choose Color", true, colorChooser, okHandler, null);
      dialog.addWindowListener(new Closer());
      dialog.addComponentListener(new DisposeOnClose());

      dialog.setVisible(true); // blocks until user brings dialog down...
    }
  /**
   * Handle action events coming from the buttons.
   *
   * @param evt, the associated ActionEvent.
   */
  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();

    if (source instanceof JButton) {
      if (source == pbSave) {
        chosen = tcc.getColor();
        onCancel();
      } else {
        onCancel();
      }
    }
  }
  protected void detachFrom(Component jc) {
    listenedTo.remove(jc);
    if (extListener != null && extListener.accept(jc)) {
      extListener.stopListeningTo(jc);
    }
    if (isProbablyAContainer(jc)) {
      detachFromHierarchyOf((Container) jc);
    } else if (jc instanceof JList) {
      ((JList) jc).removeListSelectionListener(this);
    } else if (jc instanceof JComboBox) {
      ((JComboBox) jc).removeActionListener(this);
    } else if (jc instanceof JTree) {
      ((JTree) jc).getSelectionModel().removeTreeSelectionListener(this);
    } else if (jc instanceof JToggleButton) {
      ((AbstractButton) jc).removeActionListener(this);
    } else if (jc instanceof JTextComponent) {
    } else if (jc
        instanceof JFormattedTextField) { // JFormattedTextField must be tested before JTextCompoent
      jc.removePropertyChangeListener("value", this);
      ((JTextComponent) jc).getDocument().removeDocumentListener(this);
    } else if (jc instanceof JColorChooser) {
      ((JColorChooser) jc).getSelectionModel().removeChangeListener(this);
    } else if (jc instanceof JSpinner) {
      ((JSpinner) jc).removeChangeListener(this);
    } else if (jc instanceof JSlider) {
      ((JSlider) jc).removeChangeListener(this);
    } else if (jc instanceof JTable) {
      ((JTable) jc).getSelectionModel().removeListSelectionListener(this);
    }

    if (accept(jc) && !(jc instanceof JPanel)) {
      jc.removePropertyChangeListener("name", this);
      Object key = wizardPage.getMapKeyFor(jc);

      if (key != null) {
        if (logger.isLoggable(Level.FINE)) {
          logger.fine(
              "Named component removed from hierarchy: "
                  + // NOI18N
                  key
                  + ".  Removing any corresponding "
                  + // NOI18N
                  "value from the wizard settings map."); // NOI18N
        }

        wizardPage.removeFromMap(key);
      }
    }

    if (logger.isLoggable(Level.FINE) && accept(jc)) {
      logger.fine("Stop listening to " + jc); // NOI18N
    }
  }
  public void mouseReleased(MouseEvent e) {
    if (mMouseOverButton && mPressedButton != -1) {
      boolean needsUpdate = false;
      if (mColorListMode == VisualizationColor.cColorListModeCategories) {
        Color newColor =
            JColorChooser.showDialog(
                mOwner, "Select category color", mCategoryColorList[mPressedButton]);
        if (newColor != null) {
          mCategoryColorList[mPressedButton] = newColor;
          needsUpdate = true;
        }
      } else {
        if (mPressedButton == cColorWedgeButton) {
          if (++mColorListMode > VisualizationColor.cColorListModeStraight)
            mColorListMode = VisualizationColor.cColorListModeHSBShort;
          updateColorList(-1);
          needsUpdate = true;
        } else {
          int index = mPressedButton * (mWedgeColorList.length - 1);
          Color newColor =
              JColorChooser.showDialog(
                  mOwner, "Select color for min/max value", mWedgeColorList[index]);
          if (newColor != null) {
            mWedgeColorList[index] = newColor;
            updateColorList(-1);
            needsUpdate = true;
          }
        }
      }

      mPressedButton = -1;
      mMouseOverButton = false;

      if (needsUpdate) {
        mListener.actionPerformed(
            new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "colorChanged"));
        repaint();
      }
    }
  }
  @Override
  protected void fireActionPerformed(ActionEvent event) {
    try {
      this.setSelected(true);

      // create the dialog if this has not been done yet.
      if (this.dialog == null) {
        // Get the top level ancestor as parent for the new dialog.
        // This ensures that on Mac OS X the global menu bar remains
        // visible while the dialog is open.
        final Container parent = this.getTopLevelAncestor();
        dialog =
            JColorChooser.createDialog(
                parent,
                colorChooserTitle,
                true, // modal
                colorChooser,
                this, // OK button handler
                null); // no CANCEL button handler
        dialog.pack();
        dialog.setResizable(false);
      }

      // show the dialog
      colorChooser.setColor(color);
      dialog.setVisible(true);

      /* A simpler option that is displaying the ugly preview panel:
      Color newColor = JColorChooser.showDialog(this.getTopLevelAncestor(),
              colorChooserTitle, color);
      if (newColor != null){
          // only set color and fire an event when user did not cancel.
          this.setColor(newColor);
          super.fireActionPerformed(event);
      }*/
    } catch (Exception e) {
    } finally {
      this.setSelected(false);
    }
  }
  public ColorChooserDemo() {
    super(new BorderLayout());

    // 设置一个标签,做广告的。也用来显示用户选择的颜色。
    banner = new JLabel("欢迎使用颜色选择器!", JLabel.CENTER);
    banner.setForeground(Color.yellow);
    banner.setBackground(Color.blue);
    banner.setOpaque(true);
    banner.setFont(new Font("SansSerif", Font.BOLD, 24));
    banner.setPreferredSize(new Dimension(100, 65));

    JPanel bannerPanel = new JPanel(new BorderLayout());
    bannerPanel.add(banner, BorderLayout.CENTER);
    bannerPanel.setBorder(BorderFactory.createTitledBorder("广告"));

    // 设置选择颜色选择器
    tcc = new JColorChooser(banner.getForeground()); // 设置初始颜色
    tcc.getSelectionModel().addChangeListener(this); // 给所有模式添加监听
    tcc.setBorder(BorderFactory.createTitledBorder("选择颜色"));

    add(bannerPanel, BorderLayout.CENTER);
    add(tcc, BorderLayout.PAGE_END);
  }
 /** Methode appelee quand l'utilisateur appuie sur le bouton. */
 public void actionPerformed(ActionEvent e) {
   if (e.getSource() == button) {
     Color choosedColor =
         JColorChooser.showDialog(parent.dialog(), "Choose color", new Color(R, G, B));
     if (choosedColor != null) {
       this.R = choosedColor.getRed();
       this.G = choosedColor.getGreen();
       this.B = choosedColor.getBlue();
       parent.elementModified();
     }
     String tmp =
         new String(
             label
                 + Integer.toString(getRed())
                 + ", "
                 + Integer.toString(getGreen())
                 + ", "
                 + Integer.toString(getBlue()));
     while (tmp.length() < 38) {
       tmp = tmp + " ";
     }
     jlabel.setText(tmp);
   }
 }
    @Override
    public void actionPerformed(ActionEvent evt) {
      String command = evt.getActionCommand();
      switch (command) {
        case "New File":
          {
            OSC = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics osg = OSC.getGraphics();
            osg.setColor(fillColor);
            osg.fillRect(0, 0, getWidth(), getHeight());
            osg.dispose();
            repaint();
            break;
          }
        case "Open":
          {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter =
                new FileNameExtensionFilter("JPG or PNG Images", "jpg", "png");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
              try {

                OSC = ImageIO.read(chooser.getSelectedFile());
                Graphics2D g2 = (Graphics2D) OSC.getGraphics();

                g2.scale((double) 640 / OSC.getWidth(), (double) 480 / OSC.getHeight());
                g2.drawImage(OSC, 0, 0, OSC.getWidth(), OSC.getHeight(), null);
              } catch (Exception e) {
              }
            }
            repaint();
            break;
          }
        case "Save":
          {
            JFileChooser fileDialog = new JFileChooser();
            fileDialog.setSelectedFile(new File("image.PNG"));
            fileDialog.setDialogTitle("Select File to be Saved");
            int option = fileDialog.showSaveDialog(null);
            if (option != JFileChooser.APPROVE_OPTION)
              return; // User canceled or clicked the dialog’s close box.

            File selectedFile = fileDialog.getSelectedFile();
            if (selectedFile.exists()) { // Ask the user whether to replace the file.
              int response =
                  JOptionPane.showConfirmDialog(
                      null,
                      "The file \""
                          + selectedFile.getName()
                          + "\" already exists.\nDo you want to replace it?",
                      "Confirm Save",
                      JOptionPane.YES_NO_OPTION,
                      JOptionPane.WARNING_MESSAGE);
              if (response != JOptionPane.YES_OPTION)
                return; // User does not want to replace the file.
            }
            try {
              boolean hasFormat = ImageIO.write(OSC, "PNG", selectedFile);
              if (!hasFormat) throw new Exception("PNG" + " format is not available.");
            } catch (Exception e) {
              JOptionPane.showMessageDialog(
                  null, "Sorry, an error occurred while trying to save image.");
              e.printStackTrace();
            }

            break;
          }
        case "Exit":
          {
            System.exit(0);
          }

        case "Select Drawing Color...":
          {
            Color newColor =
                JColorChooser.showDialog(AdvancedGUIEX1.this, "Select Drawing Color", currentColor);
            if (newColor != null) currentColor = newColor;
            break;
          }
        case "Fill With Color...":
          {
            Color newColor =
                JColorChooser.showDialog(AdvancedGUIEX1.this, "Select Fill Color", fillColor);
            if (newColor != null) {
              fillColor = newColor;
              Graphics osg = OSC.getGraphics();
              osg.setColor(fillColor);
              osg.fillRect(0, 0, OSC.getWidth(), OSC.getHeight());
              osg.dispose();
              AdvancedGUIEX1.this.repaint();
            }
            break;
          }
        case "Draw With Black":
          currentColor = Color.BLACK;
          break;
        case "Draw With White":
          currentColor = Color.WHITE;
          break;
        case "Draw With Red":
          currentColor = Color.RED;
          break;
        case "Draw With Green":
          currentColor = Color.GREEN;
          break;
        case "Draw With Blue":
          currentColor = Color.BLUE;
          break;
        case "Draw With Yellow":
          currentColor = Color.YELLOW;
          break;
        case "Curve":
          currentTool = Tool.CURVE;
          break;
        case "Line":
          currentTool = Tool.LINE;
          break;
        case "Rectangle":
          currentTool = Tool.RECT;
          break;
        case "Oval":
          currentTool = Tool.OVAL;
          break;
        case "Filled Rectangle":
          currentTool = Tool.FILLED_RECT;
          break;
        case "Filled Oval":
          currentTool = Tool.FILLED_OVAL;
          break;
        case "Smudge":
          currentTool = Tool.SMUDGE;
          break;
        case "Erase":
          currentTool = Tool.ERASE;
          break;
      }
    }
  public void actionPerformed(ActionEvent e) {
    System.out.println("actionPerformed");
    if (e.getSource() == pen) // 画笔
    {
      System.out.println("pen");
      toolFlag = 0;
    }

    if (e.getSource() == eraser) // 橡皮
    {
      System.out.println("eraser");
      toolFlag = 1;
    }

    if (e.getSource() == clear) // 清除
    {
      System.out.println("clear");
      toolFlag = 2;
      paintInfo.removeAllElements();
      repaint();
    }

    if (e.getSource() == drLine) // 画线
    {
      System.out.println("drLine");
      toolFlag = 3;
    }

    if (e.getSource() == drCircle) // 画圆
    {
      System.out.println("drCircle");
      toolFlag = 4;
    }

    if (e.getSource() == drRect) // 画矩形
    {
      System.out.println("drRect");
      toolFlag = 5;
    }

    if (e.getSource() == colchooser) // 调色板
    {
      System.out.println("colchooser");
      Color newColor = JColorChooser.showDialog(this, "我的调色板", c);
      c = newColor;
    }

    if (e.getSource() == openPic) // 打开图画
    {

      openPicture.setVisible(true);

      if (openPicture.getFile() != null) {
        int tempflag;
        tempflag = toolFlag;
        toolFlag = 2;
        repaint();

        try {
          paintInfo.removeAllElements();
          File filein = new File(openPicture.getDirectory(), openPicture.getFile());
          picIn = new FileInputStream(filein);
          VIn = new ObjectInputStream(picIn);
          paintInfo = (Vector) VIn.readObject();
          VIn.close();
          repaint();
          toolFlag = tempflag;

        } catch (ClassNotFoundException IOe2) {
          repaint();
          toolFlag = tempflag;
          System.out.println("can not read object");
        } catch (IOException IOe) {
          repaint();
          toolFlag = tempflag;
          System.out.println("can not read file");
        }
      }
    }

    if (e.getSource() == savePic) // 保存图画
    {
      savePicture.setVisible(true);
      try {
        File fileout = new File(savePicture.getDirectory(), savePicture.getFile());
        picOut = new FileOutputStream(fileout);
        VOut = new ObjectOutputStream(picOut);
        VOut.writeObject(paintInfo);
        VOut.close();
      } catch (IOException IOe) {
        System.out.println("can not write object");
      }
    }
  }
 /**
  * <br>
  * 方法说明:事件监听。用户选择颜色触发 <br>
  * 输入参数:ChangeEvent e 用户选择事件 <br>
  * 返回类型:
  */
 public void stateChanged(ChangeEvent e) {
   Color newColor = tcc.getColor(); // 获取用户选择的颜色
   banner.setForeground(newColor);
 }
Exemple #15
0
 public void actionPerformed(ActionEvent e) {
   color = chooser.getColor();
 }
 /** Set the colour the user has chosen. */
 public void setColour(Color color) {
   tcc.setColor(color);
 }
Exemple #17
0
 /** Action listener for the OK button in the color chooser. */
 public void actionPerformed(ActionEvent e) {
   // ok button in color chooser was pressed.
   this.setColor(colorChooser.getColor());
   super.fireActionPerformed(e);
 }