我想知道如何在用户调整滑块值时用度数填充椭圆内部的弧。滑块侦听器位于与弧和椭圆不同的文件中。
答案 0 :(得分:1)
super.paintComponent
,这样可以防止生成任何可能的绘制工件,虽然时髦,但并不是很好。有关详细信息,请参阅Painting in AWT and Swing和Performing Custom Painting reapint
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class FillEllipse {
public static void main(String[] args) {
new FillEllipse();
}
public FillEllipse() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final ArcPane arcPane = new ArcPane();
final JSlider slider = new JSlider(0, 360);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
arcPane.setAngle(value);
}
});
slider.setValue(0);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(arcPane);
frame.add(slider, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ArcPane extends JPanel {
private int angel;
public ArcPane() {
}
public void setAngle(int angel) {
this.angel = angel;
repaint();
}
public int getAngel() {
return angel;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int diameter = Math.min(getWidth(), getHeight());
int x = (getWidth() - diameter) / 2;
int y = (getHeight() - diameter) / 2;
g2d.setColor(Color.RED);
g2d.fillArc(x, y, diameter, diameter, 0, getAngel());
g2d.dispose();
}
}
}