📄 postscriptgraphics.java
字号:
PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
grabber.grabPixels();
ColorModel model = ColorModel.getRGBdefault();
// print data to ps
m_printstream.println("gsave");
m_printstream.println(xTransform(xScale(x)) + " " + (yTransform(yScale(y)) - yScale(height)) + " translate");
m_printstream.println(xScale(width) + " " + yScale(height) + " scale");
m_printstream.println(width + " " + height + " " + "8" + " [" + width + " 0 0 " + (-height) + " 0 " + height + "]");
m_printstream.println("{<");
int index;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
index = i * width + j;
m_printstream.print(toHex(model.getRed(pixels[index])));
m_printstream.print(toHex(model.getGreen(pixels[index])));
m_printstream.print(toHex(model.getBlue(pixels[index])));
}
m_printstream.println();
}
m_printstream.println(">}");
m_printstream.println("false 3 colorimage");
m_printstream.println("grestore");
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* calls drawImage(Image,int,int,int,int,Color,ImageObserver) with the color
* WHITE as background
*
* @see #drawImage(Image,int,int,int,int,Color,ImageObserver)
* @see Color#WHITE
*/
public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer){
return drawImage(img, x, y, width, height, Color.WHITE, observer);
}
/**
* Not implemented
*/
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer){
return false;
}
/**
* calls drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver)
* with Color.WHITE as background color
*
* @see #drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver)
*/
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer){
return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, Color.WHITE, observer);
}
/**
* Draw a line in current pen color.
*
* @param x1 starting x coord
* @param y1 starting y coord
* @param x2 ending x coord
* @param y2 ending y coord
*/
public void drawLine(int x1, int y1, int x2, int y2){
setStateToLocal();
m_printstream.println(xTransform(xScale(x1)) + " " + yTransform(yScale(y1)) + " moveto " + xTransform(xScale(x2)) + " " + yTransform(yScale(y2)) + " lineto stroke");
}
/**
* Draw an Oval outline in current pen color.
*
* @param x x-axis center of oval
* @param y y-axis center of oval
* @param width oval width
* @param height oval height
*/
public void drawOval(int x, int y, int width, int height){
setStateToLocal();
m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " false Oval");
}
/**
* Not implemented
*/
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints){}
/**
* Not implemented
*/
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints){}
/**
* Draw an outlined rectangle in current pen color.
*
* @param x starting x coord
* @param y starting y coord
* @param width rectangle width
* @param height rectangle height
*/
public void drawRect(int x, int y, int width, int height){
setStateToLocal();
m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " false Rect");
}
/**
* Not implemented
*/
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){}
/**
* Not implemented
*/
public void drawString(AttributedCharacterIterator iterator, int x, int y){}
/**
* Draw text in current pen color.
*
* @param String Text to output
* @param x starting x coord
* @param y starting y coord
*/
public void drawString(String str, int x, int y){
setStateToLocal();
m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " moveto" + " (" + str + ") show stroke");
}
/**
* Draw a filled rectangle with 3D effect in current pen color.
* (Current implementation: draw simple filled rectangle)
*
* @param x starting x coord
* @param y starting y coord
* @param width rectangle width
* @param height rectangle height
* @param raised True: appear raised, False: appear etched
*/
public void fill3DRect(int x, int y, int width, int height, boolean raised){
fillRect(x, y, width, height);
}
/**
* Not implemented
*/
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle){}
/**
* Draw a filled Oval in current pen color.
*
* @param x x-axis center of oval
* @param y y-axis center of oval
* @param width oval width
* @param height oval height
*/
public void fillOval(int x, int y, int width, int height){
setStateToLocal();
m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Oval");
}
/**
* Not implemented
*/
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints){}
/**
* Not implemented
*/
public void fillPolygon(Polygon p){}
/**
* Draw a filled rectangle in current pen color.
*
* @param x starting x coord
* @param y starting y coord
* @param width rectangle width
* @param height rectangle height
*/
public void fillRect(int x, int y, int width, int height){
if (width == m_extent.width && height == m_extent.height) {
clearRect(x, y, width, height); // if we're painting the entire background, just make it white
} else {
if (DEBUG)
m_printstream.println("% fillRect");
setStateToLocal();
m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Rect");
}
}
/**
* Not implemented
*/
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){}
/**
* Not implemented
*/
public void finalize(){}
/**
* Not implemented
*/
public Shape getClip(){
return(null);
}
/**
* This returns the full current drawing area
* @return full drawing area
*/
public Rectangle getClipBounds(){
return(new Rectangle(0, 0, m_extent.width, m_extent.height));
}
/**
* This returns the full current drawing area
* @return full drawing area
*/
public Rectangle getClipBounds(Rectangle r) {
r.setBounds(0, 0, m_extent.width, m_extent.height);
return r;
}
/**
* Not implemented
*/
public Rectangle getClipRect() {return null;}
/**
* Get current pen color.
*
* @return current pen color.
*/
public Color getColor(){
return (m_localGraphicsState.getColor());
}
/**
* Get current font.
*
* @return current font.
*/
public Font getFont(){
return (m_localGraphicsState.getFont());
}
/**
* Get Font metrics
*
* @param f Font
* @return Font metrics.
*/
public FontMetrics getFontMetrics(Font f){
return(Toolkit.getDefaultToolkit().getFontMetrics(f));
}
/**
* Not implemented
*/
public void setClip(int x, int y, int width, int height) {}
/**
* Not implemented
*/
public void setClip(Shape clip){}
/**
* Set current pen color. Default to black if null.
*
* @param c new pen color.
*/
public void setColor(Color c){
if (c != null){
m_localGraphicsState.setColor(c);
if (m_psGraphicsState.getColor().equals(c)) {
return;
}
m_psGraphicsState.setColor(c);
} else {
m_localGraphicsState.setColor(Color.black);
m_psGraphicsState.setColor(getColor());
}
m_printstream.print(getColor().getRed()/255.0);
m_printstream.print(" ");
m_printstream.print(getColor().getGreen()/255.0);
m_printstream.print(" ");
m_printstream.print(getColor().getBlue()/255.0);
m_printstream.println(" setrgbcolor");
}
/**
* replaces the font (PS name) if necessary and returns the new name
*/
private static String replacePSFont(String font) {
String result;
result = font;
// do we have to replace it? -> same style, size
if (m_PSFontReplacement.containsKey(font)) {
result = m_PSFontReplacement.get(font).toString();
if (DEBUG)
System.out.println("switched font from '" + font + "' to '" + result + "'");
}
return result;
}
/**
* Set current font. Default to Plain Courier 11 if null.
*
* @param font new font.
*/
public void setFont(Font font){
if (font != null){
m_localGraphicsState.setFont(font);
if ( font.getName().equals(m_psGraphicsState.getFont().getName())
&& (m_psGraphicsState.getFont().getStyle() == font.getStyle())
&& (m_psGraphicsState.getFont().getSize() == yScale(font.getSize())))
return;
m_psGraphicsState.setFont(new Font(font.getName(), font.getStyle(), yScale(getFont().getSize())));
}
else {
m_localGraphicsState.setFont(new Font ("Courier", Font.PLAIN, 11));
m_psGraphicsState.setFont(getFont());
}
m_printstream.println("/(" + replacePSFont(getFont().getPSName()) + ")" + " findfont");
m_printstream.println(yScale(getFont().getSize()) + " scalefont setfont");
}
/**
* Not implemented
*/
public void setPaintMode(){}
/**
* Not implemented
*/
public void setXORMode(Color c1){}
/**
* Translates the origin of the graphics context to the point (x, y) in the
* current coordinate system. Modifies this graphics context so that its new
* origin corresponds to the point (x, y) in this graphics context's original
* coordinate system. All coordinates used in subsequent rendering operations
* on this graphics context will be relative to this new origin.
*
* @param x the x coordinate.
* @param y the y coordinate.
*/
public void translate(int x, int y){
if (DEBUG)
System.out.println("translate with x = " + x + " and y = " + y);
m_localGraphicsState.setXOffset(m_localGraphicsState.getXOffset() + xScale(x));
m_localGraphicsState.setYOffset(m_localGraphicsState.getYOffset() + yScale(y));
m_psGraphicsState.setXOffset(m_psGraphicsState.getXOffset() + xScale(x));
m_psGraphicsState.setYOffset(m_psGraphicsState.getYOffset() + yScale(y));
}
/***** END overridden Graphics methods *****/
/***** START overridden Graphics2D methods *****/
public FontRenderContext getFontRenderContext(){
return (new FontRenderContext(null,true,true));
}
public void clip(Shape s){}
public Stroke getStroke(){
return(m_localGraphicsState.getStroke());
}
public Color getBackground(){
return(Color.white);
}
public void setBackground(Color c){}
public Composite getComposite(){
return(AlphaComposite.getInstance(AlphaComposite.SRC));
}
public Paint getPaint(){
return((Paint) (new Color(getColor().getRed(),getColor().getGreen(),getColor().getBlue())));
}
public AffineTransform getTransform(){
return(new AffineTransform());
}
public void setTransform(AffineTransform at) {}
public void transform(AffineTransform at) {}
public void shear(double d1, double d2){}
public void scale(double d1, double d2) {
m_localGraphicsState.setXScale(d1);
m_localGraphicsState.setYScale(d2);
if (DEBUG)
System.err.println("d1 = " + d1 + ", d2 = " + d2);
}
public void rotate(double d1, double d2, double d3){}
public void rotate(double d1){}
public void translate(double d1, double d2) {}
public RenderingHints getRenderingHints(){
return(new RenderingHints(null));
}
public void addRenderingHints(Map m){}
public void setRenderingHints(Map m){}
public Object getRenderingHint(RenderingHints.Key key){
return(null);
}
public void setRenderingHint(RenderingHints.Key key, Object o){}
public void setStroke(Stroke s){
if (s != null){
m_localGraphicsState.setStroke(s);
if (s.equals(m_psGraphicsState.getStroke())) {
return;
}
m_psGraphicsState.setStroke(s);
} else {
m_localGraphicsState.setStroke(new BasicStroke());
m_psGraphicsState.setStroke(getStroke());
}
// ouput postscript here to set stroke.
}
public void setPaint(Paint p){
}
public void setComposite(Composite c){}
public GraphicsConfiguration getDeviceConfiguration(){
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return(gd.getDefaultConfiguration());
}
public boolean hit(Rectangle r, Shape s, boolean onstroke){
return(false);
}
public void fill(Shape s){}
public void drawGlyphVector(GlyphVector gv, float f1, float f2){}
public void drawString(AttributedCharacterIterator aci, float f1, float f2){}
public void drawString(String str, float x, float y){
drawString(str,(int)x, (int)y);
}
public void drawRenderableImage(RenderableImage ri, AffineTransform at){}
public void drawRenderedImage(RenderedImage ri, AffineTransform af){}
public void drawImage(BufferedImage bi, BufferedImageOp bio, int i1, int i2){}
public boolean drawImage(Image im, AffineTransform at, ImageObserver io){
return(false);
}
public void draw(Shape s){}
/***** END *****/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -