/** Convenience doAdd() that takes an int x,y, adds and returns a dot model for it. */ public ManModel doAdd(int x, int y) { ManModel dotModel = new ManModel(); dotModel.setXY(x, y); // Debug.println("MAN_WIDTH is " + MAN_WIDTH + " and MAN_HEIGHT is " + MAN_HEIGHT); doAdd(dotModel); return dotModel; }
/** * Utility -- does a repaint rect just around one dot. Used by smart repaint when dragging a dot. */ private void repaintDot(ManModel dot) { repaint( dot.getX() - Constants.MAN_WIDTH / 2, dot.getY() - Constants.MAN_HEIGHT / 2, Constants.MAN_WIDTH + 1, Constants.MAN_HEIGHT + 1); }
/** Standard override -- draws all the dots. */ public void paintComponent(Graphics g) { // As a JPanel subclass we need call super.paintComponent() // so JPanel will draw the white background for us. super.paintComponent(g); Color origColor = g.getColor(); for (Line l : lines) { if (l.isPencil()) { g.setColor(Color.RED); } else { g.setColor(Color.BLACK); } g.drawLine((int) l.getX1(), (int) l.getY1(), (int) l.getX2(), (int) l.getY2()); } g.setColor(origColor); // Go through all the dots, drawing a circle for each for (ManModel dotModel : dots) { // g.drawImage(img, dotModel.getX() - MAN_WIDTH/2, dotModel.getY() - MAN_HEIGHT/2, MAN_WIDTH, // MAN_HEIGHT, null); g.drawImage( img, dotModel.getX() - Constants.MAN_WIDTH / 2, dotModel.getY() - Constants.MAN_HEIGHT / 2, null); } numberMen.setText("" + dots.size()); // Draw the "requested" clip rect in red // (this just shows off smart-repaint) if (redPaint) { Rectangle clip = g.getClipBounds(); if (clip != null) { g.setColor(Color.red); g.drawRect(clip.x, clip.y, clip.width - 1, clip.height - 1); g.setColor(origColor); } } g.setColor(Color.RED); for (Line l : circles) { g.drawLine((int) l.getX1(), (int) l.getY1(), (int) l.getX2(), (int) l.getY2()); } for (Arc l : arcs) { g.drawArc( (int) l.getX1(), (int) l.getY1(), (int) l.getWidth(), (int) l.getHeight(), (int) l.getStartAngle(), (int) l.getSweep()); // g.drawArc((int) l.getX1(), (int) l.getY1(), (int) l.getWidth()+1, (int) l.getHeight()+1, // l.getStartAngle()-1, l.getSweep()); } g.setColor(Color.BLACK); }
public void shuffle() { for (ManModel man : dots) { int randX = rgen.nextInt(Constants.MAN_WIDTH / 2, getWidth() - Constants.MAN_WIDTH / 2); int randY = rgen.nextInt(Constants.MAN_HEIGHT / 2, getHeight() - Constants.MAN_HEIGHT / 2); man.setXY(randX, randY); // Debug.println(randX + ", " + randY); // Debug.println("width" + getWidth() + ": height" + getHeight()); } repaint(); }
private ManModel findOverlap(double x1, double y1, double x2, double y2) { double manW = Constants.MAN_WIDTH / 2; double manH = Constants.MAN_HEIGHT / 2; for (ManModel dot : dots) { double leftX = dot.getX() - manW; double topY = dot.getY() - manH; double rightX = dot.getX() + manW; double botY = dot.getY() + manH; if (x1 < rightX && y1 < botY && x2 > leftX && y2 > topY) { return dot; } } return null; }
/** * Moves a dot from one place to another. Does the necessary repaint. This animation can repaint * two ways. Plain repaint: repaint the whole panel Smart repaint: repaint just the old+new bounds * of the dot */ public void doMove(ManModel dotModel, int dx, int dy) { if (!smartRepaint) { // Change the data model, then repaint the whole panel dotModel.moveBy(dx, dy); repaint(); } else { // Smart repaint: old + new // Repaint the "old" rectangle if (oldRepaint) { repaintDot(dotModel); } // Change the model dotModel.moveBy(dx, dy); // Repaint the "new" rectangle repaintDot(dotModel); } setDirty(true); }
/** Finds a dot in the data model that contains the given x,y or returns null. */ public ManModel findDot(int x, int y) { // Search through the dots in reverse order, so // hit topmost ones first. for (int i = dots.size() - 1; i >= 0; i--) { ManModel dotModel = dots.get(i); int centerX = dotModel.getX(); int centerY = dotModel.getY(); // figure x-squared + y-squared, see if it's // less than radius squared. // trick: don't need to take square root this way if (x > (centerX - Constants.MAN_WIDTH / 2) && x < (centerX + Constants.MAN_WIDTH / 2) && y > (centerY - Constants.MAN_HEIGHT / 2) && y < (centerY + Constants.MAN_HEIGHT / 2)) { return dotModel; } } return null; }