写一个java程序将一首古诗竖着排序从右往左读
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import javax.swing.Jframe;
import javax.swing.JLabel;
public class PoetryTest {
public static void main(String[] args) throws Exception {
final Jframe f = new Jframe();
final ArrayList<Poetry> ps = PContents.load();
final PoetryLabel plabel = new PoetryLabel();
f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
f.add(plabel);
f.setSize(320,268);
f.setLocationRelativeTo(null);
f.setVisible(true);
plabel.setPoetry(ps.get(0));
f.setTitle(plabel.getTitle()+"-"+plabel.getAuthor());
plabel.addMouseListener(new MouseAdapter(){
int index;
public void mouseClicked(MouseEvent e){
++index;
index=index%ps.size();
Poetry p = ps.get(index);
plabel.setPoetry(p);
f.setTitle(plabel.getTitle()+"-"+plabel.getAuthor());
}
});
}
}
//呈现诗歌的标签类
class PoetryLabel extends JLabel{
private static final Color tColor = Color.white;//标题颜色
private static final Color aColor = Color.yellow;//作者颜色
private static final Color cColor = Color.LIGHT_GRAY;//内容颜色
private static final int fontSize=16;//字体大小
private static final int tType = Font.BOLD;//标题字体样式为粗体
private static final int aType = Font.ITALIC;//作者字体样式为斜体
private static final int cType = Font.PLAIN;//内容字体样式为普通样式
private static final float scaleFactor = 1.2f;//标题字体大小比其它字体大
private static final String fontName="SimSun";//字体名称
private static final int offset=5,space=5;
private Font font;
private AffineTransform at;
private Poetry poem;
public PoetryLabel(){
this.poem=new Poetry();
this.font=new Font(fontName,cType,fontSize);
this.setHorizontalAlignment(JLabel.CENTER);
this.setVerticalAlignment(JLabel.CENTER);
at = new AffineTransform();
at.setToRotation(Math.toRadians(-90));
font = font.deriveFont(at);//将字体逆时针旋转90°
}
public Poetry getPoetry() {
return poem;
}
//设置诗歌,来初始化标签内容
public void setPoetry(Poetry s){
this.poem=s;
this.repaint();
}
public String getTitle(){return poem.title;}
public String getAuthor(){return poem.author;}
//创建图像,先将逆时针旋转了90°的文件画上,然后将整幅图像顺时针旋转90°
private Image createImage(){
int x;
int y=offset;
int w = getTWidth(poem);
int h = getTHeight(poem);
BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
//draw title
g.setColor(tColor);
Font tfont = font.deriveFont(font.getSize()*scaleFactor).deriveFont(tType);
g.setFont(tfont);
y+=tfont.getSize();
char[] cs = poem.title.toCharArray();
for(int i=0; i<cs.length; i++){
x=(i+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, i, 1, x, y);
}
//draw author
y+=space*2+tfont.getSize();
cs = poem.author.toCharArray();
tfont = font.deriveFont(aType);
g.setColor(aColor);
g.setFont(tfont);
for(int i=0; i<cs.length; i++){
x=(i+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, i, 1, x, y);
}
//draw contents
y+=space*3;
g.setColor(cColor);
g.setFont(font);
for(int i=0; i<poem.contents.size(); i++){
y+=fontSize+space;
cs = poem.contents.get(i).toCharArray();
for(int j=0; j<cs.length; j++){
x=(j+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, j, 1, x, y);
}
}
g.dispose();
return this.getRotateImage(img);
}
//将图像顺时针旋转90°
private Image getRotateImage(final BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage newImg = new BufferedImage(h,w,2);
AffineTransformOp op = new AffineTransformOp(AffineTransform.
getRotateInstance(Math.toRadians(90),h/2,h/2),null);
return op.filter(img,newImg);
}
//绘图
public void paint(Graphics gg){
BufferedImage img = (BufferedImage)this.createImage();
int iw = img.getWidth();
int ih = img.getHeight();
Graphics2D g = (Graphics2D)gg;
int w = this.getWidth();
int h = this.getHeight();
g.setPaint(new GradientPaint(0,0,Color.DARK_GRAY,0,h,Color.black));
g.fillRect(0,0,w,h);
int x = (w-iw)/2;
int y = (h-ih)/2;
g.drawImage(img, x, y, this);
g.dispose();
}
//计算图片的高度
private int getTHeight(Poetry s) {
int h = offset*2+space*5;
h+=(int)(fontSize*(scaleFactor+1));
h+=(fontSize+space*2)*s.contents.size();
return h;
}
//计算图片的宽度
private int getTWidth(Poetry s) {
int tl = (int)(s.title.length()*(fontSize*scaleFactor+space)+offset*2);
int al = s.author.length()*(fontSize+space)+offset*2;
int t;
int max = tl>al?tl:al;
for(int i=0; i<s.contents.size(); i++){
t = s.contents.get(i).length()*(fontSize+space)+offset*2;
max=max>t?max:t;
}
return max;
}
}
//诗歌类
class Poetry{
public String title;//标题
public String author;//作者
public ArrayList<String> contents=new ArrayList<String>();//内容
public void addLine(String s){this.contents.add(s);}//加入一行内容
}
//
class PContents{
public static String s=
"苏武庙\r\n"+
"温庭筠\r\n"+
"苏武魂销汉使前,\r\n"+
"古祠高树两茫然。
\r\n"+
"云边雁断胡天月,\r\n"+
"陇上羊归塞草烟。
\r\n"+
"回日楼台非甲帐,\r\n"+
"去时冠剑是丁年。
\r\n"+
"茂陵不见封侯印,\r\n"+
"空向秋波哭逝川。
\r\n"+
"\r\n"+
"贫女\r\n"+
"秦韬玉\r\n"+
"蓬门未识绮罗香,\r\n"+
"拟托良媒益自伤。
\r\n"+
"谁爱风流高格调?\r\n"+
"共怜时世俭梳妆。
\r\n"+
"敢将十指夸针巧,\r\n"+
"不把双眉斗画长。
\r\n"+
"苦恨年年压金线,\r\n"+
"为他人作嫁衣裳。
\r\n"+
"\r\n"+
"宫词\r\n"+
"薛逢\r\n"+
"十二楼中尽晓妆,\r\n"+
"望仙楼上望君王。
\r\n"+
"锁衔金兽连环冷,\r\n"+
"水滴铜龙昼漏长。
\r\n"+
"云髻罢梳还对镜,\r\n"+
"罗衣欲换更添香。
\r\n"+
"遥窥正殿帘开处,\r\n"+
"袍裤宫人扫御床。
\r\n"+
"\r\n"+
"利洲南渡\r\n"+
"温庭筠\r\n"+
"澹然空水对斜晖,\r\n"+
"曲岛苍茫接翠微。
\r\n"+
"波上马嘶看棹去,\r\n"+
"柳边人歇待船归。
\r\n"+
"数丛沙草群鸥散,\r\n"+
"万顷江田一鹭飞。
\r\n"+
"谁解乘舟寻范蠡,\r\n"+
"五湖烟水独忘机?\r\n"+
"\r\n"+
"无题二首之一\r\n"+
"李商隐\r\n"+
"凤尾香罗薄几重,\r\n"+
"碧文圆顶夜深缝。
\r\n"+
"扇裁月魄羞难掩,\r\n"+
"车走雷声语未通。
\r\n"+
"曾是寂寥金烬暗,\r\n"+
"断无消息石榴红。
\r\n"+
"斑骓只系垂杨岸,\r\n"+
"何处西南任好风?\r\n"+
"\r\n"+
"无题二首之二\r\n"+
"李商隐\r\n"+
"重帷深下莫愁堂,\r\n"+
"卧后清宵细细长。
\r\n"+
"神女生涯原是梦,\r\n"+
"小姑居处本无郎。
\r\n"+
"风波不信菱枝弱,\r\n"+
"月露谁教桂叶香?\r\n"+
"直道相思了无益,\r\n"+
"未妨惆怅是清狂。
\r\n"+
"\r\n"+
"金陵图\r\n"+
"韦庄\r\n"+
"江雨霏霏江草齐,\r\n"+
"六朝如梦鸟空啼。
\r\n"+
"无情最是台城柳,\r\n"+
"依旧烟笼十里堤。
\r\n" +
"\r\n"
;
public static ArrayList<Poetry> load() throws Exception{
ArrayList<Poetry> list = new ArrayList<Poetry>();
BufferedReader br = new BufferedReader(new StringReader(s));
Poetry p = null;
String t = null;
while((t = br.readLine()) != null){
if(p==null)
p=new Poetry();
if(t.isEmpty()){
list.add(p);
p=null;
}
else if(p.title==null)
p.title=t;
else if(p.author==null)
p.author=t;
else
p.addLine(t);
}
return list;
}
}