I'm making Braid. I have a class Wall that prevents that an object goes into a wall.
Everything with Wall is working. But now I'm trying to make the same with the ceilings.
My ceiling class extends Wall. I've simply made a constructor like this:
public Ceiling(boolean deadly, int[] xPositions, int[] yPositions)
{
super(deadly,
Direction.Down, //Enum: the direction you have to leave the wall (Left, Right)
//Here of course down
xPositions, yPositions);
}
Now I have in my level-class an ArrayList of all the Walls and an ArrayList of all the Ceilings.
I have to add the Walls on this way:
walls.add(new Wall(false, Direction.Right, ..., ...));
And the Ceilings on this way:
ceilings.add(new Ceiling(false, ..., ...));
The ... replaces the coordinates.
If I check if there is an object in a Ceiling: there has nothing happened: the object goes through the ceiling. And if I use this way to add a Ceiling, it works:
ceilings.add(new Wall(false, Direction.Down, ..., ...));
I hope I've explained well.
Does somebody know what the problem is??
Thanks
Edit:
This is my collition code:
public boolean intersects(Rectangle r)
{
if (!bounds.intersects(r))
{
return false;
}
for (int i = 1; i < yPositions.length; i++) {
Line l = new Line(xPositions[i - 1], yPositions[i - 1], xPositions[i], yPositions[i]);
if (r.intersectsLine(l)) {
return true;
}
}
return false;
}
My Code
'doodelijk' means deadly
Wall:
package levels;
import domein.Direction;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class Wall
{
public boolean doodelijk;
public int[] yPositions;
public int[] xPositions;
private Rectangle bounds;
public Direction directionToLeave;
public Wall(boolean doodelijk, Direction directionToLeave, int[] yPositions, int[] xPositions)
{
this.doodelijk = doodelijk;
this.yPositions = yPositions;
this.xPositions = xPositions;
this.directionToLeave = directionToLeave;
createRectangle();
}
public boolean intersects(Rectangle r)
{
if (!bounds.intersects(r))
{
return false;
}
for (int i = 1; i < yPositions.length; i++) {
Line l = new Line(xPositions[i - 1], yPositions[i - 1], xPositions[i], yPositions[i]);
if (r.intersectsLine(l)) {
return true;
}
}
return false;
}
private void createRectangle()
{
int x = Integer.MAX_VALUE;
int y = Integer.MAX_VALUE;
int x1 = 0;
int y1 = 0;
for (int i = 0; i < xPositions.length; i++)
{
int tx = xPositions[i];
int ty = yPositions[i];
if (x > tx)
{
x = tx;
}
if (y > ty)
{
y = ty;
}
if (x1 < tx)
{
x1 = tx;
}
if (y1 < ty)
{
y1 = ty;
}
}
bounds = new Rectangle(x, y, x1 - x + 1, y1 - y +1);
System.out.println("Rect: " + bounds);
}
class Line extends Line2D
{
Point2D p1;
Point2D p2;
public Line()
{
p1 = new Point();
p2 = new Point();
}
public Line(Point2D p1, Point2D p2)
{
this.p1 = p1;
this.p2 = p2;
}
public Line(double X1, double Y1, double X2, double Y2)
{
this();
setLine(X1, Y1, X2, Y2);
}
#Override
public double getX1()
{
return p1.getX();
}
#Override
public double getY1()
{
return p1.getY();
}
#Override
public Point2D getP1()
{
return p1;
}
#Override
public double getX2()
{
return p2.getX();
}
#Override
public double getY2()
{
return p2.getY();
}
#Override
public Point2D getP2()
{
return p2;
}
#Override
public void setLine(double X1, double Y1, double X2, double Y2)
{
p1.setLocation(X1, Y1);
p2.setLocation(X2, Y2);
}
public Rectangle2D getBounds2D()
{
return new Rectangle((int) getX1(), (int) getY1(), (int) (getX2() - getX1()), (int) (getX2() - getY1()));
}
}
public void setXpositions(int ... xPos)
{
this.xPositions = xPos;
}
public void setYpositions(int ... yPos)
{
this.yPositions = yPos;
}
public void setPositions(int[] yPos, int[] xPos)
{
setXpositions(xPos);
setYpositions(yPos);
}
}
Ceiling:
package levels;
import domein.Direction;
public class Ceiling extends Wall
{
public Ceiling(boolean doodelijk, int[] xPositions, int[] yPositions)
{
super(doodelijk, Direction.Down, yPositions, xPositions);
}
}
Are you sure about your settings of the xposition and yposition arguments? That is, is the ceiling really where you think it is? Is it the same in your two variants of the constructor?
I guess the problem is in the code that checks for the "collision". I cant see any problem in the one you've given.
I don't see any obvious problems with your classes. There might be a bug somewhere else and you would have to post the complete classes for us to identify it.
Related
So i code consist of 3 parts 2 classes and test.
This is code for test
#Test
public void testRectangle1() {
Point center = new Point(20, 30);
Rectangle rect = new Rectangle(center, 20, 20);
assertAll(
() -> assertEquals(10, rect.getTopLeft().getX()),
() -> assertEquals(20, rect.getTopLeft().getY()),
() -> assertEquals(30, rect.getBottomRight().getX()),
() -> assertEquals(40, rect.getBottomRight().getY()),
() -> assertEquals(20, rect.getWidth()),
() -> assertEquals(20, rect.getHeight())
);
}
Class Point works fine, and i am ading it just for clarity
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void moveTo(int newX, int newY) {
x = newX;
y = newY;
}
public void moveRel(int dx, int dy) {
x += dx;
y += dy;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
This is class for Rectangle itself, and it includes both constructor and aditional methods.
public class Rectangle {
public int width = 0;
public int height = 0;
public Point center;
public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
width=x;
height=y;
}
public Point getTopLeft() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(- width / 2, height / 2);
return point;
}
public Point getBottomRight() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(width / 2, - height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Main problem is that this code return only zeroes in tests, i guess problem is in rectangle class in constructor, or aditionalmethods.
Your constructor for Rectangle is always setting width and height to 0. I think the constructor should look something like...
public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
this.width=width;
this.height=height;
this.center=center;
}
I am wring the bouncing ball program in java. And I Now have one bouncing ball, I would like to have at least five bouncing balls. I have tried a few ways to do it, however, I only end up with one ball or error.
Do you have any suggestions on how to proceed? This in the piece of code used for the one ball, is it possible to rewrite this piece of code to get multiple balls in a neat way?
import javafx.scene.shape.Rectangle;
public class World {
private final double width, height;
private Ball[] balls;
private final Rectangle pad;
public World(double width, double height) {
this.width = width;
this.height = height;
balls = new Ball[1];
balls[0] = new Ball(10, 10);
balls[0].setVelocity(75.0, 100.0);
pad = new Rectangle(width / 2, 0.9 * height,
width / 8, height / 32);
}
public void move(long elapsedTimeNs) {
balls[0].move(elapsedTimeNs);
constrainBall(balls[0]);
checkForCollisionWithPad(balls[0]);
}
public Ball[] getBalls() {
return (Ball[]) balls.clone();
}
public Rectangle getPad() {
return pad;
}
public void setPadX(double x) {
if (x > width) {
x = width;
}
if (x < 0) {
x = 0;
}
pad.setX(x);
}
private void constrainBall(Ball ball) {
double x = ball.getX(), y = ball.getY();
double dx = ball.getDx(), dy = ball.getDy();
double radius = ball.getRadius();
if (x < radius) {
dx = Math.abs(dx);
} else if (x > width - radius) {
dx = -Math.abs(dx);
}
if (y < radius) {
dy = Math.abs(dy);
} else if (y > height - radius) {
dy = -Math.abs(dy);
}
ball.setVelocity(dx, dy);
}
private void checkForCollisionWithPad(Ball ball) {
if (ball.intersectsArea(
pad.getX(), pad.getY(), pad.getWidth(), pad.getHeight())) {
double dx = ball.getDx();
// set dy negative, i.e. moving "up"
double newDy = -Math.abs(ball.getDy());
ball.setVelocity(dx, newDy);
}
}
}
Main
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Bounce extends Application {
private World world;
private Canvas canvas;
private AnimationTimer timer;
protected class BounceTimer extends AnimationTimer {
private long previousNs = 0;
#Override
public void handle(long nowNs) {
if (previousNs == 0) {
previousNs = nowNs;
}
world.move(nowNs - previousNs);
previousNs = nowNs;
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITESMOKE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
Rectangle pad = world.getPad();
gc.setFill(Color.BLACK);
double x = pad.getX(), y = pad.getY(),
w = pad.getWidth(), h = pad.getHeight();
gc.fillRoundRect(x, y, w, h, h, h);
for (Ball b : world.getBalls()) {
b.paint(gc);
}
}
}
#Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 300, 300, Color.WHITESMOKE);
canvas = new Canvas(scene.getWidth(), scene.getHeight());
root.getChildren().add(canvas);
stage.setTitle("Bounce");
stage.setScene(scene);
stage.setResizable(false);
stage.sizeToScene();
stage.show();
world = new World(canvas.getWidth(), canvas.getHeight());
timer = new BounceTimer();
timer.start();
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED,
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
world.setPadX(me.getX());
}
});
}
public static void main(String[] args) {
launch(args);
}
private void showAlert(String message) {
alert.setHeaderText("");
alert.setTitle("Alert!");
alert.setContentText(message);
alert.show();
}
private final Alert alert = new Alert(Alert.AlertType.INFORMATION);
}
Ball
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Ball {
public static final double BILLION = 1_000_000_000.0;
private double x, y; // position of the balls center
private double dx, dy; // velocity measured in pixels/second
private double radius;
private Color color;
public Ball(double x0, double y0) {
x = x0;
y = y0;
radius = 10;
color = Color.MAGENTA;
}
public Ball(double x0, double y0, double rad, Color col) {
x = x0;
y = y0;
radius = rad;
color = col;
}
Ball(int i, int i0, Color BLUEVIOLET) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void setColor(Color col) { // setColor
color = col; }
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double newX) {
x = newX;
}
public void setY(double newY) {
y = newY;
}
public double getRadius() {
return radius;
}
public double getDx() {
return dx;
}
public double getDy() {
return dy;
}
public void setVelocity(double newDx, double newDy) {
dx = newDx;
dy = newDy;
}
public void moveTo(double newX, double newY) {
x = newX;
y = newY;
}
public void move(long elapsedTimeNs) {
x += dx * elapsedTimeNs / BILLION;
y += dy * elapsedTimeNs / BILLION;
}
public void paint(GraphicsContext gc) {
gc.setFill(color);
// arguments to fillOval: see the javadoc for GraphicsContext
gc.fillOval(x - radius, y - radius, radius * 2, radius * 2);
}
public boolean intersectsArea(
double rectX, double rectY,
double rectWidth, double rectHeight) {
double closestX = clamp(x, rectX, rectX + rectWidth);
double closestY = clamp(y, rectY, rectY + rectHeight);
double distanceX = x - closestX;
double distanceY = y - closestY;
return (distanceX * distanceX) + (distanceY * distanceY)
< (radius * radius);
}
private double clamp(double value, double lower, double upper) {
if (value < lower) {
return lower;
}
if (value > upper) {
return upper;
}
return value;
}
}
As Stormblessed said, you are only targeting one ball in your move method.
You should do:
public void move(Ball ball, long elapsedTimeNs) {
ball.move(elapsedTimeNs);
constrainBall(ball);
checkForCollisionWithPad(ball);
}
Edit: Since you want the handler method to accept only the elapsedTimeNs argument, do:
public void move(long elapsedTimeNs) {
for (Ball ball : balls) {
ball.move(elapsedTimeNs);
constrainBall(ball);
checkForCollisionWithPad(ball);
}
}
Edit 2: You should probably have a method that creates a new ball, for convenience:
public Ball newBall(double x, double y, double velocity1, double velocity2) {
Ball tmp = new Ball(x, y);
tmp.setVelocity(velocity1, velocity2);
balls.add(tmp);
return tmp;
}
Edit 3: The reason it throws an error is that you designated balls to have only one index position by using balls = new Ball[1]. You should use an ArrayList (java.util.ArrayList) instead, like so:
import java.util.ArrayList;
ArrayList<Ball> balls = new ArrayList<>;
You should now use balls.add and balls.get instead of = and []. References have been updated accordingly.
I'm looking for a java code to create a oriented minimum bounding box with points, which have a lat/lon value. I've already created a minimum bounding box, like this:
public Mbb boundingBox() {
Point ll, ur;
Mbb bBox;
int id =1;
ll = new Point(id, Double.MAX_VALUE, Double.MAX_VALUE);
ur= new Point(id,-1*Double.MAX_VALUE, -1*Double.MAX_VALUE);
if (this.pg.size() <=1)
return null;
double minLat = Double.MAX_VALUE;
double minLong = Double.MAX_VALUE;
double maxLat = Double.MAX_VALUE*-1;
double maxLong = Double.MAX_VALUE*-1;
for (Point testPoint: this.pg) {
double lat = testPoint.getLat();
double lon = testPoint.getLon();
if(minLat>lat)
minLat=lat;
if(minLong>lon)
minLong=lon;
if(maxLat<lat)
maxLat=lat;
if(maxLong<lon)
maxLong=lon;
}
ll.setLat(minLat);
ll.setLon(minLong);
ur.setLat(maxLat);
ur.setLon(maxLong);
bBox= new Mbb(id, ll, ur);
return bBox;
}
But this is not an oriented one. Has anybody an idea how to orient my bounding box?
The computation if the minimum oriented bounding box is not so trivial. But one approach is described in an answer to https://gis.stackexchange.com/q/22895 . The answer links to a Java implementation, but this is obviously part of a larger framework. However, I implemented the approach here as an example:
The computation of the convex hull is made with a code snippet taken from convex-hull at Google code - it's not the nicest implementation, but was the first one that I found, and does the job.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MinOrientedBoundingBoxTest
{
public static void main(String[] args) throws IOException
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new MinOrientedBoundingBoxTestPanel());
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class MinOrientedBoundingBoxTestPanel extends JPanel
implements MouseListener, MouseMotionListener
{
private final List<Point2D> points;
private Point2D draggedPoint = null;
MinOrientedBoundingBoxTestPanel()
{
points = new ArrayList<Point2D>();
Random r = new Random(0);
for (int i=0; i<8; i++)
{
double x = 200 + r.nextDouble() * 200;
double y = 200 + r.nextDouble() * 200;
points.add(new Point2D.Double(x,y));
}
addMouseListener(this);
addMouseMotionListener(this);
}
#Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
drawPoints(g, points);
boolean showConvexHull = false;
showConvexHull = true;
if (showConvexHull)
{
List<Point2D> convexHullPoints =
MinOrientedBoundingBoxComputer.computeConvexHullPoints(
points);
Path2D convexHullPath =
MinOrientedBoundingBoxComputer.createPath(
convexHullPoints);
g.setColor(Color.GRAY);
g.draw(convexHullPath);
}
List<Point2D> minObbCorners =
MinOrientedBoundingBoxComputer.computeCorners(points);
Path2D p = MinOrientedBoundingBoxComputer.createPath(minObbCorners);
g.setColor(Color.BLUE);
g.draw(p);
}
static void drawPoints(Graphics2D g, List<Point2D> points)
{
double r = 3;
for (Point2D point : points)
{
double x = point.getX();
double y = point.getY();
g.fill(new Ellipse2D.Double(
x-r, y-r, r+r, r+r));
}
}
#Override
public void mouseDragged(MouseEvent e)
{
if (draggedPoint != null)
{
draggedPoint.setLocation(e.getPoint());
repaint();
}
}
#Override
public void mouseMoved(MouseEvent e)
{
}
#Override
public void mouseClicked(MouseEvent e)
{
}
#Override
public void mousePressed(MouseEvent e)
{
draggedPoint = null;
double thresholdSquared = 10*10;
double minDs = Double.MAX_VALUE;
for (Point2D point : points)
{
double ds = point.distanceSq(e.getPoint());
if (ds < thresholdSquared && ds < minDs)
{
minDs = ds;
draggedPoint = point;
}
}
}
#Override
public void mouseReleased(MouseEvent e)
{
draggedPoint = null;
}
#Override
public void mouseEntered(MouseEvent e)
{
}
#Override
public void mouseExited(MouseEvent e)
{
}
}
class MinOrientedBoundingBoxComputer
{
static List<Point2D> computeCorners(List<Point2D> points)
{
List<Point2D> convexHullPoints =
computeConvexHullPoints(points);
int alignmentPointIndex =
computeAlignmentPointIndex(convexHullPoints);
Rectangle2D r = computeAlignedBounds(
convexHullPoints, alignmentPointIndex);
List<Point2D> alignedCorners = new ArrayList<Point2D>();
alignedCorners.add(new Point2D.Double(r.getMinX(), r.getMinY()));
alignedCorners.add(new Point2D.Double(r.getMaxX(), r.getMinY()));
alignedCorners.add(new Point2D.Double(r.getMaxX(), r.getMaxY()));
alignedCorners.add(new Point2D.Double(r.getMinX(), r.getMaxY()));
Point2D center = convexHullPoints.get(alignmentPointIndex);
double angleRad = computeEdgeAngleRad(
convexHullPoints, alignmentPointIndex);
AffineTransform at = new AffineTransform();
at.concatenate(
AffineTransform.getTranslateInstance(
center.getX(), center.getY()));
at.concatenate(
AffineTransform.getRotateInstance(angleRad));
List<Point2D> corners = transform(alignedCorners, at);
return corners;
}
private static int computeAlignmentPointIndex(
List<Point2D> points)
{
double minArea = Double.MAX_VALUE;
int minAreaIndex = -1;
for (int i=0; i<points.size(); i++)
{
Rectangle2D r = computeAlignedBounds(points, i);
double area = r.getWidth() * r.getHeight();
if (area < minArea)
{
minArea = area;
minAreaIndex = i;
}
}
return minAreaIndex;
}
private static double computeEdgeAngleRad(
List<Point2D> points, int index)
{
int i0 = index;
int i1 = (i0+1)%points.size();
Point2D p0 = points.get(i0);
Point2D p1 = points.get(i1);
double dx = p1.getX() - p0.getX();
double dy = p1.getY() - p0.getY();
double angleRad = Math.atan2(dy, dx);
return angleRad;
}
private static Rectangle2D computeAlignedBounds(
List<Point2D> points, int index)
{
Point2D p0 = points.get(index);
double angleRad = computeEdgeAngleRad(points, index);
AffineTransform at = createTransform(-angleRad, p0);
List<Point2D> transformedPoints = transform(points, at);
Rectangle2D bounds = computeBounds(transformedPoints);
return bounds;
}
private static AffineTransform createTransform(
double angleRad, Point2D center)
{
AffineTransform at = new AffineTransform();
at.concatenate(
AffineTransform.getRotateInstance(angleRad));
at.concatenate(
AffineTransform.getTranslateInstance(
-center.getX(), -center.getY()));
return at;
}
private static List<Point2D> transform(
List<Point2D> points, AffineTransform at)
{
List<Point2D> result = new ArrayList<Point2D>();
for (Point2D p : points)
{
Point2D tp = at.transform(p, null);
result.add(tp);
}
return result;
}
private static Rectangle2D computeBounds(
List<Point2D> points)
{
double minX = Double.MAX_VALUE;
double minY = Double.MAX_VALUE;
double maxX = -Double.MAX_VALUE;
double maxY = -Double.MAX_VALUE;
for (Point2D p : points)
{
double x = p.getX();
double y = p.getY();
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
}
static Path2D createPath(List<Point2D> points)
{
Path2D path = new Path2D.Double();
for (int i=0; i<points.size(); i++)
{
Point2D p = points.get(i);
double x = p.getX();
double y = p.getY();
if (i == 0)
{
path.moveTo(x, y);
}
else
{
path.lineTo(x, y);
}
}
path.closePath();
return path;
}
static List<Point2D> computeConvexHullPoints(List<Point2D> points)
{
// NOTE: Converting from Point2D to Point here
// because the FastConvexHull class expects
// the points with integer coordinates.
// This should be generalized to Point2D!
ArrayList<Point> ps = new ArrayList<Point>();
for (Point2D p : points)
{
ps.add(new Point((int)p.getX(), (int)p.getY()));
}
List<Point> convexHull = FastConvexHull.execute(ps);
List<Point2D> result = new ArrayList<Point2D>();
for (Point p : convexHull)
{
double x = p.getX();
double y = p.getY();
result.add(new Point2D.Double(x,y));
}
return result;
}
}
// From https://code.google.com/p/convex-hull/source/browse/
// Convex+Hull/src/algorithms/FastConvexHull.java?r=4
// Under GPL2 license
// (Not a "nice" implementation, but the first one that
// I found with a websearch. Maybe, when I'm bored, I'll
// replace it with another one...)
class FastConvexHull
{
public static ArrayList<Point> execute(ArrayList<Point> points)
{
ArrayList<Point> xSorted = (ArrayList<Point>) points.clone();
Collections.sort(xSorted, new XCompare());
int n = xSorted.size();
Point[] lUpper = new Point[n];
lUpper[0] = xSorted.get(0);
lUpper[1] = xSorted.get(1);
int lUpperSize = 2;
for (int i = 2; i < n; i++)
{
lUpper[lUpperSize] = xSorted.get(i);
lUpperSize++;
while (lUpperSize > 2 &&
!rightTurn(lUpper[lUpperSize - 3], lUpper[lUpperSize - 2],
lUpper[lUpperSize - 1]))
{
// Remove the middle point of the three last
lUpper[lUpperSize - 2] = lUpper[lUpperSize - 1];
lUpperSize--;
}
}
Point[] lLower = new Point[n];
lLower[0] = xSorted.get(n - 1);
lLower[1] = xSorted.get(n - 2);
int lLowerSize = 2;
for (int i = n - 3; i >= 0; i--)
{
lLower[lLowerSize] = xSorted.get(i);
lLowerSize++;
while (lLowerSize > 2 &&
!rightTurn(lLower[lLowerSize - 3], lLower[lLowerSize - 2],
lLower[lLowerSize - 1]))
{
// Remove the middle point of the three last
lLower[lLowerSize - 2] = lLower[lLowerSize - 1];
lLowerSize--;
}
}
ArrayList<Point> result = new ArrayList<Point>();
for (int i = 0; i < lUpperSize; i++)
{
result.add(lUpper[i]);
}
for (int i = 1; i < lLowerSize - 1; i++)
{
result.add(lLower[i]);
}
return result;
}
private static boolean rightTurn(Point a, Point b, Point c)
{
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) > 0;
}
private static class XCompare implements Comparator<Point>
{
#Override
public int compare(Point o1, Point o2)
{
return (new Integer(o1.x)).compareTo(new Integer(o2.x));
}
}
}
I am trying to create a rectangle in Java but only with awt package classes.
I can only click two points and the program must calculate the width and height and draw a rectangle between those exact two points.
The following doesn't work for me:
package ie.iact.shapes;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
public class Rect extends Shapes {``
private Point secondPoint;
public Rect(Point f, Point s) {
setFirstPoint(f);
setSecondPoint(s);
}
#Override
public void draw(Graphics g) {
int x1 = firstPoint.x;
int y1 = firstPoint.y;
int x2 = secondPoint.x;
int y2 = secondPoint.y;
int a;
int b;
if (x1 < x2) {
a = x1;
} else {
a = x2;
}
if (y1 < y2) {
b = y1;
} else {
b = y2;
}
int width = secondPoint.x - a;
int hight = secondPoint.y - b;
g.drawRect(getFirstPoint().x, getFirstPoint().y, secondPoint.x, secondPoint.y);
}
public Point getSecondPoint() {
return secondPoint;
}
public void setSecondPoint(Point secondPoint) {
this.secondPoint = secondPoint;
}
}
Rectangle class can already handle all of your calculations:
Rectangle rect= new Rectangle(point1);
rect.add(point2);
g.fillRect(rect.x, rect.y, rect.width, rect.height);
Alternatively you can use setFrameFromDiagonal:
Rectangle rect= new Rectangle();
rect.setFrameFromDiagonal(point1, point2);
g.fillRect(rect.x, rect.y, rect.width, rect.height);
So I have an assignment for University. The concept was that we were to complete some class hierarchy stuff. Basically it was stuff to allow us to draw different shapes.
I can successfully draw each shape; where I need it to be and how big I need it to be, like required... the part I'm having trouble on is this compound hierarchy.
Basically we're supposed to have a new class called Compound.java and extending from that we are supposed to have three other classes, House, tree, and earth; each of which are supposed to take the shape objects we created (Rectangle, Square, Line, Oval and Circle) and draw the required pictures as denoted by the class name.
Where I am having the problem is in the house class; for example: I can get it to draw one rectangle but when I try to get it to draw the second rectangle after, it basically forgets about the first and only draws the second!
We haven't had any practice with the Graphics stuff so I don't know any methods or anything that I can call to draw then continue in the House constructor.
I understand why it overwrites the first rectangle, when the House constructor is called, it runs through all the stuff in the constructor, then goes back up to the Compound.java and draws it using the draw(Graphics g) method....
But I don't know how to fix it! Any help would be appreciated... it's due tomorrow.
Here's all the code:
Shape.java:
import java.awt.*;
public abstract class Shape {
int initX, initY;
Color fillColour;
public Shape() {
initX = 0;
initY = 0;
}
public Shape(int x, int y) {
initX = x;
initY = y;
}
public void setInitX (int x) {
initX = x;
}
public void setInitY (int y) {
initY = y;
}
public abstract void draw(Graphics g);
public abstract double Area();
public abstract double Perimeter();
public void Move(int deltaX, int deltaY){
//future work
}
}
ClosedShape.java :
import java.awt.Graphics;
public abstract class ClosedShape extends Shape {
boolean polygon;
int numPoints;
int[] xVertices;
int[] yVertices;
int x,y,width, height;
public ClosedShape(boolean isPolygon, int numPoints) {
super(0,0);
this.polygon = isPolygon;
this.numPoints = numPoints;
}
public ClosedShape(boolean isPolygon, int numPoints, int[] x, int[] y) {
super(x[0],y[0]);
this.polygon = isPolygon;
if (isPolygon) {
this.numPoints = numPoints;
xVertices = new int[numPoints]; // error check? if x.length == numPoints
for (int i = 0; i < x.length; i++) { // make copy of array: why?
xVertices[i] = x[i];
}
yVertices = new int[numPoints]; // error check? if y.length == numPoints
for (int i = 0; i < y.length; i++) { // make copy of array
yVertices[i] = y[i];
}
}
else { // its an oval - define bounding box
this.numPoints = 4;
this.x = x[0];
this.y = y[0];
width = x[1];
height = y[1];
}
}
public void setXYCoords(int[] x, int[] y){
this.xVertices = x;
this.yVertices = y;
}
// Gives access to the width attribute
public void setWidth(int width){
this.width = width;
}
// Gives access to the height attribute
public void setHeight(int height) {
this.height = height;
}
public void draw(Graphics g) {
if (polygon) {
g.drawPolygon(xVertices, yVertices, numPoints);
}
else {
g.drawOval(x, y, width, height);
}
}
public abstract double Area();
public abstract double Perimeter();
}
Rectangle.java :
public class Rectangle extends ClosedShape
{
public Rectangle(int x, int y, int width, int height)
{
super(true, 4);
setWidth(width);
setHeight(height);
int [] arrayX = new int[4];
arrayX[0] = x;
arrayX[1] = (x+width);
arrayX[2] = (x+width);
arrayX[3] = x;
int [] arrayY = new int[4];
arrayY[0] = y;
arrayY[1] = y;
arrayY[2] = y+height;
arrayY[3] = y+height;
setXYCoords(arrayX, arrayY);
}
public double Area()
{
return 0;
}
public double Perimeter()
{
return 0;
}
}
Compound.java :
import java.awt.*;
import java.awt.Graphics;
public class Compound
{
boolean polygon;
int[] xVertices;
int[] yVertices;
int initX, initY;
Color fillColour;
public void setXYCoords(int[] x, int[] y)
{
this.xVertices = x;
this.yVertices = y;
}
public void draw(Graphics g)
{
if (polygon) {
g.drawPolygon(xVertices, yVertices, 4);
}
else {
g.drawOval(1, 1, 1, 1);
}
}
}
House.java :
import java.awt.*;
import java.awt.Graphics;
public class House extends Compound
{
public House(int x, int y, int width, int height)
{
int [] arrayX = new int[4];
arrayX[0] = x;
arrayX[1] = (x+width);
arrayX[2] = (x+width);
arrayX[3] = x;
int [] arrayY = new int[4];
arrayY[0] = y;
arrayY[1] = y;
arrayY[2] = y+height;
arrayY[3] = y+height;
setXYCoords(arrayX, arrayY);
this.polygon = true;
Rectangle house = new Rectangle(x, y, width, height);
int [] arrayXTwo = new int[4];
arrayXTwo[0] = x+(width/4);
arrayXTwo[1] = x+(2*(width/4));
arrayXTwo[2] = x+(2*(width/4));
arrayXTwo[3] = x+(width/4);
int [] arrayYTwo = new int[4];
arrayYTwo[0] = y+(height/4);
arrayYTwo[1] = y+(height/4);
arrayYTwo[2] = y+height;
arrayYTwo[3] = y+height;
setXYCoords(arrayXTwo, arrayYTwo);
this.polygon = true;
Rectangle door = new Rectangle(x, y, width, height);
}
}
From your sample code, there is no way that to "add" any shapes to the Compound class. From your description, Compound should be a "container" of shapes. Something more along the lines of...
public class Compound
{
private List<Shape> shapes;
public Compound() {
shapes = new ArrayList<Shape>(25);
}
public void addShape(Shape shape) {
shapes.add(shape);
}
public Iterable<Shape> getShapes() {
return shape;
}
public void draw(Graphics g) {
for (Shape shape : shapes) {
shape.draw(g);
}
}
}
Now you need to decide, where is it best to associate the Color with the Shape should it be defined for the shape itself? That means you can't reuse the shape. Or with the Compound shape?