62,628
社区成员
发帖
与我相关
我的任务
分享
package chunsen.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/** Created by chunsen on 15-6-11.*/
public class OrvalText extends JComponent{
private String text;
private int radius;
private double startAngle =0;
public OrvalText(String text){
this.text = text;
radius = 100;
}
public void setRadius(int radius) {
if(this.radius != radius) {
this.radius = radius;
repaint();
}
}
public void setStartAngle(double startAngle) {
if(this.startAngle != startAngle) {
this.startAngle = startAngle;
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
Dimension d = getSize();
Rectangle rec = new Rectangle(d);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getBackground());
g2d.fill(rec);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(getForeground());
g2d.setFont(getFont());
g2d.translate(rec.getCenterX(), rec.getCenterY());
g2d.translate(0, -radius);
g2d.rotate(Math.PI/2- startAngle,0,radius);
int length = text.length();
for(int i= 0;i<length;i++){
g2d.drawString(text.substring(i,i+1),0,0);
g2d.rotate(0.2,0,radius);
}
}
public static void main(String[] args){
JFrame frame =new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final OrvalText text = new OrvalText("This is a test text");
text.setFont(new Font("Dialog", Font.BOLD, 12));
text.setForeground(Color.RED);
frame.setLayout(new BorderLayout());
frame.add(text, BorderLayout.CENTER);
frame.setBounds(200, 200, 500, 500);
frame.setVisible(true);
JPanel setting = new JPanel(new FlowLayout(FlowLayout.LEADING));
final JTextField radius = new JTextField("100",8);
final JTextField startAngle = new JTextField("0",8);
setting.add(new JLabel("Radius:"));
setting.add(radius);
setting.add(new JLabel(" Start Angle:"));
setting.add(startAngle);
JButton ok = new JButton("Apply");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double r = Double.valueOf(radius.getText().trim());
double a = Double.valueOf(startAngle.getText().trim());
text.setRadius((int)r);
text.setStartAngle(a);
}
});
setting.add(ok);
frame.add(setting,BorderLayout.SOUTH);
}
}
demo 奉上,忘好评