⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttonbar.java

📁 利用java编写的paintbox小程序
💻 JAVA
字号:
/*
 * ButtonBar.java
 *
 * Created on 2007年4月23日, 下午11:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package paintbox;

/**
 *
 * @author fly
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonBar extends JToolBar {
    
    /** Creates a new instance of ButtonBar */
public static final int LINE = 0;
   public static final int OVAL = 1;
   public static final int RECT = 2;
   public static final int POLY = 3;
   public static final int COLOR = -1;
   public static final int SELECT = -2;
   public static final int FILLCOLOR=-3;
   private JRadioButton selectButton, lineButton, ovalButton,rectButton, polyButton;
   private JButton strokeButton,fillButton;
   private int currentButton;
   private Color currentStrokeColor, currentFillColor;

   //-----------------------------------------------------------------
   //  Sets up the panel of buttons.
   //-----------------------------------------------------------------
   public ButtonBar ()
   {
      currentButton = SELECT;
      currentStrokeColor = Color.BLACK;
      currentFillColor=Color.WHITE;

      ButtonListener listener = new ButtonListener ();

      ButtonGroup shapeGroup = new ButtonGroup ();

      selectButton = new JRadioButton (new ImageIcon ("select.gif"));
      selectButton.setSelectedIcon (new ImageIcon ("selectOn.gif"));
      selectButton.setToolTipText ("Select tool");
      selectButton.addActionListener (listener);
      selectButton.setBackground(Color.BLUE);
      selectButton.setSelected(true);

      lineButton = new JRadioButton (new ImageIcon ("line.gif"));
      lineButton.setSelectedIcon (new ImageIcon ("lineOn.gif"));
      lineButton.setToolTipText ("Deawing line tool");
      lineButton.addActionListener (listener);
      lineButton.setBackground(Color.BLUE);

      ovalButton = new JRadioButton (new ImageIcon ("oval.gif"));
      ovalButton.setSelectedIcon (new ImageIcon ("ovalOn.gif"));
      ovalButton.setToolTipText ("Drawing oval tool");
      ovalButton.addActionListener (listener);
      ovalButton.setBackground(Color.BLUE);

      rectButton = new JRadioButton (new ImageIcon ("rect.gif"));
      rectButton.setSelectedIcon (new ImageIcon ("rectOn.gif"));
      rectButton.setToolTipText ("Drawing rectangle tool");
      rectButton.addActionListener (listener);
      rectButton.setBackground(Color.BLUE);

      polyButton = new JRadioButton (new ImageIcon ("poly.gif"));
      polyButton.setSelectedIcon (new ImageIcon ("polyOn.gif"));
      polyButton.setToolTipText ("Drawing polyline tool");
      polyButton.addActionListener (listener);
      polyButton.setBackground(Color.BLUE);

      strokeButton = new JButton (" Stroke ");
      strokeButton.setToolTipText ("Choose stroke color tool");
      strokeButton.setBackground (Color.black);
      strokeButton.setForeground (Color.white);
      strokeButton.addActionListener (listener);

      fillButton = new JButton (" FILL ");
      fillButton.setToolTipText ("Choose fill color tool");
      fillButton.setBackground (Color.black);
      fillButton.setForeground (Color.white);
      fillButton.addActionListener (listener);
      
      shapeGroup.add (selectButton);
      shapeGroup.add (lineButton);
      shapeGroup.add (ovalButton);
      shapeGroup.add (rectButton);
      shapeGroup.add (polyButton);

      add (selectButton);
      add (new JToolBar.Separator ());
      add (lineButton);
      add (ovalButton);
      add (rectButton);
      add (polyButton);
      add (new JToolBar.Separator ());
      add (strokeButton);
      add (new JToolBar.Separator ());
      add (fillButton);

      setFloatable (false);
      setBackground(Color.BLUE);
   }

   //-----------------------------------------------------------------
   //  Returns the currently toggled button.
   //-----------------------------------------------------------------
   public int getButton ()
   {
      return currentButton;
   }

   //-----------------------------------------------------------------
   //  Returns the current stroke color.
   //-----------------------------------------------------------------
   public Color getStrokeColor ()
   {
      return currentStrokeColor;
   }
   
   public Color getcurrentFillColor()
   {
       return currentFillColor;
   }

   //*****************************************************************
   //  An inner class to handle button events.
   //*****************************************************************
   private class ButtonListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Responds to action events caused by buttons.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
         Object source = event.getSource ();

         if (source == selectButton)
            currentButton = SELECT;

         else if (source == lineButton)
            currentButton = LINE;

         else if (source == ovalButton)
            currentButton = OVAL;

         else if (source == rectButton)
            currentButton = RECT;

         else if (source == polyButton)
            currentButton = POLY;

         else if (source == strokeButton)
         {
            currentButton=COLOR;
            Color returnedColor = JColorChooser.showDialog (
                (Component) event.getSource (),
                "Set the stroke color:",  Color.black);

            if (returnedColor != null)
            {
                currentStrokeColor = returnedColor;

                int rgbValue = returnedColor.getRed () +
                               returnedColor.getGreen () +
                               returnedColor.getBlue ();

                if (rgbValue < 250)
                    strokeButton.setForeground (Color.white);
                else
                    strokeButton.setForeground (Color.black);

                strokeButton.setBackground (currentStrokeColor);
            }
         }
         else if(source == fillButton)
         {
            currentButton=FILLCOLOR;
            Color returnedColor = JColorChooser.showDialog (
                (Component) event.getSource (),
                "Set the fill color:",  Color.WHITE);

            if (returnedColor != null)
            {
                currentFillColor = returnedColor;

                int rgbValue = returnedColor.getRed () +
                               returnedColor.getGreen () +
                               returnedColor.getBlue ();

                if (rgbValue < 250)
                    fillButton.setForeground (Color.white);
                else
                    fillButton.setForeground (Color.black);

                fillButton.setBackground (currentFillColor);
            }
         }
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -