这是一个图形编辑器中的两段程序:
1.
import java.awt.* ;public abstract class Element
{
// This implementation assumes that all elements have four
// control points. Certain operations must be overridden for
// elements with fewer, such as lines. static final Color highlightColor = Color.red ;
Rectangle bbox ;
int drag ;
public Element(Point p, Point q)
{
resize(p, q) ;
} public void move(int x, int y)
{
bbox.x += x ;
bbox.y += y ;
} public void resize(Point p, Point q)
{
bbox = new Rectangle(Math.min(p.x,q.x), Math.min(p.y,q.y),
Math.abs(p.x-q.x), Math.abs(p.y-q.y)) ;
} public void moveControl(int x, int y)
{
switch (drag) {
case 0:
bbox.x += x ;
bbox.y += y ;
bbox.width -= x ;
bbox.height -= y ;
break ;
case 1:
bbox.y += y ;
bbox.width += x ;
bbox.height -= y ;
break ;
case 2:
bbox.width += x ;
bbox.height += y ;
break ;
case 3:
bbox.x += x ;
bbox.width -= x ;
bbox.height += y ;
break ;
}
if (bbox.width < 0) {
bbox.x += bbox.width ;
bbox.width = -bbox.width ;
drag += drag%2 == 0 ? 1 : -1 ;
}
if (bbox.height < 0) {
bbox.y += bbox.height ;
bbox.height = -bbox.height ;
drag = 3-drag ;
}
} public boolean findControl( Point p )
{
drag = -1 ;
if (nearEnough(p, new Point(bbox.x, bbox.y))) {
drag = 0 ;
} else if (nearEnough(p, new Point(bbox.x+bbox.width, bbox.y))) {
drag = 1 ;
} else if (nearEnough(p, new Point(bbox.x+bbox.width, bbox.y+bbox.height))) {
drag = 2 ;
} else if (nearEnough(p, new Point(bbox.x, bbox.y+bbox.height))) {
drag = 3 ;
}
return drag != -1 ;
} public void highlight(Graphics g)
{
drawHighlight(g, bbox.x, bbox.y, highlightColor) ;
drawHighlight(g, bbox.x+bbox.width, bbox.y, highlightColor) ;
drawHighlight(g, bbox.x, bbox.y+bbox.height, highlightColor) ;
drawHighlight(g, bbox.x+bbox.width, bbox.y+bbox.height, highlightColor) ;
} Rectangle bounds()
{
return bbox ;
} public boolean contains( Point p )
{
return bounds().inside(p.x, p.y) ;
} void drawHighlight(Graphics g, int x, int y, Color c)
{
Color oldColor = g.getColor() ;
g.setColor(c) ;
g.drawRect(x-1, y-1, 3, 3) ;
g.setColor(oldColor) ;
} abstract void draw(Graphics g) ;
boolean nearEnough( Point p0, Point p1 )
{
return Math.abs(p0.x - p1.x) <= 3 && Math.abs(p0.y - p1.y) <= 3 ;
}
}
2.
import java.awt.* ;
import java.util.* ;public class SelectionTool extends Tool
{
final static int Locating = 0 ;
final static int Moving = 1 ;
final static int Resizing = 2 ;
final static int Error = 3 ;
int state ; Point lastPoint ; Vector selected = new Vector(16) ;
Element resizing ;
SelectionTool(Diagram d) {
super(d) ;
state = Locating ;
} void draw( Graphics g )
{
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element e = (Element) enum.nextElement() ;
e.highlight(g) ;
}
} void delete()
{
switch (state) {
case Locating:
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element e = (Element) enum.nextElement() ;
// The following line is suggested by the sequence diagram.
// However, use of "removeElement" in an iteration corrupts
// the vector "selected", so it's replaced by a call to
// removeAllElements() after the iteration is complete.
// unselect(e) ;
diagram.remove(e) ;
}
selected.removeAllElements() ; // see comment above
break ;
case Moving:
break ;
case Resizing:
break ;
case Error:
break ;
}
}
void move( Point p )
{
current = p ;
switch (state) {
case Locating:
break ;
case Moving:
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element e = (Element) enum.nextElement() ;
e.move(current.x-lastPoint.x, current.y-lastPoint.y) ;
}
break ;
case Resizing:
resizing.moveControl(current.x-lastPoint.x, current.y-lastPoint.y) ;
break ;
case Error:
break ;
}
lastPoint = current ;
} void press()
{
switch (state) {
case Locating:
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element el = (Element) enum.nextElement() ;
if (el.findControl(current)) {
resizing = el ;
break ;
}
}
if (resizing != null) {
state = Resizing ;
} else {
Element el = diagram.find(current) ;
if (el != null) {
select(el) ;
state = Moving ;
} else {
state = Error ;
}
}
case Moving:
break ;
case Resizing:
break ;
case Error:
break ;
}
} void select(Element e)
{
if (!selected.contains(e)) {
selected.addElement(e) ;
}
} void unselect(Element e)
{
selected.removeElement(e) ;
} void release()
{
switch (state) {
case Locating:
break ;
case Moving:
state = Locating ;
break ;
case Resizing:
resizing = null ;
state = Locating ;
break ;
case Error:
state = Locating ;
break ;
}
}
}
1.
import java.awt.* ;public abstract class Element
{
// This implementation assumes that all elements have four
// control points. Certain operations must be overridden for
// elements with fewer, such as lines. static final Color highlightColor = Color.red ;
Rectangle bbox ;
int drag ;
public Element(Point p, Point q)
{
resize(p, q) ;
} public void move(int x, int y)
{
bbox.x += x ;
bbox.y += y ;
} public void resize(Point p, Point q)
{
bbox = new Rectangle(Math.min(p.x,q.x), Math.min(p.y,q.y),
Math.abs(p.x-q.x), Math.abs(p.y-q.y)) ;
} public void moveControl(int x, int y)
{
switch (drag) {
case 0:
bbox.x += x ;
bbox.y += y ;
bbox.width -= x ;
bbox.height -= y ;
break ;
case 1:
bbox.y += y ;
bbox.width += x ;
bbox.height -= y ;
break ;
case 2:
bbox.width += x ;
bbox.height += y ;
break ;
case 3:
bbox.x += x ;
bbox.width -= x ;
bbox.height += y ;
break ;
}
if (bbox.width < 0) {
bbox.x += bbox.width ;
bbox.width = -bbox.width ;
drag += drag%2 == 0 ? 1 : -1 ;
}
if (bbox.height < 0) {
bbox.y += bbox.height ;
bbox.height = -bbox.height ;
drag = 3-drag ;
}
} public boolean findControl( Point p )
{
drag = -1 ;
if (nearEnough(p, new Point(bbox.x, bbox.y))) {
drag = 0 ;
} else if (nearEnough(p, new Point(bbox.x+bbox.width, bbox.y))) {
drag = 1 ;
} else if (nearEnough(p, new Point(bbox.x+bbox.width, bbox.y+bbox.height))) {
drag = 2 ;
} else if (nearEnough(p, new Point(bbox.x, bbox.y+bbox.height))) {
drag = 3 ;
}
return drag != -1 ;
} public void highlight(Graphics g)
{
drawHighlight(g, bbox.x, bbox.y, highlightColor) ;
drawHighlight(g, bbox.x+bbox.width, bbox.y, highlightColor) ;
drawHighlight(g, bbox.x, bbox.y+bbox.height, highlightColor) ;
drawHighlight(g, bbox.x+bbox.width, bbox.y+bbox.height, highlightColor) ;
} Rectangle bounds()
{
return bbox ;
} public boolean contains( Point p )
{
return bounds().inside(p.x, p.y) ;
} void drawHighlight(Graphics g, int x, int y, Color c)
{
Color oldColor = g.getColor() ;
g.setColor(c) ;
g.drawRect(x-1, y-1, 3, 3) ;
g.setColor(oldColor) ;
} abstract void draw(Graphics g) ;
boolean nearEnough( Point p0, Point p1 )
{
return Math.abs(p0.x - p1.x) <= 3 && Math.abs(p0.y - p1.y) <= 3 ;
}
}
2.
import java.awt.* ;
import java.util.* ;public class SelectionTool extends Tool
{
final static int Locating = 0 ;
final static int Moving = 1 ;
final static int Resizing = 2 ;
final static int Error = 3 ;
int state ; Point lastPoint ; Vector selected = new Vector(16) ;
Element resizing ;
SelectionTool(Diagram d) {
super(d) ;
state = Locating ;
} void draw( Graphics g )
{
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element e = (Element) enum.nextElement() ;
e.highlight(g) ;
}
} void delete()
{
switch (state) {
case Locating:
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element e = (Element) enum.nextElement() ;
// The following line is suggested by the sequence diagram.
// However, use of "removeElement" in an iteration corrupts
// the vector "selected", so it's replaced by a call to
// removeAllElements() after the iteration is complete.
// unselect(e) ;
diagram.remove(e) ;
}
selected.removeAllElements() ; // see comment above
break ;
case Moving:
break ;
case Resizing:
break ;
case Error:
break ;
}
}
void move( Point p )
{
current = p ;
switch (state) {
case Locating:
break ;
case Moving:
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element e = (Element) enum.nextElement() ;
e.move(current.x-lastPoint.x, current.y-lastPoint.y) ;
}
break ;
case Resizing:
resizing.moveControl(current.x-lastPoint.x, current.y-lastPoint.y) ;
break ;
case Error:
break ;
}
lastPoint = current ;
} void press()
{
switch (state) {
case Locating:
Enumeration enum = selected.elements() ;
while (enum.hasMoreElements()) {
Element el = (Element) enum.nextElement() ;
if (el.findControl(current)) {
resizing = el ;
break ;
}
}
if (resizing != null) {
state = Resizing ;
} else {
Element el = diagram.find(current) ;
if (el != null) {
select(el) ;
state = Moving ;
} else {
state = Error ;
}
}
case Moving:
break ;
case Resizing:
break ;
case Error:
break ;
}
} void select(Element e)
{
if (!selected.contains(e)) {
selected.addElement(e) ;
}
} void unselect(Element e)
{
selected.removeElement(e) ;
} void release()
{
switch (state) {
case Locating:
break ;
case Moving:
state = Locating ;
break ;
case Resizing:
resizing = null ;
state = Locating ;
break ;
case Error:
state = Locating ;
break ;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货