重绘没有做任何事情

时间:2017-11-22 06:55:35

标签: java swing jframe jpanel repaint

我正在尝试使用GUI滑块制作矩形更改颜色。我知道这可以通过改变背景来完成,但我正在尝试使用重绘。

我知道重绘会保留请求并随意执行。我正试图寻找一种解决方法因为我被卡住了。

我已经阅读了repaint();和repaintManager并试图操纵我的代码,但我仍然无法得到我想要的输出。

任何帮助将不胜感激。

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 *
 */
public class MyColorChooser2 extends JPanel {

    private JFrame frame;
    private JLabel redLabel, greenLabel, blueLabel;
    private JSlider redSlider, greenSlider, blueSlider;
    private JTextField redTextField, greenTextField, blueTextField;

    private JPanel redPanel, greenPanel, bluePanel, colorPanel, paintPanel;

    private int holdNbr1, holdNbr2, holdNbr3;
    DrawPanel rect = new DrawPanel();

    public MyColorChooser2() {
        JFrame frame = new JFrame();

        frame.setTitle("Color Chooser");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // set up labels
        redLabel = new JLabel("Red:");
        greenLabel = new JLabel("Green:");
        blueLabel = new JLabel("Blue:");

        // set up sliders and register their event handler:
        redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 50);
        redSlider.setMajorTickSpacing(10); // create tick every 10
        redSlider.setPaintTicks(true); // paint ticks on slider

        greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 100);
        greenSlider.setMajorTickSpacing(10); // create tick every 10
        greenSlider.setPaintTicks(true); // paint ticks on slider

        blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 200);
        blueSlider.setMajorTickSpacing(10); // create tick every 10
        blueSlider.setPaintTicks(true); // paint ticks on slider

        //slider event handling:
        SliderHandler sliderHandler = new SliderHandler();
        redSlider.addChangeListener(sliderHandler);
        greenSlider.addChangeListener(sliderHandler);
        blueSlider.addChangeListener(sliderHandler);

        //set up textFields and register their event handler:
        redTextField = new JTextField(3);
        redTextField.setText("50"); //initialize
        redTextField.setEditable(false);
        redTextField.setText("" + redSlider.getValue());

        greenTextField = new JTextField(3);
        greenTextField.setText("100"); //initialize
        greenTextField.setEditable(false);
        greenTextField.setText("" + greenSlider.getValue());

        blueTextField = new JTextField(3);
        blueTextField.setText("200"); //initialize
        blueTextField.setEditable(false);
        blueTextField.setText("" + blueSlider.getValue());


        // build colorPanel:
        // build redPanel:    
        redPanel = new JPanel();
        redPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        redPanel.add(redLabel);
        redPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        redPanel.add(redSlider);
        redPanel.add(redTextField);
        // build greenPanel:
        greenPanel = new JPanel();
        greenPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        greenPanel.add(greenLabel);
        greenPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        greenPanel.add(greenSlider);
        greenPanel.add(greenTextField);
        // build bluePanel:
        bluePanel = new JPanel();
        bluePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        bluePanel.add(blueLabel);
        bluePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        bluePanel.add(blueSlider);
        bluePanel.add(blueTextField);

        colorPanel = new JPanel();
        colorPanel.add(redPanel);
        colorPanel.add(greenPanel);
        colorPanel.add(bluePanel);

        frame.setLayout(new BorderLayout());
        frame.add(rect, BorderLayout.CENTER);
        frame.add(colorPanel, BorderLayout.SOUTH);

        frame.setPreferredSize(new Dimension(900, 300));
        frame.setVisible(true);
        frame.pack();

    }

    public class DrawPanel extends JPanel {

        private Color color;
        private int red = 50, blue = 100, green = 200;
        private Graphics g;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            changeColor();
            g.setColor(color);

            g.fillRect(10, 10, 880, 200);
            g.dispose();

        }

        public void DrawPanel(Color c) {

            color = c;

            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();

        }

        void changeColor() {

            color = new Color(red, green, blue);
            this.color = color;

        }

        public void setRed(int r) {
            red = r;
            changeColor();

        }

        public void setBlue(int b) {
            blue = b;
            changeColor();

        }

        public void setGreen(int g) {
            green = g;
            changeColor();

        }
    }

    private class SliderHandler implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider change = (JSlider) e.getSource();
            DrawPanel draw = new DrawPanel();
            int value;

            if (change == redSlider) {
                value = change.getValue();
                redTextField.setText(String.valueOf(value));
                draw.setRed(value);
            } else if (change == greenSlider) {
                value = change.getValue();
                greenTextField.setText(String.valueOf(value));
                draw.setGreen(value);
            } else if (change == blueSlider) {
                value = change.getValue();
                blueTextField.setText(String.valueOf(value));
                draw.setBlue(value);
            }

            draw.changeColor();
            draw.repaint();
        }

    }

    public static void main(String args[]) {
        MyColorChooser2 color = new MyColorChooser2();
    }
}

1 个答案:

答案 0 :(得分:1)

并不是说它没有做任何事情。问题是你被困住了,因为每次你做出改变时你都试图更新不同的组件。您正在重新创建状态更改的多个面板。

我添加了有关我为使其正常工作所做的更改的评论。如果您需要更多帮助,请询问。干杯!

您是否愿意尝试使用此代码:

public class MyColorChooser2 extends JPanel {

    private JFrame frame;
    private DrawPanel drawPanel;

    private JLabel redLabel, greenLabel, blueLabel;
    private JSlider redSlider, greenSlider, blueSlider;
    private JTextField redTextField, greenTextField, blueTextField;

    private JPanel redPanel, greenPanel, bluePanel, colorPanel, paintPanel;

    private int holdNbr1, holdNbr2, holdNbr3;
    private Color initialColor = Color.RED;

    public MyColorChooser2() {
        JFrame frame = new JFrame();
        frame.setTitle("Color Chooser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Let DrawPanel choose the initial color. I don't care.
        drawPanel = new DrawPanel();
        // This way I control what is the initial color
        //drawPanel = new DrawPanel(initialColor);

        // set up labels
        redLabel = new JLabel("Red:");
        greenLabel = new JLabel("Green:");
        blueLabel = new JLabel("Blue:");

        // set up sliders and register their event handler:
        redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, initialColor.getRed());
        redSlider.setMajorTickSpacing(10); // create tick every 10
        redSlider.setPaintTicks(true); // paint ticks on slider

        greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, initialColor.getGreen());
        greenSlider.setMajorTickSpacing(10); // create tick every 10
        greenSlider.setPaintTicks(true); // paint ticks on slider

        blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, initialColor.getBlue());
        blueSlider.setMajorTickSpacing(10); // create tick every 10
        blueSlider.setPaintTicks(true); // paint ticks on slider

        // slider event handling:
        SliderHandler sliderHandler = new SliderHandler();
        redSlider.addChangeListener(sliderHandler);
        greenSlider.addChangeListener(sliderHandler);
        blueSlider.addChangeListener(sliderHandler);

        // set up textFields and register their event handler:
        redTextField = new JTextField(3);
        redTextField.setText("50"); // initialize
        redTextField.setEditable(false);
        redTextField.setText("" + redSlider.getValue());

        greenTextField = new JTextField(3);
        greenTextField.setText("100"); // initialize
        greenTextField.setEditable(false);
        greenTextField.setText("" + greenSlider.getValue());

        blueTextField = new JTextField(3);
        blueTextField.setText("200"); // initialize
        blueTextField.setEditable(false);
        blueTextField.setText("" + blueSlider.getValue());

        // build colorPanel:
        // build redPanel:
        redPanel = new JPanel();
        redPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        redPanel.add(redLabel);
        redPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        redPanel.add(redSlider);
        redPanel.add(redTextField);
        // build greenPanel:
        greenPanel = new JPanel();
        greenPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        greenPanel.add(greenLabel);
        greenPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        greenPanel.add(greenSlider);
        greenPanel.add(greenTextField);
        // build bluePanel:
        bluePanel = new JPanel();
        bluePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        bluePanel.add(blueLabel);
        bluePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        bluePanel.add(blueSlider);
        bluePanel.add(blueTextField);

        colorPanel = new JPanel();
        colorPanel.add(redPanel);
        colorPanel.add(greenPanel);
        colorPanel.add(bluePanel);

        frame.setLayout(new BorderLayout());
        frame.add(drawPanel, BorderLayout.CENTER);
        frame.add(colorPanel, BorderLayout.SOUTH);

        frame.setPreferredSize(new Dimension(900, 300));
        frame.setVisible(true);
        frame.pack();

    }

    private class DrawPanel extends JPanel {

        private Color color;
        private int red = 255, blue = 0, green = 0;

        @Override
        protected void paintComponent(Graphics g) {
            // I've removed the call to changeColor because it was creating
            // an infinite loop of revalidates and repaints.
            // Now, the paintComponent just finishes the job it was required to.
            super.paintComponent(g);
            g.setColor(color);
            g.fillRect(10, 10, 880, 200);
        }

        public DrawPanel() {
            this(Color.BLUE);
        }

        public DrawPanel(Color c) {
            color = c;
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();

            // I'm calling this first repaint here just to make
            // sure the panel is initiated the way we want with 
            // the given Color
            repaint();
        }

        void changeColor() {
            color = new Color(red, green, blue);
            // We just need to change the color and call this repaint here
            // so that the paintComponent can do its job
            repaint();
        }

        public void setRed(int r) {
            red = r;
            changeColor();
        }

        public void setBlue(int b) {
            blue = b;
            changeColor();
        }

        public void setGreen(int g) {
            green = g;
            changeColor();
        }
    }

    private class SliderHandler implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider change = (JSlider) e.getSource();
            int value;

            // I've removed this line of code because you were
            // recreating the drawingPanel. That's not what you want.
            // You want to reuse the same panel.
            // DrawPanel draw = new DrawPanel();

            if (change == redSlider) {
                value = change.getValue();
                redTextField.setText(String.valueOf(value));
                drawPanel.setRed(value);
            } else if (change == greenSlider) {
                value = change.getValue();
                greenTextField.setText(String.valueOf(value));
                drawPanel.setGreen(value);
            } else if (change == blueSlider) {
                value = change.getValue();
                blueTextField.setText(String.valueOf(value));
                drawPanel.setBlue(value);
            }

            // You don't need to call those methods, because the
            // changeColor will be called by the settings 
            // of Red, Green            and Blue
            //       draw.changeColor();
            //       draw.repaint();

        }

    }

    public static void main(String args[]) {
        MyColorChooser2 color = new MyColorChooser2();
    }
}