Commit 7c10e239 authored by Daniel Vogel's avatar Daniel Vogel

add graphics demos

parent 4a52f529
/*
* CS 349 Java Code Examples
*
* ClosestPointDemo Uses two methods to compute distance from
mouse to closest point on line. Double click
to generate new line.
*
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.vecmath.*;
import java.lang.Math.*;
import java.util.Random;
// create the window and run the demo
public class ClosestPointDemo extends JPanel
implements MouseInputListener {
Point P0 = new Point();
Point P1 = null;
Point M = new Point(); // mouse point
public static void main(String[] args) {
// create the window
ClosestPointDemo canvas = new ClosestPointDemo();
JFrame f = new JFrame("ClosestPointDemo"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400); // window size
f.setContentPane(canvas); // add canvas to jframe
f.setVisible(true); // show the window
}
ClosestPointDemo() {
setBackground(Color.WHITE);
// add listeners
addMouseListener(this);
addMouseMotionListener(this);
}
// custom graphics drawing
public void paintComponent(Graphics g) {
super.paintComponent(g); // JPanel paint
Graphics2D g2 = (Graphics2D)g;
if (P1 == null) {
P1 = new Point();
randomLine();
}
// antiliasing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(2));
// draw line
g2.setColor(Color.BLACK);
g2.drawLine(P0.x, P0.y, P1.x, P1.y);
// draw mouse point
g2.setColor(Color.BLACK);
int s = 7;
g2.drawOval(M.x - s, M.y - s, 2 * s, 2 * s );
// get closest point using the vector projection method
Point2d M2 = new Point2d(M.x, M.y);
Point2d I1 = closestPoint(M2,
new Point2d(P0.x, P0.y),
new Point2d(P1.x, P1.y));
// draw closest point
g2.setColor(Color.RED);
g2.drawOval((int)I1.x - s, (int)I1.y - s, 2 * s, 2 * s );
// use that to get distance
double d1 = M2.distance(I1);
// get distance using Java2D method
double d2 = Line2D.ptSegDist(P0.x, P0.y, P1.x, P1.y, M.x, M.y);
g2.setColor(Color.BLACK);
g2.drawString(String.format("%.1f %.1f", d1, d2), M.x + 10,M.y);
}
// find closest point using projection method
static Point2d closestPoint(Point2d M, Point2d P0, Point2d P1) {
Vector2d v = new Vector2d();
v.sub(P1,P0); // v = P2 - P1
// early out if line is less than 1 pixel long
if (v.lengthSquared() < 0.5)
return P0;
Vector2d u = new Vector2d();
u.sub(M,P0); // u = M - P1
// scalar of vector projection ...
double s = u.dot(v) / v.dot(v);
// find point for constrained line segment
if (s < 0)
return P0;
else if (s > 1)
return P1;
else {
Point2d I = P0;
Vector2d w = new Vector2d();
w.scale(s, v); // w = s * v
I.add(w); // I = P1 + w
return I;
}
}
// random numbers
static Random rand = new Random();
int random(int min, int max) {
return rand.nextInt(max - min + 1) + min;
}
void randomLine()
{
// create random line
int m = 50; // margin
P0.x = m;
P0.y = random(m, getHeight() - m);
P1.x = getWidth() - m;
P1.y = random(m, getHeight() - m);
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
M.x = arg0.getX();
M.y = arg0.getY();
repaint();
}
@Override
public void mouseClicked(MouseEvent arg0) {
// generate new line on double click
if (arg0.getClickCount() == 2)
randomLine();
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class PolygonHittestDemo extends JPanel{
Point M = new Point(); // mouse point
Polygon poly = new Polygon();
public static void main(String args[]){
JFrame window = new JFrame("PolygonHittestDemo");
window.setSize(300, 300);
window.setContentPane(new PolygonHittestDemo());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
public PolygonHittestDemo(){
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
poly.addPoint(e.getX(), e.getY());
repaint();
}
});
this.addMouseMotionListener(new MouseAdapter(){
public void mouseMoved(MouseEvent e){
M.x = e.getX();
M.y = e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (poly.contains(M.x, M.y))
g2.setColor(Color.BLUE);
else
g2.setColor(Color.RED);
g2.fillPolygon(poly);
g2.setColor(Color.BLACK);
g.drawPolyline(poly.xpoints, poly.ypoints, poly.npoints);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleDraw {
public static void main(String[] args) {
JFrame f = new JFrame("SimpleDraw");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setContentPane(new Canvas());
f.setVisible(true);
}
}
// JComponent is a base class for custom components
class Canvas extends JComponent {
Point M = new Point(); // mouse point
Point C = new Point(150, 150); // click point
Canvas(){
super();
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
C.x = e.getX();
C.y = e.getY();
repaint();
}
});
this.addMouseMotionListener(new MouseAdapter(){
public void mouseMoved(MouseEvent e){
M.x = e.getX();
M.y = e.getY();
repaint();
}
});
}
// custom graphics drawing
public void paintComponent(Graphics g) {
super.paintComponent(g);
// cast to get 2D drawing methods
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
g2.drawLine(C.x, C.y, M.x, M.y);
g2.fillOval(M.x - 5, M.y - 5, 10, 10);
g2.setColor(Color.RED);
g2.fillOval(C.x - 5, C.y - 5, 10, 10);
g2.drawString(String.format("%d,%d", C.x, C.y), C.x + 10, C.y + 10);
g2.setColor(Color.BLACK);
g2.drawString(String.format("%d,%d", M.x, M.y), M.x + 10, M.y + 10);
}
}
# super simple makefile
# call it using 'make NAME=name_of_code_file_without_extension'
# (assumes a .java extension)
NAME = SimpleDraw
# you may need to pass OS=win to run on windows
OS =
# HACK: vecmath is included regardless if needed
all:
@echo "Compiling..."
javac -cp vecmath.jar $(NAME).java
run: all
# windows needs a semicolon
ifeq ($(OS),win)
@echo "Running on windows ..."
java -cp "vecmath.jar;." $(NAME)
# everyone else likes a colon
else
@echo "Running ..."
java -cp "vecmath.jar:." $(NAME)
endif
clean:
rm -rf *.class
## Java graphics demos
> NOTE: These demos require vecmath.jar to be compiled with demo. See the included Makefile.
* `SimpleDraw.java` Responds to mouse events and draws primitives in 2D canvas.
* `ClosestPointDemo.java` Computes distance from mouse to closest point on line using algorithm discussed in class. Also uses built-in method to accomplish same thing.
* `PolygonHittestDemo.java` Uses built-in method to hit-test closed polygon.
<!-- TransConcatDemo Demo of different concatenation orders of matrix transforms using
MyShape class. Click the window to change the order.
RotateLine Use transformations to rotate line
RotateTriangle1 Animates a triangle rotating around a circle using transformations. -->
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment