import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class RawDataFromImageFilePDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("RawDataFromImageFilePDF.pdf"));
document.open();
RandomAccessFile rf = new RandomAccessFile("logo.png", "r");
int size = (int) rf.length();
byte imext[] = new byte[size];
rf.readFully(imext);
rf.close();
Image img1 = Image.getInstance(imext);
img1.setAbsolutePosition(50, 500);
document.add(img1);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
2012年12月3日月曜日
GlyphVectorで文字列を電光掲示板風にスクロール
class MarqueePanel extends JComponent implements ActionListener {
public final javax.swing.Timer animator;
private final GlyphVector gv;
private float xx, yy;
public MarqueePanel() {
super();
animator = new javax.swing.Timer(10, this);
String text = "asffdfaaAAASFDsfasdfsdfasdfasd";
Font font = new Font("serif", Font.PLAIN, 100);
FontRenderContext frc = new FontRenderContext(null,true,true);
gv = font.createGlyphVector(frc, text);
LineMetrics lm = font.getLineMetrics(text, frc);
yy = lm.getAscent()/2f + (float)gv.getVisualBounds().getY();
}
@Override public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int cw = getWidth();
int ch = getHeight();
g2.setPaint(Color.WHITE);
g2.draw(new Line2D.Float(0,ch/2f,cw,ch/2f));
g2.setPaint(Color.BLACK);
g2.drawGlyphVector(gv, cw-xx, ch/2f-yy);
xx = (cw+gv.getVisualBounds().getWidth()-xx > 0) ? xx+2f : 0f;
}
@Override public void actionPerformed(ActionEvent e) {
repaint();
}
}
解説
上記のサンプルでは、GlyphVectorやLineMetricsから、テキストのVisualBoundsやAscentを取得して、文字列を描画する位置などを計算しています。
以下のようにTextLayoutを使用する方法もあります。
TextLayout tl = new TextLayout(text, font, frc);
Rectangle2D b = tl.getBounds();
yy = tl.getAscent()/2f + (float)b.getY();
public final javax.swing.Timer animator;
private final GlyphVector gv;
private float xx, yy;
public MarqueePanel() {
super();
animator = new javax.swing.Timer(10, this);
String text = "asffdfaaAAASFDsfasdfsdfasdfasd";
Font font = new Font("serif", Font.PLAIN, 100);
FontRenderContext frc = new FontRenderContext(null,true,true);
gv = font.createGlyphVector(frc, text);
LineMetrics lm = font.getLineMetrics(text, frc);
yy = lm.getAscent()/2f + (float)gv.getVisualBounds().getY();
}
@Override public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int cw = getWidth();
int ch = getHeight();
g2.setPaint(Color.WHITE);
g2.draw(new Line2D.Float(0,ch/2f,cw,ch/2f));
g2.setPaint(Color.BLACK);
g2.drawGlyphVector(gv, cw-xx, ch/2f-yy);
xx = (cw+gv.getVisualBounds().getWidth()-xx > 0) ? xx+2f : 0f;
}
@Override public void actionPerformed(ActionEvent e) {
repaint();
}
}
解説
上記のサンプルでは、GlyphVectorやLineMetricsから、テキストのVisualBoundsやAscentを取得して、文字列を描画する位置などを計算しています。
以下のようにTextLayoutを使用する方法もあります。
TextLayout tl = new TextLayout(text, font, frc);
Rectangle2D b = tl.getBounds();
yy = tl.getAscent()/2f + (float)b.getY();
登録:
投稿 (Atom)