使用JColorChooser绘图

时间:2019-02-02 20:17:24

标签: java swing jcolorchooser

我正在尝试使用颜色选择器选择要绘制的颜色。我可以显示颜色选择并以黑色绘制,但是之后就被卡住了。

package sketch;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* @author Brittany
*/
public class Sketch extends JPanel{

/**
 * Keeps track of the last point to draw the next line from.
 */
private Point lastPoint;

/**
 * Constructs a panel, registering listeners for the mouse.
 */
public Sketch() {

 Color drawColor = Color.BLACK;



    // When the mouse button goes down, set the current point
    // to the location at which the mouse was pressed.
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            lastPoint = new Point(e.getX(), e.getY());
        }
    });

    // When the mouse is dragged, draw a line from the old point
    // to the new point and update the value of lastPoint to hold
    // the new current point.
    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            Graphics g = getGraphics();
            g.setColor(drawColor);
            g.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
            lastPoint = new Point(e.getX(), e.getY());
            g.dispose();
        }
    });
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    JFrame frame = new JFrame("Simple Sketching Program");
    JButton colorBtn = new JButton("Color");

   colorBtn.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
 Color newColor = JColorChooser.showDialog(frame,"Choose Color",Color.BLACK);
            if(newColor != null){

            }

        }
    });



    frame.getContentPane().add(new Sketch(), BorderLayout.CENTER);
    frame.getContentPane().add(colorBtn, BorderLayout.NORTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);

}

}

1 个答案:

答案 0 :(得分:0)

Graphics g = getGraphics();不是定制绘画的工作方式。 getGraphics可以返回null,无非就是上一次绘画过程的快照,它将在下一次绘画过程中被替换。

首先查看Performing Custom PaintingPainting in AWT and Swing,以获取有关绘画工作方式以及如何使用绘画的更多详细信息。

您那么有两种选择。您可以将BufferedImage用作主要绘图表面,然后将其绘制到组件上,或者可以跟踪所绘制的内容(以及所用的颜色),并在每次绘制周期发生时进行复制。两者都有优点和缺点,并且你会使用将取决于你的需要。

回答您的问题。您需要某种方式将新颜色传递到Sketch的实例。

我将首先在Sketch中创建一个setter,该setter可用于其他类来更改颜色...

public Sketch() {

    private Color drawColor = Color.BLACK;

    public void setDrawColor(Color color) {
        if (color == null) {
            drawColor = Color.BLACK;
        } else {
            drawColor = colorl
        }
    }

然后您将使用此方法将新颜色传递给它...

public static void main(String[] args) {
    JFrame frame = new JFrame("Simple Sketching Program");
    JButton colorBtn = new JButton("Color");

    Sketch sketch = new Sketch();

    colorBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            Color newColor = JColorChooser.showDialog(frame,"Choose Color",Color.BLACK);
            if(newColor != null){
                sketch.setDrawColor(newColor);
            }
        }
    });


    frame.getContentPane().add(new Sketch(), BorderLayout.CENTER);