美化圆形加粗import javax.swing.*; import java.awt.*; public class DrawCircle extends JPanel implements Runnable{ int angle=0; Thread t; public DrawCircle(){ this.setPreferredSize(new Dimension(500,500)); this.setBackground(Color.orange); t=new Thread(this); t.start(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.fillArc(50, 50, 300, 300, 0, angle); } public static void main(String args[]){ JFrame f=new JFrame("画扇形"); f.getContentPane().add(new DrawCircle()); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void run() { // TODO Auto-generated method stub try{ for(int i=0;i<12;i++){ Thread.sleep(100); angle=angle+30; repaint();//重画 重新调用paintComponent } }catch(Exception ex){ } } }
时间: 2024-02-14 12:04:48 浏览: 15
你可以尝试使用 Graphics2D 类的方法实现对圆形的美化和加粗,具体实现步骤如下:
1. 在 paintComponent() 方法中实例化 Graphics2D 对象。
2. 设置 Graphics2D 对象的 Stroke 属性为一个新的 BasicStroke 对象,该对象的宽度为你想要的粗细值。
3. 调用 Graphics2D 对象的 draw() 或 fill() 方法来绘制圆形。
修改后的代码如下:
import javax.swing.*;
import java.awt.*;
public class DrawCircle extends JPanel implements Runnable{
int angle=0;
Thread t;
public DrawCircle(){
this.setPreferredSize(new Dimension(500,500));
this.setBackground(Color.orange);
t=new Thread(this);
t.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(5.0f)); // 设置线条粗细为 5 像素
g2d.fillArc(50, 50, 300, 300, 0, angle);
}
public static void main(String args[]){
JFrame f=new JFrame("画扇形");
f.getContentPane().add(new DrawCircle());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
for(int i=0;i<12;i++){
Thread.sleep(100);
angle=angle+30;
repaint();//重画 重新调用paintComponent
}
}catch(Exception ex){}
}
}