如何从另一个更改 paintComponent 的颜色 class
How to change color of paintComponent from another class
我在 OvalShape.java 的面板上有一个椭圆形组件,我想更改
的颜色
public class OvalShape extends JPanel {
private int diameter = 100;
private Color myColor = Color.RED;
//create the actual panel
public OvalShape() {
Dimension size = getPreferredSize();
size.width = 300;
size.height= 300;
setPreferredSize(size);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
public void setMyColor(Color theColor) {
this.myColor = theColor;
}
//create the oval
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(0, 0, diameter, diameter);
g.setColor(myColor);
}
//method for setting the diameter
public void setDiameter(int newDiameter) { //method to create diameter
//if diam invalid set to 10
diameter = (newDiameter >=0 ? newDiameter : 10);
repaint(); //repaint panel
}
}
来自 OvalPanel.java 我希望能够改变颜色。这是 class 的样子
public class OvalPanel extends JPanel {
public int diameter = 100; //default diameter of the oval
public JSlider diamSlider;
public JLabel diamLabel;
public OvalShape ovalShape;
public OvalPanel() {
Dimension size = getPreferredSize();
size.width = 500;
setPreferredSize(size);
//create components
diamSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 100);
diamSlider.setSnapToTicks(true);
diamSlider.setMajorTickSpacing(100);
diamSlider.setMinorTickSpacing(25);
diamSlider.setPaintTicks(true);
diamSlider.setPaintLabels(true);
diamLabel = new JLabel("Diameter = " + diameter);
ovalShape = new OvalShape();
ovalShape.setDiameter(diamSlider.getValue());
ovalShape.setMyColor(Color.RED);
//set listeners
diamSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
diamLabel.setText("Diameter = " + diamSlider.getValue());
ovalShape.setDiameter(diamSlider.getValue());
}
});
//set layout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
////////first row////////
gbc.gridx = 0;
gbc.gridy = 0;
add(ovalShape, gbc);
////////second row////////
gbc.gridx = 0;
gbc.gridy = 1;
add(diamSlider, gbc);
////////third row////////
gbc.gridx = 0;
gbc.gridy = 2;
add(diamLabel, gbc);
}
但我真的不明白为什么这不起作用。椭圆形刚出来是黑色的。我尝试应用我在创建直径滑块时所做的相同理论,因为我能够使用 OvalPanel.java
中的滑块更改椭圆的直径
感谢您的帮助
g.fillOval(0, 0, diameter, diameter);
g.setColor(myColor);
您需要在绘制椭圆之前设置颜色。
我在 OvalShape.java 的面板上有一个椭圆形组件,我想更改
的颜色public class OvalShape extends JPanel {
private int diameter = 100;
private Color myColor = Color.RED;
//create the actual panel
public OvalShape() {
Dimension size = getPreferredSize();
size.width = 300;
size.height= 300;
setPreferredSize(size);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
public void setMyColor(Color theColor) {
this.myColor = theColor;
}
//create the oval
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(0, 0, diameter, diameter);
g.setColor(myColor);
}
//method for setting the diameter
public void setDiameter(int newDiameter) { //method to create diameter
//if diam invalid set to 10
diameter = (newDiameter >=0 ? newDiameter : 10);
repaint(); //repaint panel
}
}
来自 OvalPanel.java 我希望能够改变颜色。这是 class 的样子
public class OvalPanel extends JPanel {
public int diameter = 100; //default diameter of the oval
public JSlider diamSlider;
public JLabel diamLabel;
public OvalShape ovalShape;
public OvalPanel() {
Dimension size = getPreferredSize();
size.width = 500;
setPreferredSize(size);
//create components
diamSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 100);
diamSlider.setSnapToTicks(true);
diamSlider.setMajorTickSpacing(100);
diamSlider.setMinorTickSpacing(25);
diamSlider.setPaintTicks(true);
diamSlider.setPaintLabels(true);
diamLabel = new JLabel("Diameter = " + diameter);
ovalShape = new OvalShape();
ovalShape.setDiameter(diamSlider.getValue());
ovalShape.setMyColor(Color.RED);
//set listeners
diamSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
diamLabel.setText("Diameter = " + diamSlider.getValue());
ovalShape.setDiameter(diamSlider.getValue());
}
});
//set layout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
////////first row////////
gbc.gridx = 0;
gbc.gridy = 0;
add(ovalShape, gbc);
////////second row////////
gbc.gridx = 0;
gbc.gridy = 1;
add(diamSlider, gbc);
////////third row////////
gbc.gridx = 0;
gbc.gridy = 2;
add(diamLabel, gbc);
}
但我真的不明白为什么这不起作用。椭圆形刚出来是黑色的。我尝试应用我在创建直径滑块时所做的相同理论,因为我能够使用 OvalPanel.java
中的滑块更改椭圆的直径感谢您的帮助
g.fillOval(0, 0, diameter, diameter);
g.setColor(myColor);
您需要在绘制椭圆之前设置颜色。