Skip to content

Commit

Permalink
Enhance WebcamPanel default painter - add camera name
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Dec 4, 2012
1 parent ee94acb commit 9aaf9d6
Showing 1 changed file with 22 additions and 7 deletions.
Expand Up @@ -52,6 +52,8 @@ public static interface Painter {
*/
public class DefaultPainter implements Painter {

private String name = null;

@Override
public void paintPanel(WebcamPanel owner, Graphics2D g2) {

Expand All @@ -76,26 +78,35 @@ public void paintPanel(WebcamPanel owner, Graphics2D g2) {
g2.fillRect(cx + 63, cy + 28, 7, 2);
g2.fillRect(cx + 63, cy + 31, 7, 2);

g2.setColor(Color.DARK_GRAY);
g2.setStroke(new BasicStroke(3));
g2.drawLine(0, 0, getWidth(), getHeight());
g2.drawLine(0, getHeight(), getWidth(), 0);

String str = starting ? "Initializing" : "No Image";
FontMetrics metrics = g2.getFontMetrics(getFont());
int w = metrics.stringWidth(str);
int h = metrics.getHeight();

g2.setColor(Color.WHITE);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g2.drawString(str, (getWidth() - w) / 2, cy - h / 2);
g2.drawString(str, (getWidth() - w) / 2, cy - h);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.DARK_GRAY);
g2.setStroke(new BasicStroke(3));
g2.drawLine(0, 0, getWidth(), getHeight());
g2.drawLine(0, getHeight(), getWidth(), 0);
if (name == null) {
name = webcam.getName();
}

str = name;
w = metrics.stringWidth(str);
h = metrics.getHeight();

g2.drawString(str, (getWidth() - w) / 2, cy - 2 * h);
}

@Override
public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
g2.drawImage(image, 0, 0, null);
}

}

/**
Expand Down Expand Up @@ -232,6 +243,10 @@ public WebcamPanel(Webcam webcam) {
*/
public WebcamPanel(Webcam webcam, boolean start) {

if (webcam == null) {
throw new IllegalArgumentException(String.format("Webcam argument in %s constructor cannot be null!", getClass().getSimpleName()));
}

this.webcam = webcam;
this.webcam.addWebcamListener(this);

Expand Down

0 comments on commit 9aaf9d6

Please sign in to comment.