📄 orbitingbodycanvas.java
字号:
/*
* OrbitingBodyCanvas.java
*
* Copyright (C) 2000 Jason M. Hanley
* Released under the GNU General Public License (GPL)
* See license.txt for additional information.
*
* Created on August 6, 2000, 8:59 PM
*/
package fate.ui;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import fate.world.*;
import fate.util.*;
/**
* Displays a collection of {@link OrbitingBody} objects.
*
* @author preylude@s3m.com
* @version 0.1.0
*/
public class OrbitingBodyCanvas extends JPanel {
int radiusInPixels;
/** Set of {@link OrbitingBody} objects */
Vector objects;
/** Creates new OrbitingObjectCanvas */
public OrbitingBodyCanvas( int radiusInPixels ) {
super();
this.radiusInPixels = radiusInPixels;
setMinimumSize( new Dimension( radiusInPixels * 2, radiusInPixels * 2 ) );
setPreferredSize( new Dimension( radiusInPixels * 2, radiusInPixels * 2 ) );
}
/** Set object data */
public void setObjects( Vector objects ) {
this.objects = objects;
}
/** Display the canvas */
public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background
if (objects == null) {
Debug.trace( "Error: OrbitingBodyCanvas.paintComponet(): objects are null" );
return;
}
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
// Set origin to the middle of the canvas
g2.translate( radiusInPixels, radiusInPixels );
// Cycle through objects
Iterator iterObject = objects.iterator();
OrbitingBody object;
while( iterObject.hasNext() ) {
object = (OrbitingBody) iterObject.next();
// Calculates the orbit size such that an object distance of 100
// will place the orbit half way between the center and outside border
int orbitSize = object.distance * radiusInPixels / 100;
//@ this will be changed when we use bitmaps for objects
int objectSize = radiusInPixels / 15;
// Draw orbit
g2.setColor( Color.gray );
g2.drawOval( 0 - orbitSize/2, 0 - orbitSize/2, orbitSize, orbitSize );
// Draw body
int screenX = object.x() * radiusInPixels / 200;
int screenY = object.y() * radiusInPixels / 200;
g2.setColor( new Color( 0, 0, 80, 96 ) );
g2.fillOval( screenX-objectSize/2, screenY-objectSize/2, objectSize, objectSize );
g2.setColor( Color.black );
g2.drawOval( screenX-objectSize/2, screenY-objectSize/2, objectSize, objectSize );
Font font = new Font( "SansSerif", Font.PLAIN, 10 );
g2.setFont( font );
FontMetrics metrics = g2.getFontMetrics();
int fontWidth = metrics.stringWidth( object.name );
g2.setColor( Color.black );
g2.drawString( object.name, screenX-fontWidth/2-1, screenY+objectSize+2 );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -