⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawingpanel.java

📁 java算法大全
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;import java.applet.*;import java.io.*;import java.util.*;/** * <code>DrawingPanel</code> is the graphical panel attached to the * animation frame <code>AlgAnimFrame</code>. It contains the methods * to draw objects for the animation algorithm or instances of another * classes, which contain the drawing methods. * <p> * This class is <b>NOT TO BE MODIFIED</b>. * <p> * Any object class which implements the interface <code>DrawingObj</code> * can be added to this panel by calling the <code>addDrawingObj()</code> * method. Similarly, commentary box of type <code>ComBox</code> can be * added to the panel by using <code>addCom</code> method. Since the * commentary box (<code>ComBox</code>) is also implementing the interface * <code>DrawingObj</code>, it can also be added by using the * <code>addDrawingObj</code> method. However, <code>ComBox</code> added * to the drawing panel by using <code>addCom</code> will always appear on * top of any object added to the panel by using the <code>addDrawingObj</code> * method. * <p> * Note that last object added to the panel will be displayed on top. * * @see AlgAnimFrame#getDrawingPanel * @see DrawingObj */public class DrawingPanel extends Panel {    private int panel_height = 100;    private int panel_width = 100;    private int offset = 10;    private int pref_height = 200;    private int pref_width = 250;    private int delayDuration = 200;    private FontMetrics fm;    private Dimension offscreensize = null;    private Image offscreen = null;    private Graphics offGraphics = null;    private boolean noAnim = false;    private Font bigFont;    private Font smallFont, tinyFont, hugeFont, fixFont;    private boolean skip = false;    private Vector comment;    private Vector drawingObj;    /**     * Creates a panel with white background and initializes the fonts     * to be used during the animation.     * @see DrawingPanel#getBigFont     * @see DrawingPanel#getSmallFont     * @see DrawingPanel#getTinyFont     * @see DrawingPanel#getHugeFont     * @see DrawingPanel#getFixFont     */    public DrawingPanel() {        smallFont = new Font("Dialog", Font.PLAIN, 10);        fm = this.getFontMetrics(smallFont);	bigFont = new Font("Dialog", Font.PLAIN, 12);	hugeFont = new Font("Dialog", Font.PLAIN, 14);	fixFont = new Font("Courier", Font.PLAIN, 12);	tinyFont = new Font("Dialog", Font.PLAIN, 8);	setBackground(Color.white);	panel_height = size().height;	panel_width = size().width;	comment = new Vector();	drawingObj = new Vector();	init();    } // DrawingPanel()     /**     * Initialize the commentary boxes and drawing objects,      * removing all of them from the drawing panel.      */    public void init() {	comment = new Vector();	drawingObj = new Vector();    }        /**     * Causing a short delay of 1/3 of the normal delay duration.     * This method is normally used during debugging to speed up     * the achievement of certain system state.     */    public void shortDelay() {	try {	    Thread.sleep(delayDuration/3);	} catch (InterruptedException e) {}    }    /**     * Invoke a delay for a certain duration specified by      * <code>setDelay(int)</code>.     * @see DrawingPanel#setDelay     */    public void delay() {	try {	  if (!skip)	    Thread.sleep(delayDuration);	  else	    Thread.sleep(delayDuration/100);	} catch (InterruptedException e) {}    }    private String toString(int i ) {        return new String(""+ (char)('A' + i));    }    /**     * Set the delay duration between animation update. This method     * is normally called from the event handler of the simulation     * delay menu bar.     * @param delay Delay duration in milliseconds.     */    public void setDelay(int delay) {	this.delayDuration = delay;    }    /**     * Calls repaint() followed by delay(). Since these two methods     * are being called very frequently in order, the redraw() method     * is constructed to save typing.     */    public void redraw() {	repaint();	delay();    }        /**     * This method is invoked when the <code>repaint()</code> method is called.     * The <code>update</code> method is override here to eliminate flashing.     * @param g Graphical context     */    public void update(Graphics g) {        Dimension d = size();        if (d.width < 1 || d.height < 1)            return;        if ((offscreen == null) || (d.width != offscreensize.width) ||                (d.height != offscreensize.height)) {            offscreen = createImage(d.width, d.height);            offscreensize = d;            offGraphics = offscreen.getGraphics();        }        offGraphics.setColor(getBackground());        offGraphics.fillRect(0, 0, d.width, d.height);        offGraphics.setFont(smallFont);        fm = offGraphics.getFontMetrics();        paint(offGraphics);        g.drawImage(offscreen, 0, 0, null);    }    /**     * Method to draw objects on the drawing panel.     */    public void paint(Graphics g) {	panel_height = size().height;	panel_width = size().width;    	for (int i = 0; i < drawingObj.size(); i++) {	    DrawingObj dr = (DrawingObj)drawingObj.elementAt(i);	    if (dr != null)		dr.draw(g);	    else		drawingObj.removeElementAt(i);	}	for (int i = 0; i < comment.size(); i++) {	    ComBox com = (ComBox)comment.elementAt(i);	    if (com != null)		com.draw(g);	}        g.setColor( Color.black );        g.drawRect( 1, 1, panel_width-2, panel_height-2 );    } // paint()    /**     * Returns the initial preferred size of the drawing panel.     * This method is called by the layout manager during the creation     * of the corresponding drawing frame.     * @return The dimension of the drawing panel.     */    public Dimension getPreferredSize() {        return new Dimension( pref_width, pref_height );    } // getPreferredSize()    /**     * Returns the font objects initialized during the class initialization,      * which     * can be readily used during the animation. Initialization fonts     * on the fly during the animation will slow down the screen update     * significantly (especially on SGI Irix platform). Therefore,     * this fonts are only initialized during the creation of the class     * and used by any other classes that require them to reduce     * font initialization overhead.     * @return Size 12 PLAIN Dialog font.     */    public Font getBigFont() {	return bigFont;    }    /**     * Return font objects initialized during the class initialization, which     * can be readily used during the animation.     * @return Size 10 PLAIN Dialog font.     * @see DrawingPanel#getBigFont     */    public Font getSmallFont() {	return smallFont;    }    /**     * Return font objects initialized during the class initialization, which     * can be readily used during the animation.     * @return Size 8 PLAIN Dialog font.     * @see DrawingPanel#getBigFont     */    public Font getTinyFont() {	return tinyFont;    }    /**     * Return font objects initialized during the class initialization, which     * can be readily used during the animation.     * @return Size 14 PLAIN Dialog font.     * @see DrawingPanel#getBigFont     */    public Font getHugeFont() {	return hugeFont;    }    /**     * Return font objects initialized during the class initialization, which     * can be readily used during the animation.     * @return Size 12 PLAIN Courier font.     * @see DrawingPanel#getBigFont     */    public Font getFixFont() {	return fixFont;    }    /**     * Get the height of the drawing panel.     * @return Height of the drawing panel set during the initialization of the     * class or after each invoke of the <code>repaint()</code> method.     */    public int getPanelHeight() {	return panel_height;    }    /**     * Get the width of the drawing panel.     * @return Width of the drawing panel set during the initialization of the     * class or after each invoke of the <code>repaint()</code> method.     */    public int getPanelWidth() {	return panel_width;    }    /**     * Get the number of pixel towards the edge of the panel which are not

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -