首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JColorChooser绘图

使用JColorChooser绘图
EN

Stack Overflow用户
提问于 2019-02-03 04:17:24
回答 1查看 90关注 0票数 0

我正在尝试使用颜色选择器来选择要用来绘制的颜色。我可以让它显示颜色选择和绘制在黑色,但我被卡住了。

代码语言:javascript
复制
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);

}

}
EN

回答 1

Stack Overflow用户

发布于 2019-02-03 05:09:31

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

首先看看Performing Custom PaintingPainting in AWT and Swing,了解更多关于绘画的工作原理和你应该如何使用它的细节。

然后,您有两个选择。可以使用BufferedImage作为主绘制曲面,然后将其绘制到组件,也可以跟踪绘制的内容(以及绘制的颜色),并在每次发生绘制循环时重新生成它。这两种方法各有优缺点,您将根据自己的需要来决定使用哪种方法。

来回答你的问题。您需要某种方法来将新颜色传递给Sketch的一个实例。

我会从在Sketch中创建一个设置器开始,它可以用于其他类来更改颜色……

代码语言:javascript
复制
public Sketch() {

    private Color drawColor = Color.BLACK;

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

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

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54497132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档