Reputation:
I have a little problem on my project, I don't know how to handle the setColor option in this case I try to. The main Problem is that I can't give the color to the paintComponent ore I don't know how to do that. I create an object from Scatterplot (Scatterplot sp = new Scatterplot) did not work.
here is my code for the 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);
}
}
}
The JColorChooser is implemented here:
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);
}
}
That is all (and some more Panels) added to a Frame:
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);
}
The daterModel Class (This is returned by an 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();
}
}
Upvotes: 0
Views: 433
Reputation: 8348
This is a broad question with lots of plausible answers: here's one way to do it:
Color
instance variable in the Scatterplot (with a setter method and default color value)g.setColor(myColor);
Scatterplot
object to GuiOptionPanel
GuiOptionPanel
, the JColorChooser.showDialog
method returns a Color object. Assign that to a variable. Color chosenColor = JColorChooser.showDialog( null, "Color", null );
Scatterplot
from (1) and (3)Note that the Color returned from JColorChooser.showDialog may be null if the user cancelled - should add a conditional to make sure it is not null
. You should also call repaint
in the color setter method of (1) if you want the color change to take immediate affect.
Upvotes: 0
Reputation: 13
If I understand you correctly, you probably want to do this:
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
[..]
}
In other words, a component is painted by its Graphics object, which handles things like color.
Upvotes: 1