补张运行结果图
package net.gerhut.coolest;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
public class Coolest {
private Coolest() {
(new MainFrame()).setVisible(true);
}
public static void main(String[] args) {
new Coolest();
}
class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private String bigText="古力斯特";
private String smallText="Coolest!";
private Color backColor=Color.BLACK;
IconPanel pnlIcon=new IconPanel();
public MainFrame()
{
this.setLayout(new BorderLayout());
this.add(pnlIcon,BorderLayout.CENTER);
this.add(new ControlPanel(),BorderLayout.EAST);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("Coolest!");
}
class IconPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public IconPanel()
{
this.setPreferredSize(new Dimension(420,420));
}
@Override
public void paint(Graphics g)
{
(new IconPainter(bigText,smallText,backColor)).startPaint((Graphics2D)g,true);
}
}
class ControlPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public ControlPanel()
{
this.setLayout(new BorderLayout());
JPanel pnlUp = new JPanel(new FlowLayout());
this.add(pnlUp,BorderLayout.NORTH);
final JTextField tfBigText=new JTextField(bigText,5);
tfBigText.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent e) {
bigText=tfBigText.getText();
pnlIcon.repaint();
}
@Override
public void insertUpdate(DocumentEvent e) {
bigText=tfBigText.getText();
pnlIcon.repaint();
}
@Override
public void removeUpdate(DocumentEvent e) {
bigText=tfBigText.getText();
pnlIcon.repaint();
}
});
pnlUp.add(tfBigText);
final JTextField tfSmallText=new JTextField(smallText,10);
tfSmallText.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent e) {
smallText=tfSmallText.getText();
pnlIcon.repaint();
}
@Override
public void insertUpdate(DocumentEvent e) {
smallText=tfSmallText.getText();
pnlIcon.repaint();
}
@Override
public void removeUpdate(DocumentEvent e) {
smallText=tfSmallText.getText();
pnlIcon.repaint();
}
});
pnlUp.add(tfSmallText);
JButton btnSave=new JButton("保存");
btnSave.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc=new JFileChooser(){
private static final long serialVersionUID = 1L;
public void approveSelection()
{
File savedFile = getSelectedFile();
if (savedFile.exists())
{
int overwriteSelect = JOptionPane.showConfirmDialog(this,
"文件" + savedFile.getName() + "已存在,是否覆盖?",
"是否覆盖?",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (overwriteSelect != JOptionPane.YES_OPTION)
{
return;
}
}
super.approveSelection();
}
};
fc.setFileFilter(new FileFilter(){
@Override
public boolean accept(File f) {
return f.getName().substring(f.getName().lastIndexOf(".")+1).toLowerCase().equals("gif");
}
@Override
public String getDescription() {
return "GIF Image(*.gif).";
}
});
fc.setSelectedFile(new File(bigText+".gif"));
if(fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
{
File f=fc.getSelectedFile();
if(f.getName().endsWith("gif"))f=new File(f.getAbsolutePath(),f.getName()+".gif");
(new IconPainter(bigText,smallText,backColor)).save(fc.getSelectedFile());
}
}
});
pnlUp.add(btnSave);
final JColorChooser ccBack=new JColorChooser(backColor);
ccBack.setPreviewPanel(new JPanel());
ccBack.removeChooserPanel(ccBack.getChooserPanels()[2]);
ccBack.getSelectionModel().addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
backColor=ccBack.getColor();
pnlIcon.repaint();
}
});
this.add(ccBack,BorderLayout.CENTER);
}
}
}
class IconPainter {
private final int GRAD_R=15;
private String bigText="";
private String smallText="";
private Color backColor=Color.BLACK;
public IconPainter(){}
public IconPainter(String bigText,String smallText)
{
this.bigText=bigText;
this.smallText=smallText;
}
public IconPainter(String bigText,String smallText,Color backColor)
{
this.bigText=bigText;
this.smallText=smallText;
this.backColor=backColor;
}
public void setBigText(String bigText) {
this.bigText = bigText;
}
public String getBigText() {
return bigText;
}
public void setSmallText(String smallText) {
this.smallText = smallText;
}
public String getSmallText() {
return smallText;
}
public void setBackColor(Color backColor) {
this.backColor = backColor;
}
public Color getBackColor() {
return backColor;
}
public void startPaint(Graphics2D graph,boolean clear)
{
Image img=new BufferedImage(420,420,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
if(clear)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, 420, 420);
}
for(int i=0;i<=GRAD_R;i++)
{
Color c=new Color(
255-(255-backColor.getRed())*i/GRAD_R,
255-(255-backColor.getGreen())*i/GRAD_R,
255-(255-backColor.getBlue())*i/GRAD_R);
g.setColor(c);
g.fillOval(i+10, i+10, 400-2*i, 400-2*i);
}
g.setFont(new Font("黑体",Font.BOLD,160));
g.setColor(Color.WHITE);
String upText;
String downText;
if(bigText.length()<=2)
{
upText=bigText;
downText="";
}else if(bigText.length()<=4)
{
upText=bigText.substring(0,2);
downText=bigText.substring(2);
}else{
upText=bigText.substring(0,2);
downText=bigText.substring(2,4);
}
FontMetrics metrics=g.getFontMetrics();
g.drawString(upText,
210-metrics.stringWidth(upText)/2, 165);
g.drawString(downText,
210-metrics.stringWidth(downText)/2, 365);
g.setFont(new Font("Wide Latin",Font.PLAIN,40));
metrics=g.getFontMetrics();
g.setColor(Color.YELLOW);
g.drawString(smallText,
210-metrics.stringWidth(smallText)/2,
210+metrics.getHeight()/2);
if(clear)
{
graph.setColor(Color.WHITE);
graph.fillRect(0, 0, 420, 420);
}
graph.rotate(6,210,210);
graph.drawImage(img,0,0,null);
}
public void save(File f)
{
BufferedImage img = new BufferedImage(420,420,BufferedImage.TYPE_4BYTE_ABGR);
startPaint((Graphics2D) img.getGraphics(),true);
try {
ImageIO.write(img, "gif", f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}