单击绘图区域时Java背景会发生变化

时间:2013-12-12 20:54:25

标签: java swing colors background click

在此之前我有问题在使用颜色选择器时更改笔的颜色和背景颜色..现在可以更改颜色笔但背景无法改变颜色。它可以改变背景颜色但是我需要点击绘制区域然后背景会改变..当我们选择颜色时应该改变背景颜色吗?但它没有......

ButtonPanel

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

public class ButtonPanel extends JPanel implements ItemListener,
    ActionListener
{
    private DrawingArea drawingArea;

        private String tools[] = {"Pencil", "Line", "Circle", "Rectangle", "Filled Circle", "Filled Rectangle", "Round Rectangle", "Filled Round Rectangle"};

        private Color color = (Color.WHITE);
        private JComboBox<String> jcbTool;
    private JButton btnClear;
        private JButton save;
        private JButton infobutton;
        private JButton colorBtn;
        private JButton colorBg;



    public void itemStateChanged(ItemEvent ie)
    {
                if (ie.getSource()==jcbTool)

            {       

                String tool = (String)jcbTool.getSelectedItem();
                drawingArea.setTool(tool);

        }
             //  else  
                //    if (ie.getSource()==eraser)
//{               String tool = (String)eraser.getSelectedItem();
 //              drawingArea.setTool(tool)
        }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource()==btnClear)
            drawingArea.clear();     
                else if (e.getSource()==infobutton)
                {
            //default title and icon
                JOptionPane.showMessageDialog(this,"Paint java created by bla bla bla bla bla blaa");
                }
                else if  (e.getSource()==colorBtn)
                {
                    color = JColorChooser.showDialog(null,"LOL",color);
                    drawingArea.setColorBtn(color);
                }
                  else if  (e.getSource()==colorBg)
                  {
                    color = JColorChooser.showDialog(null,"LOL",color);
                   drawingArea.setColorBg(color);
                  }
        }
}

1 个答案:

答案 0 :(得分:1)

只需在repaint()中的actionPerformed()方法中调用DrawingArea方法:

if (e.getSource() == colorBg) {
    color = JColorChooser.showDialog(null, "LOL", color);
    drawingArea.setColorBg(color);
    drawingArea.repaint();
}

因为当您更改画笔颜色并单击鼠标repaint()方法时,

但是当你设置背景颜色时,你也需要强制重绘。