📄 namedgraphicalobject.java
字号:
package toocom.ocgl;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.util.*;
import java.lang.*;
/**
* This class represents the generic named and graphical object. Such objects have a Terms and
* a position. They can be in a selected state.
*
* @author Fr閐閞ic F黵st
*/
public abstract class NamedGraphicalObject extends NamedObject{
public int x;
public int y;
private boolean selected;
private boolean highlighted;
private boolean transparent;
public NamedGraphicalObject(Terms terms,int x, int y){
super(terms);
this.x = x;
this.y = y;
this.selected = false;
this.highlighted = false;
this.transparent = false;
}
public NamedGraphicalObject(Terms terms){
super(terms);
this.selected = false;
this.highlighted = false;
this.transparent = false;
this.setRandomLocation();
}
public void setSelected(boolean value){
this.selected = value;
}
public boolean isSelected(){
return this.selected;
}
public void setHighlighted(boolean value){
this.highlighted = value;
}
public void setTransparent(boolean value){
this.transparent = value;
}
public void setLocation(int x, int y){
this.x = x;
this.y = y;
}
public void setRandomLocation(){
this.setLocation((int) (Math.random() * CGConstants.X_MAX),(int) (Math.random() * CGConstants.Y_MAX));
}
public void translate(int dx, int dy){
this.x = this.x + dx;
this.y = this.y + dy;
}
/** Returns the center of the image */
public Point getCenter(){
return (new Point(this.x,this.y)); //Now X and Y are already the center of the box
}
/** Returns the bounds of the image according to the font size of the given Graphics. */
public abstract RectangularShape getBounds(Graphics g,Language l);
/** Returns true if the image of the object is embedded into the specified rectangle.*/
public boolean isIncludedInto(Graphics g, Language l, Rectangle r){
RectangularShape shape = this.getBounds(g,l);
return ((shape.getX() > r.x) && (shape.getY() > r.y) && (shape.getMaxX() < (r.x + r.width))
&& (shape.getMaxY() < (r.y + r.height)));
}
/** Paint the primitive with its label in a shape. The object color is used to fill the
* shape, except when the object is highlighted (then the highlighted color is used), and
* border color is used to draw the border of the shape, except when the object is
* selected (then the selected color is used). */
public void paintObject(Graphics g, Color objectColor, Color borderColor, Color selectedColor, Color highlightedColor, Color textColor,Language l){
Graphics2D g2d = (Graphics2D) g;
RectangularShape r = this.getBounds(g2d,l);
if(!this.transparent){
if(highlighted) g2d.setColor(highlightedColor);
else g2d.setColor(objectColor);
}
else g2d.setColor(g2d.getBackground());
g2d.fill(r);
if(this.selected) g2d.setColor(selectedColor);
else g2d.setColor(borderColor);
g2d.setStroke(new BasicStroke(CGConstants.GRAPHICAL_OBJECT_BORDER_WIDTH));
g2d.draw(r);
g2d.setStroke(new BasicStroke(1.0f));
g2d.setColor(textColor);
Point startWriting=GraphicsToolkit.getTextPosition(g,this.toString(l),this.getCenter());
g2d.drawString(this.toString(l),(int)startWriting.getX(),(int)startWriting.getY());
}
/** This method is the general method used to easily paint a graphical object. It must use the
* paintObject(Graphics g, Color objectColor, Color borderColor, Color selectedColor, Color highlightedColor, Color textColor,Language l)
* method. */
public abstract void paintObject(Graphics g,Language l);
/** Erase the primitive image by filling its bounding area with the specified color. */
public void erase(Graphics g, Color c, Language l){
RectangularShape shape = this.getBounds(g,l);
g.setColor(c);
((Graphics2D) g).fill(shape);
}
/** Returns true if the point (x,y) is in the primitive image according to the font size of
* the given Graphics. */
public boolean containsPoint(int x, int y, Graphics g,Language l){
return this.getBounds(g,l).contains(x,y);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -