java - PaintComponent如何从外部设置颜色

标签 java swing paintcomponent jcolorchooser

我的项目有一个小问题,我不知道在我尝试的这种情况下如何处理 setColor 选项。 主要问题是我无法为 PaintComponent 矿石提供颜色,我不知道该怎么做。我从 Scatterplot 创建一个对象(Scatterplot sp = new Scatterplot)不起作用。

这是我的plotPanel 代码。

public class Scatterplot extends JPanel {

    List<Double> values_x = new ArrayList<>();
    List<Double> values_y = new ArrayList<>();

    protected double maxValue_x, minValue_x, maxValue_y, minValue_y;


    public Scatterplot(List<Double> variableValues_1, List<Double> variableValues_2) {
        values_x = variableValues_1;
        maxValue_x = Collections.max(values_x);
        minValue_x = Collections.min(values_x);

        values_y = variableValues_2;
        maxValue_y = Collections.max(values_y);
        minValue_y = Collections.min(values_y);

    }



    @Override
    protected void paintComponent(Graphics g) {

        int m = 30;
        double width = getWidth();
        double height = getHeight();

        double x1 = (width-2*m) / (maxValue_x - minValue_x);
        double y1 = (height-2*m) / (maxValue_y - minValue_y);

        for (int i = 0; i < values_x.size(); i++) {
            double value_x = values_x.get(i);  
            double value_y = values_y.get(i);

            g.fillOval((int)(2*m + x1*(value_x-minValue_x)-2*m), (int)(height - (y1*(value_y-minValue_y))-2*m), 2*m, 2*m);

        }
    }      
}

JColorChooser 在这里实现:

public class GuiOptionPanel extends JPanel {


    public GuiOptionPanel(DataModel dataModel) {
        JPanel optionPanel = new JPanel(new GridLayout(7,1));

        JPanel menuPanel = new JPanel();
        JLabel menuLabel = new JLabel("Menu");
        menuLabel.setFont(menuLabel.getFont().deriveFont(Font.BOLD));
        menuPanel.add(menuLabel);
        optionPanel.add(menuPanel);

        JButton setColorButton = new JButton("Set Color");
        optionPanel.add(setColorButton);
        setColorButton.addActionListener((ActionEvent e) -> {
            JColorChooser.showDialog( null, "Color", null );
        });

        add(optionPanel);
    }
}

这就是添加到框架中的所有内容(以及更多面板):

public GuiFrame(DataModel dataModel) {
    setSize(FRAME_WIDTH, FRAME_HIGHT);
    /**
     * Create Objects
     */
    GuiInfoPanel ip = new GuiInfoPanel(dataModel);
    GuiOptionPanel op = new GuiOptionPanel(dataModel);
    JComponent sp = new Scatterplot(dataModel.getVariableValues(0), dataModel.getVariableValues(1));
    JComponent hg1 = new Histograms(dataModel.getVariableValues(0));
    JComponent hg2 = new Histograms(dataModel.getVariableValues(1));
    /**
     * Create Panels
     */
    createMainPanel();

    /**
     * Add Panels
     */
    this.add(mainPanel);
    this.add(ip, BorderLayout.SOUTH);
    menuPanel.add(op, BorderLayout.NORTH);
    scatterplotPanel.add(sp, BorderLayout.CENTER);
    histogram1.add(hg1, BorderLayout.CENTER);
    histogram2.add(hg2, BorderLayout.CENTER);
    }

daterModel 类(由接口(interface)返回):

package dataplotproject;

import java.util.ArrayList;
import java.util.List;

public class DataModel {

    List<Variable> listOfEveryVariable = new ArrayList<>();
    String fileName = "";

    public DataModel(List<Variable> listOfEveryVariable2, String fileName2) {
        listOfEveryVariable = listOfEveryVariable2;
        fileName = fileName2;
    }

    public List<Variable> getVariables() {
        return listOfEveryVariable;
    }

    public String getFileName() {
        return fileName;
    }

    public List<String> getVariableNames() {
        List<String> names = new ArrayList<>();
        for (Variable obj : listOfEveryVariable) {
            names.add(obj.getName());
        }
        return names;
    }

    public List<Double> getVariableValues(int i){
       return listOfEveryVariable.get(i).getValueList();
    }
}

最佳答案

如果我理解正确,你可能想要这样做:

@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
[..]
}

换句话说,组件是由其 Graphics 对象绘制的,该对象处理颜色等内容。

关于java - PaintComponent如何从外部设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30378604/

相关文章:

java - 在 Swing 中,有没有办法从工具包中提取预定义的鼠标光标图像?

java - 如何刷新面板?

Java-图形-导入背景图像

java - XWPFRun 生成修剪了空格的运行

java - Android Java on toggle 改变颜色

java - 如何用正则表达式截取URL

java - 标记 x 和 y 轴 java gui

java - 从字符串Java的值调用函数

java - 如何让多个 JButton 工作?

java - 重新绘制 JPanel 会略微移动其上的图像