Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hu.kazocsaba.imageviewer;
  
  import java.awt.Color;
  import java.awt.Point;
  import java.util.HashSet;
 import java.util.Set;

An overlay for marking pixels of the image. A circle will be drawn around the selected pixels; the size of the circle is set independently of the current zoom, but it may be enlarged in order to contain the pixel entirely.

The default size is 5 pixels and the default color is red.

This object stores the list of pixels; this list can be edited with the setPoint(java.awt.Point), setPoints(java.lang.Iterable), addPoint(java.awt.Point), and clear() functions. All of these mutator functions cause the overlay to be repainted.

Author(s):
Kazó Csaba
 
 public class PixelMarkerOverlay extends Overlay {
 
 	private Set<Pointpoints = new HashSet<Point>();
 	private Color color = .;
 	private int size = 5;

Sets the size of the circle. The actual width and height of the circle will be 2*newSize+1.

Parameters:
newSize the new size of the marker circles
Throws:
java.lang.IllegalArgumentException if newSize is negative
 
 	public void setSize(int newSize) {
 		if (newSize < 0) throw new IllegalArgumentException("Negative size");
 		 = newSize;
 		repaint();
 	}

Sets the color of the marker circles.

Parameters:
color the new color
Throws:
java.lang.NullPointerException if color is null
 
 	public void setColor(Color color) {
 		if (color == nullthrow new NullPointerException();
 		this. = color;
 		repaint();
 	}

Adds a point to the list of pixels marked by this overlay.

Parameters:
p a new point
Throws:
java.lang.NullPointerException if p is null
 
 	public void addPoint(Point p) {
 		if (p == nullthrow new NullPointerException();
 		.add(new Point(p));
 		repaint();
 	}

Sets the argument as the only point marked by this overlay.

Parameters:
p the point to mark; if null, then no points will be selected
 
 	public void setPoint(Point p) {
 		if (p!=null) {
 			.add(new Point(p));
 		}
 		repaint();
 	}

Sets the marked pixels.

Parameters:
points an iterable of all the pixels that should be selected
Throws:
java.lang.NullPointerException if points or any individual point is null
 
 	public void setPoints(Iterable<Pointpoints) {
 		if (points==nullthrow new NullPointerException();
 		this..clear();
 		for (Point p : points) {
 			this..add(new Point(p));
 		}
 		repaint();
 	}

Removes all pixels from the overlay, clearing the selection.
 
 	public void clear() {
 		if (!.isEmpty()) {
 			repaint();
 		}
 	}
 
 	public void paint(Graphics2D gBufferedImage imageAffineTransform transform) {
		Point2D p2d = new Point2D.Double();
		int currentSize = Math.max(, (int) Math.ceil(transform.getScaleX() / Math.sqrt(2)));
		for (Point p : ) {
			p2d.setLocation(p.x + .5, p.y + .5);
			transform.transform(p2dp2d);
			g.drawOval((intp2d.getX() - currentSize, (intp2d.getY() - currentSize, 2 * currentSize + 1, 2 * currentSize + 1);
		}
	}
New to GrepCode? Check out our FAQ X