创建颜色选择器JSlider以实现矩形

时间:2018-04-18 15:58:51

标签: java swing colors jslider

分配是创建一个颜色选择器。为什么当我移动滑块时颜色没有变化?我必须使用JSlider来影响矩形的填充颜色。

原始请求是:

  

(创建颜色选择器)声明名为JPanel的{​​{1}}的子类,它提供三个MyColorChooser个对象和三个JSlider个对象。每个JTextField表示颜色的红色,绿色和蓝色部分的0到255之间的值。使用这些值作为Color构造函数的参数来创建新的Color对象。显示相应JSlider中每个JSlider的当前值。当用户更改JTextField的值时,应相应更改JSlider。使用新的GUI组件作为应用程序的一部分,通过绘制填充矩形来显示当前JTextField值。

Color

代码中唯一不能按我想要的方式工作的部分是滑块不会影响package creatingacolorchooser; import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * * @author ian20 */ public class MyColorChooser extends JPanel { private JLabel redLabel; private JLabel greenLabel; private JLabel blueLabel; private JTextField redText; private JTextField greenText; private JTextField blueText; private JSlider redSlider; private JSlider greenSlider; private JSlider blueSlider; /** * Constructor */ public MyColorChooser() { //Create FlowLayout manager. setLayout(new FlowLayout()); //Create message Labels redLabel = new JLabel("Red: "); greenLabel = new JLabel("Green: "); blueLabel = new JLabel("Blue: "); //Create read only text fields. redText = new JTextField("0", 5); greenText = new JTextField("0", 5); blueText = new JTextField("0", 5); //Create JSlider redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); redSlider.setPreferredSize(new Dimension(1000,75)); redSlider.setMajorTickSpacing(10); redSlider.setMinorTickSpacing(1); redSlider.setPaintTicks(true); redSlider.setPaintLabels(true); redSlider.addChangeListener(new RedSlideListener()); greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); greenSlider.setPreferredSize(new Dimension(1000,75)); greenSlider.setMajorTickSpacing(10); greenSlider.setMinorTickSpacing(1); greenSlider.setPaintTicks(true); greenSlider.setPaintLabels(true); greenSlider.addChangeListener(new GreenSlideListener()); blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); blueSlider.setPreferredSize(new Dimension(1000,75)); blueSlider.setMajorTickSpacing(10); blueSlider.setMinorTickSpacing(1); blueSlider.setPaintTicks(true); blueSlider.setPaintLabels(true); blueSlider.addChangeListener(new BlueSlideListener()); // add(redLabel); add(redSlider); add(redText); add(greenLabel); add(greenSlider); add(greenText); add(blueLabel); add(blueSlider); add(blueText); } /** * Private inner class to handle the the change events that are * generated when the slider is moved */ private class RedSlideListener implements ChangeListener { public void stateChanged(ChangeEvent e) { int redInt; //get slider value redInt = redSlider.getValue(); //Create object and set value. RectPanel rp = new RectPanel(); rp.setRed(redInt = redSlider.getValue()); //Display slider value in text field. redText.setText(Integer.toString(redInt)); } } /** * Private inner class to handle the the change events that are generated * when the slider is moved */ private class GreenSlideListener implements ChangeListener { public void stateChanged(ChangeEvent e) { int greenInt; //get slider value greenInt = greenSlider.getValue(); //Create object and set value. RectPanel rp = new RectPanel(); rp.setGreen(greenInt = greenSlider.getValue()); //Display slider value in text field. greenText.setText(Integer.toString(greenInt)); } } /** * Private inner class to handle the the change events that are generated * when the slider is moved */ private class BlueSlideListener implements ChangeListener { public void stateChanged(ChangeEvent e) { int blueInt; //get slider value blueInt = blueSlider.getValue(); //Create object and set value. RectPanel rp = new RectPanel(); rp.setBlue(blueInt = blueSlider.getValue()); //Display slider value in text field. blueText.setText(Integer.toString(blueInt)); } } } enter code here package creatingacolorchooser; import java.awt.Color; import javax.swing.*; import java.awt.Graphics; /** * * @author ian20 */ public class RectPanel extends JPanel { private int red; private int blue; private int green; /** * The setRed method sets int red to a int value. * @param r */ public void setRed(int r) { red = r; } /** * The setBlue method sets int blue to a int value. * @param r */ public void setBlue(int b) { blue = b; } /** * The setGreen method sets int Green to a int value. * @param r */ public void setGreen(int g) { green = g; } /** * The getRed method returns int value called red. * @return */ public int getRed() { return red; } /** * The getBlue method returns int value called blue. * @return */ public int getBlue() { return blue; } /** * The getGreen method returns int value called blue. * @return */ public int getGreen() { return green; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); RectPanel rp = new RectPanel(); g.setColor(new Color(rp.getRed(),rp.getBlue(),rp.getGreen())); g.fillRect(475, 50, 200, 200); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package creatingacolorchooser; import java.awt.GridLayout; import javax.swing.JFrame; /** * * @author ian20 */ public class MainColor { public static void main(String[] args) { JFrame frame = new JFrame("Change Rectangle Color"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectPanel rect = new RectPanel(); rect.repaint(); MyColorChooser mcc = new MyColorChooser(); frame.setLayout(new GridLayout(2, 1)); frame.add(rect); frame.add(mcc); frame.setSize(1150, 600); frame.setVisible(true); } }

我改变了一些事情似乎工作,任何评论都会受到赞赏。

setColor

1 个答案:

答案 0 :(得分:0)

这没有任何用处:

RectPanel rp = new  RectPanel();
rp.setRed(redInt = redSlider.getValue());

正在创建 JPanel,更改其属性并无处显示。相反,您应该更改已经显示的RectPanel JPanel的状态,而不是创建一个新的。这意味着必须将要更改颜色的JPanel的引用传递到颜色选择器对象,以便可以更改其状态(颜色)。

此外,你应该在paintComponent方法中创建一个RectPanel。

你的问题相当于你拥有一辆蓝色的福特野马,然后你去汽车店,买一辆新的福特野马,把它漆成绿色,然后想知道为什么原来的野马仍然是蓝色的,这个问题源于一个关于什么是对象以及它们如何工作的基本混淆。每次使用new运算符调用类的构造函数时,请了解您正在创建该类型对象的全新且唯一的实例,并且此实例完全独立于之前可能已创建的其他实例。

我创建了一个工作示例,但我不会在这里发布整个内容,因为我们不做作业,但我会发布部分内容。请注意,我创建了一个与3种不同颜色一起使用的JPanel,称为ColorSliderPanel。我也称我的绘图JPanel MyColorPanel。

ColorSliderPanel的部分内容如下所示:

public class ColorSliderPanel extends JPanel {
    private static final int MAX_COLOR = 255;
    private Color color;
    private JSlider slider = new JSlider(0, MAX_COLOR, 0);
    private JTextField textField = new JTextField("0", 5);
    private MyColorPanel myColorPanel;

    // here is the key, my constructor takes MyColorPanel reference
    public ColorSliderPanel(String text, Color color, MyColorPanel myColorPanel) {
        this.color = color;
        this.myColorPanel = myColorPanel;  // and then sets the field of the class

        slider.setMajorTickSpacing(20);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.addChangeListener(new SliderListener());

        // .....

        setBorder(BorderFactory.createTitledBorder(text)); 

        // .....

    }

    private class SliderListener implements ChangeListener {
        @Override
        public void stateChanged(ChangeEvent cEvt) {
            int newValue = slider.getValue();

            // call getColor() on the original MyColorPanel object
            Color origClr = myColorPanel.getColor(); // get the original color
            textField.setText("" + newValue);

            // here we decide what the new color's r,g,b should be----

            // ......

            Color newColor = new Color(r, g, b);

            // set the color of the original MyColorPanel object
            myColorPanel.setColor(newColor);
        }
    }
}

MyColorPanel的部分内容。请注意,我提供了getColor()setColor(Color color)方法,但如果您愿意,可以使用您的红/蓝/绿色getter / setter:

public class MyColorPanel extends JPanel {
    private static final Color INIT_COLOR = Color.BLACK;
    private static final int RECT_WIDTH = 200;
    private Color color = INIT_COLOR;

    // ...

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        int rectX = (getWidth() - RECT_WIDTH) / 2;
        int rectY = (getHeight() - RECT_WIDTH) / 2;
        g.fillRect(rectX, rectY, RECT_WIDTH, RECT_WIDTH);
    }
}   

主GUI将所有内容挂钩:

public class MyColorFoo extends JPanel {
    private MyColorPanel myColorPanel = new MyColorPanel(1000, 400);

    public MyColorFoo() {
        JPanel slidersPanel = new JPanel(new GridLayout(0, 1, 3, 3));
        slidersPanel.add(new ColorSliderPanel("Red", Color.RED, myColorPanel));
        slidersPanel.add(new ColorSliderPanel("Green", Color.GREEN, myColorPanel));
        slidersPanel.add(new ColorSliderPanel("Blue", Color.BLUE, myColorPanel));

        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        setLayout(new BorderLayout(3, 3));
        add(myColorPanel, BorderLayout.CENTER);
        add(slidersPanel, BorderLayout.PAGE_END);
    }

更新更完整的版本:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class ColorExample extends JPanel {
    private ColorSliderPanel[] sliders = { 
            new ColorSliderPanel(Color.RED, "Red"),
            new ColorSliderPanel(Color.GREEN, "Green"), 
            new ColorSliderPanel(Color.BLUE, "Blue") };
    private JPanel colorPanel = new JPanel();

    public ColorExample() {
        JPanel sliderPanel = new JPanel(new GridLayout(0, 1));
        for (ColorSliderPanel colorSliderPanel : sliders) {
            sliderPanel.add(colorSliderPanel);
            colorSliderPanel.addListener(evt -> setColorPanelBackground());
        }

        colorPanel.setPreferredSize(new Dimension(600, 300));
        setLayout(new GridLayout(0, 1));
        add(colorPanel);
        add(sliderPanel);
        setColorPanelBackground();
    }

    private void setColorPanelBackground() {
        int rgb = 0;
        for (ColorSliderPanel colorSliderPanel : sliders) {
            Color c = colorSliderPanel.getColor();
            int value = colorSliderPanel.getValue();

            // numeric magic here:
            rgb |= value * (0x10101 & c.getRGB());
        }
        colorPanel.setBackground(new Color(rgb));
    }

    private static void createAndShowGui() {
        ColorExample mainPanel = new ColorExample();

        JFrame frame = new JFrame("Color Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

@SuppressWarnings("serial")
class ColorSliderPanel extends JPanel {
    private final static int MAX_VALUE = 255;
    private Color color;
    private String text;
    private JSlider slider = new JSlider(0, MAX_VALUE, MAX_VALUE / 2);

    public ColorSliderPanel(Color color, String text) {
        this.color = color;
        this.text = text;
        setBackground(color);

        // lighter color
        int colorInt = color.getRGB() | 0x7f7f7f;
        slider.setBackground(new Color(colorInt));
        slider.setMajorTickSpacing(50);
        slider.setMinorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

        setLayout(new BorderLayout());
        add(slider);
        setBorder(BorderFactory.createTitledBorder(text));
    }

    public Color getColor() {
        return color;
    }

    public String getText() {
        return text;
    }

    public int getValue() {
        return slider.getValue();
    }

    public void addListener(ChangeListener listener) {
        slider.addChangeListener(listener);
    }

}