📄 graphicsinfo.java
字号:
package edu.odu.cs.zeil.AlgAE.Client;import java.awt.Canvas;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Image;import java.awt.Graphics;import java.awt.Rectangle;import edu.odu.cs.zeil.AlgAE.Debug;/** * An extended set of information about the graphics context in which * the frame graphics will be written, including support for double-buffering. * * @author Steven J. Zeil */public class GraphicsInfo { /** * The graphics context in which to display the graph. * This is valid only between an <code>openGraphics</code>, * <code>closeGraphics</code> pair. * */ public Graphics g; /** * The size of the area in which the graph is to be drawn. */ private Dimension canvasSize; public Dimension getCanvasSize() {return canvasSize;} private Canvas canvas; public Canvas getCanvas() {return canvas;} public void setCanvas(Canvas theCanvas) { canvas = theCanvas; font = null; } /** * The font to be used for node and edge labels. */ private Font font; public Font getFont() { if (font == null) { font = new Font ("Helvetica", Font.PLAIN, fontSize); g.setFont (font); FontMetrics fm = g.getFontMetrics(); textOffset.width = (fm.charWidth('X') + 1) / 2; textOffset.height = (fm.getAscent() + 1) / 2; } return font; } public FontMetrics getFontMetrics() { FontMetrics fm; if (font == null) { font = new Font ("Helvetica", Font.PLAIN, fontSize); g.setFont (font); fm = g.getFontMetrics(); textOffset.width = (fm.charWidth('X') + 1) / 2; textOffset.height = (fm.getAscent() + 1) / 2; } else fm = g.getFontMetrics(); return fm; } /** * The size of the font (in points) to be used for node and edge labels. */ private int fontSize; public int getFontSize() {return fontSize;} public void setFontSize(int newFontSize) { fontSize = newFontSize; font = null; } /** * Indicates the amount of space (in pixels/graphics units) to be left * as a border to each side and above and below the text of a node. * This size should be proportional to the fontSize. */ private Dimension textOffset; public Dimension getTextOffset() {return textOffset;} /** * Information about interpolated drawing -- when nodes change position * this is indicated by drawing a series of pictures in which the nodes * are drawn at positions interpolated between their old and new positions. */ private int interpolationTotal; private int interpolationStep; public int getInterpolationStep() {return interpolationStep;} public int getInterpolationTotal() {return interpolationTotal;} public void advanceInterpolation () { if (interpolationStep <= interpolationTotal) ++interpolationStep; } public boolean interpolationCompleted () { return (interpolationStep >= interpolationTotal); } /** * Cancel any remaining interpolational drawings. */ public void cancelInterpolation () { interpolationStep = interpolationTotal; } /** * Set the value of interpolationStep. * @param v Value to assign to interpolationStep. */ public void setInterpolationStep(int v) {interpolationStep = v;} /** * Set the value of interpolationStep and interpolationTotal. * @param v Value to assign to interpolationStep. */ public void setInterpolation(int step, int total) {interpolationStep = step; interpolationTotal = total;} /** * Number of major iterations to be employed when repositioning * nodes. */ public int repositionEffort; /** * Are nodes repositioned automatically at the end of each frame? */ public boolean autoArranging; private Rectangle clipBox; public GraphicsInfo() { g = null; fontSize = 12; canvasSize = new Dimension(60, 40); textOffset = new Dimension (fontSize/2, fontSize/2); repositionEffort = 3; autoArranging = true; repaintFrom = drawTo = null; font = null; interpolationTotal = 5; interpolationStep = 5; clipBox = null; } /** * The image that new drawing is written into. */ private Image drawTo; /** * The image with old drawing that the canvas is repainted from. */ private Image repaintFrom; public Image getRepaintImage() {return repaintFrom;} public static boolean doubleBuffering = false; public void invalidate() { canvasSize.width = canvasSize.height = 1; } public void clip (Rectangle box) { clipBox = new Rectangle(); clipBox.x = box.x; clipBox.y = box.y; clipBox.width = box.width; clipBox.height = box.height; } /** * Initialize the graphics data member <code>g</code> to a valid * graphics context and sets the canvasSize and textOffset values. */ public void openGraphics() { if (doubleBuffering) { if (drawTo == null) { canvasSize = canvas.getSize(); drawTo = canvas.createImage (800,600); repaintFrom = canvas.createImage (800,600); } /* if ((drawTo == null) || (canvasSize.width != canvas.getSize().width) || (canvasSize.height != canvas.getSize().height)) { Debug.show(Debug.image, "old canvasSize", canvasSize); canvasSize = canvas.getSize(); Debug.show(Debug.image, "new canvasSize", canvasSize); drawTo = canvas.createImage (canvasSize.width, canvasSize.height); repaintFrom = canvas.createImage (canvasSize.width, canvasSize.height); } */ g = drawTo.getGraphics(); g.setColor(canvas.getBackground()); g.fillRect(0, 0, canvasSize.width, canvasSize.height); } else { canvasSize = canvas.getSize(); if (clipBox == null) clip(new Rectangle(0, 0, canvasSize.width, canvasSize.height)); drawTo = repaintFrom = canvas.createImage (clipBox.width, clipBox.height); g = repaintFrom.getGraphics(); g.setColor(canvas.getBackground()); g.fillRect(0, 0, clipBox.width, clipBox.height); g.translate(-clipBox.x, -clipBox.y); } if (font == null) { font = new Font ("Helvetica", Font.PLAIN, fontSize); g.setFont (font); FontMetrics fm = g.getFontMetrics(); textOffset.width = (fm.charWidth('X') + 1) / 2; textOffset.height = (fm.getAscent() + 1) / 2; } g.setFont (font); } /** * Indicates that the graphics data member <code>g</code> is no longer * in use. * * @param wasWrittenTo Input parameter, true if anything has been * written to the graphics context since it was opened. * False if we were "only looking". */ public void closeGraphics(boolean wasWrittenTo) { if (wasWrittenTo) { Image temp = repaintFrom; repaintFrom = drawTo; drawTo = temp; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -