All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.invation.code.toval.graphic.util.GraphicUtils Maven / Gradle / Ivy

package de.invation.code.toval.graphic.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;


public class GraphicUtils {
	
	//--Drawing circles
	
	/**
	 * Draws a circle with the specified diameter using the given point as center.
	 * @param g Graphics context
	 * @param center Circle center
	 * @param diameter Circle diameter
	 */
	public static void drawCircle(Graphics g, Point center, int diameter){
		drawCircle(g, (int) center.getX(), (int) center.getY(), diameter);
	}
	
	/**
	 * Draws a circle with the specified diameter using the given point coordinates as center.
	 * @param g Graphics context
	 * @param centerX X coordinate of circle center
	 * @param centerY Y coordinate of circle center
	 * @param diameter Circle diameter
	 */
	public static void drawCircle(Graphics g, int centerX, int centerY, int diameter){
		g.drawOval((int) (centerX-diameter/2), (int) (centerY-diameter/2), diameter, diameter);
	}
	
	/**
	 * Draws a circle with the specified diameter using the given point as center
	 * and fills it with the current color of the graphics context.
	 * @param g Graphics context
	 * @param center Circle center
	 * @param diameter Circle diameter
	 */
	public static void fillCircle(Graphics g, Point center, int diameter){
		fillCircle(g, (int) center.getX(), (int) center.getY(), diameter);
	}
	
	/**
	 * Draws a circle with the specified diameter using the given point coordinates as center
	 * and fills it with the current color of the graphics context.
	 * @param g Graphics context
	 * @param centerX X coordinate of circle center
	 * @param centerY Y coordinate of circle center
	 * @param diam Circle diameter
	 */
	public static void fillCircle(Graphics g, int centerX, int centerY, int diam){
		g.fillOval((int) (centerX-diam/2), (int) (centerY-diam/2), diam, diam);
	}
	
	/**
	 * Draws a circle with the specified diameter using the given point as center
	 * and fills it with the given color.
	 * @param g Graphics context
	 * @param center Circle center
	 * @param diameter Circle diameter
	 */
	public static void fillCircle(Graphics g, Point center, int diameter, Color color){
		fillCircle(g, (int) center.x, (int) center.y, diameter, color);
	}
	
	/**
	 * Draws a circle with the specified diameter using the given point coordinates as center
	 * and fills it with the given color.
	 * @param g Graphics context
	 * @param centerX X coordinate of circle center
	 * @param centerY Y coordinate of circle center
	 * @param diam Circle diameter
	 */
	public static void fillCircle(Graphics g, int centerX, int centerY, int diam, Color color){
		Color c = g.getColor();
		g.setColor(color);
		fillCircle(g, centerX, centerY, diam);
		g.setColor(c);
	}
	
	
	
	
	
	//--Drawing arrows
	
	/**
	 * Draws an arrow between two given points using the specified weight.
* The arrow is leading from point from to point to. * @param g Graphics context * @param from Arrow starting point * @param to Arrow ending point * @param weight Arrow weight */ public static void drawArrow(Graphics g, Point from, Point to, double weight){ drawArrow(g, from.getX(), from.getY(), to.getX(), to.getY(), weight); } /** * Draws an arrow between two points (specified by their coordinates) using the specified weight.
* The arrow is leading from point from to point to. * @param g Graphics context * @param x0 X coordinate of arrow starting point * @param y0 Y coordinate of arrow starting point * @param x1 X coordinate of arrow ending point * @param y1 Y coordinate of arrow ending point * @param weight Arrow weight */ public static void drawArrow(Graphics g, double x0, double y0, double x1, double y1, double weight){ int ix2,iy2,ix3,iy3; double sinPhi,cosPhi,dx,dy,xk1,yk1,xk2,yk2,s; dx=x1-x0; dy=y1-y0; int maxArrowWidth = 10; //arrow length s=Math.sqrt(dy*dy+dx*dx); //arrow head length int headLength = (int) Math.round(s*0.5); double arrowAngle = Math.atan((double) (weight*maxArrowWidth)/headLength); double arrowAngle2 = Math.atan((double) maxArrowWidth/headLength); sinPhi=dy/s; // Winkel des Pfeils cosPhi=dx/s; // mit der X-Achse if(s * The arrow lies on the line between point from to point to and points to point to. * The distance between point from and the arrow start is defined by offsetFrom, * respectively for the arrow end, to and offsetTo. * @param g Graphics context * @param from Basic arrow starting point * @param to Basic arrow ending point * @param offsetFrom Distance between from and arrow starting point * @param offsetTo Distance between to and arrow ending point * @param weight Arrow weight */ public static void drawArrowOffset(Graphics g, Point from, Point to, int offsetFrom, int offsetTo, double weight){ double xk1,xk2,s,dx,dy,sinPhi,cosPhi; dx = to.x-from.x; dy = to.y-from.y; xk1=-offsetFrom/2; xk2=-offsetTo/2; s=Math.sqrt(dy*dy+dx*dx); sinPhi=dy/s; cosPhi=dx/s; drawArrow(g, from.getX()-xk1*cosPhi, from.getY()-xk1*sinPhi, to.getX()+xk2*cosPhi, to.getY()+xk2*sinPhi, weight); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy