📄 mycanvas.java
字号:
/*
* MyCanvas.java
*
* Created on June 2, 2007, 10:20 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package termproject_dijkstra;
/**
*
* @author Peto
*/
import java.awt.*;
import java.awt.event.*;
public class MyCanvas extends Canvas{
int[] m_iFrameSize = {0,0};
public Dijkstra m_graph;
public boolean m_bDrawOnlyPaths; // Drawing only paths to the start node
/***********************************************************
/ Source code PARTIALLY from : http://www.codeproject.com/java/javadoublebuffer.asp
***********************************************************/
// class variables
private int bufferWidth;
private int bufferHeight;
private Image bufferImage;
private Graphics bufferGraphics;
public void paint(Graphics g)
{
resetBuffer();
if(bufferGraphics!=null){
//this clears the offscreen image, not the onscreen one
bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);
//calls the paintbuffer method with
//the offscreen graphics as a param
paintBuffer(bufferGraphics);
//we finaly paint the offscreen image onto the onscreen image
g.drawImage(bufferImage,0,0,this);
}
}
MyCanvas()
{
bufferWidth = 0;
bufferHeight = 0;
m_graph = new Dijkstra();
}
private void resetBuffer()
{
// always keep track of the image size
int newBufferWidth = getSize().width;
int newBufferHeight = getSize().height;
// clean up the previous image
if(newBufferWidth != bufferWidth || newBufferHeight != bufferHeight)
{
if (bufferGraphics!=null)
bufferGraphics.dispose();
if (bufferImage!=null)
bufferImage.flush();
bufferWidth = newBufferWidth;
bufferHeight = newBufferHeight;
bufferImage=createImage(bufferWidth,bufferHeight);
bufferGraphics=bufferImage.getGraphics();
}
}
/***********************************************************
/ END Source code from : http://www.codeproject.com/java/javadoublebuffer.asp
***********************************************************/
public void paintBuffer(Graphics g)
{
float fxInc = (float)getWidth()/(float)m_iFrameSize[0];
float fyInc = (float)getHeight()/(float)m_iFrameSize[1];
g.setColor(new Color(255,255,255));
g.fillRect(0,0,getWidth(),getHeight());
if (GlobalVars.bConstCanvasSize)
m_graph.Draw(g,1.0f,1.0f);
else
m_graph.Draw(g,fxInc,fyInc);
}
public void update()
{
paint(getGraphics());
}
/** Creates a new instance of MyCanvas */
public MyCanvas(int[] iFrameSize)
{
m_iFrameSize[0] = iFrameSize[0];
m_iFrameSize[1] = iFrameSize[1];
m_graph = new Dijkstra();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -