Skip to content

Commit

Permalink
added rubber banding! Someone write us the fix so the final circle
Browse files Browse the repository at this point in the history
looks like the rubber banded one
  • Loading branch information
hydrodog committed Nov 6, 2015
1 parent 1bbf46c commit 5cba626
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions Drawing2/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/DrawArea$1.class
/DrawArea$2.class
25 changes: 20 additions & 5 deletions Drawing2/src/DrawArea.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Currency;

public class DrawArea extends JPanel {
private ArrayList<Shape> drawList; // starts as null
private int startX, startY;
private int startX, startY, endX, endY;
public DrawArea() {
drawList = new ArrayList<Shape>(10000);
drawList.add(new Circle(100,200,50));
Expand All @@ -18,17 +20,14 @@ public DrawArea() {
addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
int endX = e.getX(), endY = e.getY();
Graphics g = getGraphics();
drawList.add(new Circle(Math.min(startX,endX), Math.min(startY,endY),
Math.min((int)Math.abs(endX - startX), (int)Math.abs(endY - startY))/2
));
repaint();
// g.drawOval(Math.min(startX,endX), Math.min(startY,endY),
// (int)Math.abs(endX - startX), (int)Math.abs(endY - startY));
// System.out.println(e.getX() + "," + e.getY());
}
public void mousePressed(MouseEvent e) {
startX = e.getX(); startY = e.getY();
endX = startX = e.getX(); endY = startY = e.getY();
// System.out.println(e.getX() + "," + e.getY());
}

Expand All @@ -40,7 +39,23 @@ public void mouseEntered(MouseEvent e) {
public void mouseClicked(MouseEvent e) {
}
});
addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setXORMode(Color.WHITE);
g.drawOval(Math.min(startX,endX), Math.min(startY,endY),
(int)Math.abs(endX - startX), (int)Math.abs(endY - startY));

endX = e.getX(); endY = e.getY();
g.drawOval(Math.min(startX,endX), Math.min(startY,endY),
(int)Math.abs(endX - startX), (int)Math.abs(endY - startY));

}
});
}

public void paint(Graphics g) {
for (int i = 0; i < drawList.size(); i++) {
Shape s = drawList.get(i);
Expand Down

0 comments on commit 5cba626

Please sign in to comment.