调整大小时改变圆的颜色

Change Color of Circle When Resized

我试图让圆圈在通过滑块调整大小时 运行domly 改变颜色。我尝试了几种方法,但我知道我需要在 stateChanged 方法中调用 setColor 方法,但不知道如何调用。

这是我的两个 Classes:

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

2nd Class 我觉得 setColor() 需要在 stateChanged() 中调用

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

编辑:classes 正在 运行 测试仪 class 但其中只有帧信息。

嗯,首先你需要修复 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