java - 程序运行时更改笔画大小

标签 java swing jframe jcombobox stroke

我知道对于 Java 破解者来说这是一个愚蠢的问题。 目前我正在编写一个小绘画程序。

所以我的问题是,我想在一个小下拉菜单中设置笔画大小。

下拉菜单的代码在这里:

    DefaultComboBoxModel model = new DefaultComboBoxModel();
    model.addElement("1");
    model.addElement("5");
    model.addElement("10");
    JComboBox comboBox = new JComboBox(model);
    panel.add(comboBox);

笔画大小的代码在这里:

            public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();

            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            Stroke stroke = new BasicStroke(10);
            graphics2D.setStroke(stroke);
            graphics2D.drawLine(oldX, oldY, currentX, currentY);
            repaint();

            oldX = currentX;
            oldY = currentY;               
        }

BasicStroke 中的 10 只是一些随机数。我想实现这一点,它从下拉列表中读取(标准 1),将其保存在变量中,然后在 BasicStroke 函数中使用该变量。

以下是完整代码(Netbeans 项目):

package paint;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Paint {

public static void main(String[] args) {
    PaintWindow frame = new PaintWindow();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);
}
}

class PaintWindow extends JFrame {

public PaintWindow() {
    setTitle("Paint it");
    setSize(700, 600);

    panel = new JPanel();
    panel.setPreferredSize(new Dimension(64, 64));
    drawPad = new PadDraw();

//Creates a new container
    Container content = this.getContentPane();
    content.setLayout(new BorderLayout());

//sets the panel to the left, padDraw in the center
    content.add(panel, BorderLayout.NORTH);
    content.add(drawPad, BorderLayout.CENTER);

//add the color buttons:
    makeColorButton(Color.BLACK);
    makeColorButton(Color.BLUE);
    makeColorButton(Color.MAGENTA);
    makeColorButton(Color.RED);
    makeColorButton(Color.ORANGE);
    makeColorButton(Color.YELLOW);
    makeColorButton(Color.GREEN);
    makeColorButton(Color.CYAN);

    DefaultComboBoxModel model = new DefaultComboBoxModel();
    model.addElement("1");
    model.addElement("5");
    model.addElement("10");
    JComboBox comboBox = new JComboBox(model);
    panel.add(comboBox);

//creates the clear button
    JButton clearButton = new JButton("Clear");
    clearButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawPad.clear();
        }
    });
    panel.add(clearButton);

    panel = new JPanel();
    JLabel jlabel = new JLabel("Copyright© by Jan Büttiker");
    panel.setPreferredSize(new Dimension(0, 20));
    panel.add(jlabel);
    content.add(panel, BorderLayout.SOUTH);
}

/*
 * makes a button that changes the color
 * @param color the color used for the button
 */
public void makeColorButton(final Color color) {
    JButton tempButton = new JButton();
    tempButton.setBackground(color);
    tempButton.setPreferredSize(new Dimension(56, 56));
    panel.add(tempButton);
    tempButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawPad.changeColor(color);
        }
    });
}
private JPanel panel;
private PadDraw drawPad;
}

class PadDraw extends JComponent {
//this is gonna be the image that you draw on

Image image;
//this is what we'll be using to draw on
Graphics2D graphics2D;
//these are gonna hold the mouse coordinates
int currentX, currentY, oldX, oldY;

public PadDraw() {
    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter() {
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
        public void mousePressed(MouseEvent e) {
            oldX = e.getX();
            oldY = e.getY();
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
//while the mouse is dragged it sets currentX & currentY as the mouses x and y
//then it draws a line at the coordinates
//it repaints it and sets oldX and oldY as currentX and currentY
        public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();

            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,     
            RenderingHints.VALUE_ANTIALIAS_ON);
            Stroke stroke = new BasicStroke(10);
            graphics2D.setStroke(stroke);
            graphics2D.drawLine(oldX, oldY, currentX, currentY);
            repaint();

            oldX = currentX;
            oldY = currentY;               
        }
    });
}

//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image
public void paintComponent(Graphics g) {
    if (image == null) {
        image = createImage(getSize().width, getSize().height);
        graphics2D = (Graphics2D) image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,   
        RenderingHints.VALUE_ANTIALIAS_ON);
        clear();
    }

    g.drawImage(image, 0, 0, null);
}

//this is the clear
//it sets the colors as white
//then it fills the window with white
//thin it sets the color back to black
public void clear() {
    graphics2D.setPaint(Color.white);
    graphics2D.fillRect(0, 0, getSize().width, getSize().height);
    graphics2D.setPaint(Color.black);
    repaint();
}

public void changeColor(Color theColor) {
    graphics2D.setPaint(theColor);
    repaint();
}
}

这是我的第一篇文章,所以请友善,我仍处于学习阶段^^。

感谢您的帮助,祝您有美好的一天

最佳答案

为此,您需要使用 ItemListener在您的 JComboBox 上。了解更多关于 ComboBox in tutorial

1) 将实例变量 public float strokeSize = 10.0f; 添加到 PadDraw 作为默认值。

2) 使用StrokeStroke = new BasicStroke(StrokeSize);而不是StrokeStroke = new BasicStroke(10);

3)用下面的方式创建你的ComboBox,如果你改变JComboBox中的值,它会改变strokeSize值:

    DefaultComboBoxModel<Float> model = new DefaultComboBoxModel<Float>();
    model.addElement(1f);
    model.addElement(5f);
    model.addElement(10f);
    final JComboBox<Float> comboBox = new JComboBox<Float>(model);
    comboBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent arg0) {
            Float selectedItem = (Float) comboBox.getSelectedItem();
            if(selectedItem != null){
                drawPad.strokeSize = selectedItem;
            }

        }
    });

关于java - 程序运行时更改笔画大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19931528/

相关文章:

java - 做了一个 "virus",需要提示

java - 使用 Java 的 JComponent repaint()

java - 我无法以 200 的间隔进行图像更新

java - Scala 特征与 Java 8 接口(interface)之间有什么区别和相似之处?

java.sql.SQLException : ORA-01005: null password given; logon denied 异常

java - 如何垂直设置 JRadioButtons

java - 如何向此代码添加按钮?我不知道如何或在哪里放置它

Java GUI重绘错误?

java - 在 Java SWT 中获取菜单控件的大小

java - 如何以编程方式获取我的设备的手机号码?