我的项目有一个小问题,在这种情况下,我不知道如何处理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类(由接口返回):
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对象绘制,该对象处理诸如颜色之类的事情。