Graphics draw Dot - Java 2D Graphics

Java examples for 2D Graphics:Dot

Description

Graphics draw Dot

Demo Code

/*******************************************************************************
 * Copyright (c) 2013 Jay Unruh, Stowers Institute for Medical Research.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 ******************************************************************************/
//package com.java2s;
import java.awt.Graphics;

public class Main {
    public static void drawDot(Graphics g, int x, int y) {
        g.drawLine(x - 1, y - 1, x, y - 1);
        g.drawLine(x - 1, y, x, y);/*  w  w w .ja  va 2 s .  co  m*/
    }

    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2,
            boolean thick) {
        if (thick) {
            drawThickLine(g, x1, y1, x2, y2);
        } else {
            g.drawLine(x1, y1, x2, y2);
        }
    }

    public static void drawThickLine(Graphics g, int x1, int y1, int x2,
            int y2) {
        float length = (float) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1)
                * (y2 - y1));
        float dx = (x2 - x1) / length;
        float dy = (y2 - y1) / length;
        float xpos = x1;
        float ypos = y1;
        for (int i = 0; i < (int) length; i++) {
            drawDot(g, (int) xpos, (int) ypos);
            xpos += dx;
            ypos += dy;
        }
        drawDot(g, x2, y2);
    }
}

Related Tutorials