当滑块调整圆圈大小时,我试图让圆圈随机改变颜色。我尝试了几种方法,但我知道我需要在stateChanged方法中调用setColor方法,但不知道如何。
以下是我的两个课程:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.util.Random;
public class OvalPanel extends JPanel
{
private int diameter = 10; // default diameter
private Color myGreen = new Color(0, 255, 0);
private Random rand = new Random();
private int r = rand.nextInt(256);
private int g = rand.nextInt(256);
private int b = rand.nextInt(256);
private Color circleColor = new Color(r, g, b);
// draw an oval of the specified diameter
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(myGreen);
g.fillOval(10, 10, diameter, diameter);
}
// validate and set diameter, then repaint
public void setDiameter(int newDiameter)
{
// if diameter invalid, default to 10
diameter = (newDiameter >= 0 ? newDiameter : 10);
repaint(); // repaint panel
}
public void setColor(int newR, int newG, int newB)
{
r = newR;
g = newG;
b = newB;
}
// used by layout manager to determine preferred size
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}
// used by layout manager to determine minimum size
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public double getDiameter()
{
return diameter;
}
} // end class OvalPanel
第二类我觉得需要在stateChanged()
中调用setColor()import java.awt.BorderLayout;
import java.awt.Color;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class SliderFrame extends JFrame
{
private final JSlider diameterJSlider; // slider to select diameter
private final OvalPanel myPanel; // panel to draw circle
private JTextArea stats;
private double radius;
private double diameter;
private double circumference;
private double area;
// no-argument constructor
public SliderFrame()
{
super("Circle Statistics");
myPanel = new OvalPanel(); // create panel to draw circle
myPanel.setBackground(Color.LIGHT_GRAY);
stats = new JTextArea(1, 100);
// set up JSlider to control diameter value
diameterJSlider =
new JSlider(SwingConstants.HORIZONTAL, 0, 400, 10);
diameterJSlider.setMajorTickSpacing(10); // create tick every 10
diameterJSlider.setPaintTicks(true); // paint ticks on slider
// register JSlider event listener
diameterJSlider.addChangeListener(
new ChangeListener() // anonymous inner class
{
// handle change in slider value
@Override
public void stateChanged(ChangeEvent e)
{
myPanel.setDiameter(diameterJSlider.getValue());
radius = diameterJSlider.getValue() * .5;
diameter = diameterJSlider.getValue();
circumference = Math.PI * diameterJSlider.getValue();
area = Math.PI * radius * radius;
stats.setText(String.format("Radius: %.5f Diameter %.5f Circumference %.5f Area %.5f", radius, diameter, circumference, area));
// help here?
repaint();
}
}
);
add(diameterJSlider, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
add(stats, BorderLayout.NORTH);
}
} // end class SliderFrame
编辑:这些类是在测试器类中运行的,但其中的所有内容都是帧信息。
答案 0 :(得分:0)
首先,您需要修复setColor
- 因为您的paint
方法使用myGreen
,您需要更新myGreen
(并致电repaint
)。像,
public void setColor(int newR, int newG, int newB) {
r = newR;
g = newG;
b = newB;
myGreen = new Color(r, g, b);
repaint();
}
然后,在SliderFrame.stateChanged
中,将Oval
设置为红色(例如)。做点什么,
myPanel.setColor(255, 0, 0);
如果您想要随机颜色,请使用三个随机值调用setColor
。