📄 graphicsdirector.java
字号:
}
};
paintThread.start();
}
else {
try{
if(!isLocked)
lockPaint.wait(400);
}catch( Exception e ) {}
lockPaint.notify();
}
}
*/
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To avoid flickering.
*/
public void update( Graphics g ) {
paint( g );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To paint this JPanel.
*
* @param gc graphics object.
*/
public void paint(Graphics gc) {
if(gc==null || screen==null || getHeight()<=0 || getWidth()<=0) return;
// double-buffer init
if (backBufferImage == null || getWidth() != backBufferImage.getWidth(this) || getHeight() != backBufferImage.getHeight(this))
backBufferImage = (Image) new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics backBufferGraphics = backBufferImage.getGraphics();
if(!display) {
backBufferGraphics.setColor( Color.white );
backBufferGraphics.fillRect( 0, 0, getWidth(), getHeight() );
gc.drawImage(backBufferImage, 0, 0, this);
return;
}
Graphics2D gc2D = (Graphics2D) backBufferGraphics;
// Anti-aliasing init
RenderingHints savedRenderHints = gc2D.getRenderingHints(); // save
RenderingHints antiARenderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
antiARenderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
boolean previousHadAntiA = false;
final Rectangle r_screen = new Rectangle( screen );
backBufferGraphics.setColor( Color.white );
backBufferGraphics.fillRect( 0, 0, getWidth(), getHeight() );
synchronized( drawables ) {
drawables.resetIterator();
while( drawables.hasNext() ) {
Drawable d = drawables.next();
// Set Anti-aliasing or not ?
if( d.wantAntialiasing() && !previousHadAntiA ) {
previousHadAntiA = true;
gc2D.setRenderingHints( antiARenderHints );
}
else if( !d.wantAntialiasing() && previousHadAntiA ) {
previousHadAntiA = false;
gc2D.setRenderingHints( savedRenderHints );
}
d.paint( gc2D, r_screen );
}
}
// Rendering Hints restore...
gc2D.setRenderingHints( savedRenderHints );
// double-buffer print
gc.drawImage(backBufferImage, 0, 0, this);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To add a drawable.
*
* @param dr drawable to add.
*/
public void addDrawable( Drawable dr ) {
if(dr==null) return;
// We initialize the drawable.
dr.init(imageLib);
// We insert it in our list (list ordered by priority)
synchronized( drawables ) {
drawables.resetIterator();
while( drawables.hasNext() ) {
Drawable current = drawables.next();
if( current.getPriority() > dr.getPriority() ) {
drawables.insert( dr );
return;
}
}
drawables.add( dr );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To remove a drawable.
*
* @param dr drawable to remove.
*/
public void removeDrawable( Drawable dr ) {
if(dr==null) return;
synchronized( drawables ) {
drawables.resetIterator();
while( drawables.hasNext() )
if( drawables.next() == dr ) {
drawables.remove();
return;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To remove all the drawables.
*/
public void removeAllDrawables() {
synchronized( drawables ) {
drawables.clear();
refDrawable = null;
display = false;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** The tick method updates our screen position, drawables and repaint the whole thing.
* Never call repaint on the graphics director, call tick() !
*/
public void tick() {
if(screen==null)
return;
// 1 - We update our screen dimension.
synchronized( drawables ) {
screen.width = getWidth();
screen.height = getHeight();
}
// 2 - We update our WindowPolicy
if( getWidth()>0 && getHeight()>0 )
windowPolicy.tick();
// 3 - We tick all our sprites
synchronized( drawables ) {
drawables.resetIterator();
while( drawables.hasNext() )
if( !drawables.next().tick() )
drawables.remove();
}
// 4 - We repaint all our prites
repaint();
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To set a new drawable reference.
*
* @param newRefDrawable new drawable reference
*/
public void setRefDrawable( Drawable newRefDrawable ) {
refDrawable = newRefDrawable;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get the current drawable reference.
*
* @return drawable reference
*/
public Drawable getRefDrawable() {
return refDrawable;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get the screen rectangle. Any changes to the returned rectangle are
* affected to the original.
*/
public Rectangle getScreenRectangle() {
return screen;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get the background dimension.
*/
public Dimension getBackgroundDimension() {
return background;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Given a point we search for the first drawable that implements the DrawableOwner
* interface AND contains the point. We then return the owner of the drawable.
*
* The search in the Drawable list is performed backward : we inspect top sprites
* ( high priority ) first.
*
* @param x x cordinate
* @param y y cordinate
* @return the owner of the targeted drawable, null if none or not found.
*/
public Object findOwner( int x, int y ) {
synchronized( drawables ) {
drawables.resetIteratorToEnd();
while( drawables.hasPrev() ) {
Drawable d = drawables.prev();
if( d instanceof DrawableOwner )
if ( d.contains( x+screen.x, y+screen.y )
&& ((DrawableOwner)d).getOwner()!=null )
return ( (DrawableOwner)d ).getOwner();
}
}
return null;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Given a point we search for the first drawable that implements the DrawableOwner
* interface AND contains the point. We then return the owner of the drawable.
*
* @param p Point
* @return the owner of the targeted drawable, null if none or not found.
*/
public Object findOwner( Point p ) {
return findOwner( p.x, p.y );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get the ImageLibrary associated to this GraphicsDirector.
* @return our ImageLibrary
*/
public ImageLibrary getImageLibrary() {
return imageLib;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -