-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
414 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Lab5</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.awt.*; | ||
import java.awt.geom.*; | ||
|
||
/** | ||
A car shape. | ||
*/ | ||
public class CarShape extends CompoundShape | ||
{ | ||
/** | ||
Constructs a car shape. | ||
@param x the left of the bounding rectangle | ||
@param y the top of the bounding rectangle | ||
@param width the width of the bounding rectangle | ||
*/ | ||
public CarShape(int x, int y, int width) | ||
{ | ||
Rectangle2D.Double body | ||
= new Rectangle2D.Double(x, y + width / 6, | ||
width - 1, width / 6); | ||
Ellipse2D.Double frontTire | ||
= new Ellipse2D.Double(x + width / 6, y + width / 3, | ||
width / 6, width / 6); | ||
Ellipse2D.Double rearTire | ||
= new Ellipse2D.Double(x + width * 2 / 3, | ||
y + width / 3, | ||
width / 6, width / 6); | ||
|
||
// The bottom of the front windshield | ||
Point2D.Double r1 | ||
= new Point2D.Double(x + width / 6, y + width / 6); | ||
// The front of the roof | ||
Point2D.Double r2 | ||
= new Point2D.Double(x + width / 3, y); | ||
// The rear of the roof | ||
Point2D.Double r3 | ||
= new Point2D.Double(x + width * 2 / 3, y); | ||
// The bottom of the rear windshield | ||
Point2D.Double r4 | ||
= new Point2D.Double(x + width * 5 / 6, y + width / 6); | ||
Line2D.Double frontWindshield | ||
= new Line2D.Double(r1, r2); | ||
Line2D.Double roofTop | ||
= new Line2D.Double(r2, r3); | ||
Line2D.Double rearWindshield | ||
= new Line2D.Double(r3, r4); | ||
|
||
add(body); | ||
add(frontTire); | ||
add(rearTire); | ||
add(frontWindshield); | ||
add(roofTop); | ||
add(rearWindshield); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.awt.*; | ||
import java.awt.geom.*; | ||
|
||
/** | ||
A scene shape that is composed of multiple geometric shapes. | ||
*/ | ||
public abstract class CompoundShape extends SelectableShape | ||
{ | ||
public CompoundShape() | ||
{ | ||
path = new GeneralPath(); | ||
} | ||
|
||
protected void add(Shape s) | ||
{ | ||
path.append(s, false); | ||
} | ||
|
||
public boolean contains(Point2D aPoint) | ||
{ | ||
return path.contains(aPoint); | ||
} | ||
|
||
public void translate(int dx, int dy) | ||
{ | ||
path.transform( | ||
AffineTransform.getTranslateInstance(dx, dy)); | ||
} | ||
|
||
public void draw(Graphics2D g2) | ||
{ | ||
g2.draw(path); | ||
} | ||
|
||
private GeneralPath path; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.awt.*; | ||
import java.awt.geom.*; | ||
|
||
/** | ||
A house shape. | ||
*/ | ||
public class HouseShape extends CompoundShape | ||
{ | ||
/** | ||
Constructs a house shape. | ||
@param x the left of the bounding rectangle | ||
@param y the top of the bounding rectangle | ||
@param width the width of the bounding rectangle | ||
*/ | ||
public HouseShape(int x, int y, int width) | ||
{ | ||
Rectangle2D.Double base | ||
= new Rectangle2D.Double(x, y + width, width, width); | ||
|
||
// The left bottom of the roof | ||
Point2D.Double r1 | ||
= new Point2D.Double(x, y + width); | ||
// The top of the roof | ||
Point2D.Double r2 | ||
= new Point2D.Double(x + width / 2, y); | ||
// The right bottom of the roof | ||
Point2D.Double r3 | ||
= new Point2D.Double(x + width, y + width); | ||
|
||
Line2D.Double roofLeft | ||
= new Line2D.Double(r1, r2); | ||
Line2D.Double roofRight | ||
= new Line2D.Double(r2, r3); | ||
|
||
add(base); | ||
add(roofLeft); | ||
add(roofRight); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.awt.geom.*; | ||
import javax.swing.*; | ||
import java.util.*; | ||
|
||
/** | ||
A component that shows a scene composed of shapes. | ||
*/ | ||
public class SceneComponent extends JComponent | ||
{ | ||
public SceneComponent() | ||
{ | ||
shapes = new ArrayList<SceneShape>(); | ||
|
||
addMouseListener(new | ||
MouseAdapter() | ||
{ | ||
public void mousePressed(MouseEvent event) | ||
{ | ||
mousePoint = event.getPoint(); | ||
for (SceneShape s: shapes) | ||
{ | ||
if (s.contains(mousePoint)) | ||
s.setSelected(!s.isSelected()); | ||
} | ||
repaint(); | ||
} | ||
}); | ||
|
||
addMouseMotionListener(new | ||
MouseMotionAdapter() | ||
{ | ||
public void mouseDragged(MouseEvent event) | ||
{ | ||
Point lastMousePoint = mousePoint; | ||
mousePoint = event.getPoint(); | ||
for (SceneShape s : shapes) | ||
{ | ||
if (s.isSelected()) | ||
{ | ||
double dx = mousePoint.getX() - lastMousePoint.getX(); | ||
double dy = mousePoint.getY() - lastMousePoint.getY(); | ||
s.translate((int) dx, (int) dy); | ||
} | ||
} | ||
repaint(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
Adds an shape to the scene. | ||
@param s the shape to add | ||
*/ | ||
public void add(SceneShape s) | ||
{ | ||
shapes.add(s); | ||
repaint(); | ||
} | ||
|
||
/** | ||
Removes all selected shapes from the scene. | ||
*/ | ||
public void removeSelected() | ||
{ | ||
for (int i = shapes.size() - 1; i >= 0; i--) | ||
{ | ||
SceneShape s = shapes.get(i); | ||
if (s.isSelected()) shapes.remove(i); | ||
} | ||
repaint(); | ||
} | ||
|
||
public void paintComponent(Graphics g) | ||
{ | ||
super.paintComponent(g); | ||
Graphics2D g2 = (Graphics2D) g; | ||
for (SceneShape s : shapes) | ||
{ | ||
s.draw(g2); | ||
if (s.isSelected()) | ||
s.drawSelection(g2); | ||
} | ||
} | ||
|
||
private ArrayList<SceneShape> shapes; | ||
private Point mousePoint; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import java.awt.*; | ||
import java.awt.geom.*; | ||
import java.awt.event.*; | ||
import javax.swing.*; | ||
|
||
/** | ||
A program that allows users to edit a scene composed | ||
of items. | ||
*/ | ||
public class SceneEditor | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
JFrame frame = new JFrame(); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
final SceneComponent scene = new SceneComponent(); | ||
|
||
JButton houseButton = new JButton("House"); | ||
houseButton.addActionListener(new | ||
ActionListener() | ||
{ | ||
public void actionPerformed(ActionEvent event) | ||
{ | ||
scene.add(new HouseShape(20, 20, 50)); | ||
} | ||
}); | ||
|
||
JButton carButton = new JButton("Car"); | ||
carButton.addActionListener(new | ||
ActionListener() | ||
{ | ||
public void actionPerformed(ActionEvent event) | ||
{ | ||
scene.add(new CarShape(20, 20, 50)); | ||
} | ||
}); | ||
|
||
JButton removeButton = new JButton("Remove"); | ||
removeButton.addActionListener(new | ||
ActionListener() | ||
{ | ||
public void actionPerformed(ActionEvent event) | ||
{ | ||
scene.removeSelected(); | ||
} | ||
}); | ||
|
||
JPanel buttons = new JPanel(); | ||
buttons.add(houseButton); | ||
buttons.add(carButton); | ||
buttons.add(removeButton); | ||
|
||
frame.add(scene, BorderLayout.CENTER); | ||
frame.add(buttons, BorderLayout.NORTH); | ||
|
||
frame.setSize(300, 300); | ||
frame.setVisible(true); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.awt.*; | ||
import java.awt.geom.*; | ||
|
||
/** | ||
A shape that is a part of a scene. | ||
*/ | ||
public interface SceneShape | ||
{ | ||
/** | ||
Draws this item. | ||
@param g2 the graphics context | ||
*/ | ||
void draw(Graphics2D g2); | ||
/** | ||
Draws the selection adornment of this item. | ||
@param g2 the graphics context | ||
*/ | ||
void drawSelection(Graphics2D g2); | ||
/** | ||
Sets the selection state of this item. | ||
@param b true if this item is selected | ||
*/ | ||
void setSelected(boolean b); | ||
/** | ||
Gets the selection state of this item. | ||
@return true if this item is selected | ||
*/ | ||
boolean isSelected(); | ||
/** | ||
Translates this item by a given amount. | ||
@param dx the amount to translate in x-direction | ||
@param dy the amount to translate in y-direction | ||
*/ | ||
void translate(int dx, int dy); | ||
/** | ||
Tests whether this item contains a given point. | ||
@param p a point | ||
@return true if this item contains p | ||
*/ | ||
boolean contains(Point2D p); | ||
} | ||
|
Oops, something went wrong.