001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.gui.util; 025 026 import java.awt.BasicStroke; 027 import java.awt.Dimension; 028 import java.awt.FontMetrics; 029 import java.awt.Graphics2D; 030 import java.awt.Stroke; 031 import java.awt.Toolkit; 032 import java.awt.Window; 033 034 public abstract class GraphicsUtil { 035 public static final int ALIGNCENTER = 0; 036 037 public static final int ALIGNLEFT = 1; 038 039 public static final int ALIGNRIGHT = 2; 040 041 public static final int ALIGNBOTTOM = 3; 042 043 public static final int ALIGNTOP = 4; 044 045 public static final float[] DASHPATTERN10 = { 1f, 1f }; 046 047 public static final float[] DASHPATTERN01 = { 0.1f, 0.1f }; 048 049 public static final Stroke DASHEDSTROKE = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 050 0.0f, DASHPATTERN10, 0); 051 052 public static final void drawString(Graphics2D g2d, double xPos, double yPos, int xAlign, int yAlign, String s) { 053 FontMetrics fm = g2d.getFontMetrics(g2d.getFont()); 054 if ((xAlign == ALIGNCENTER) && (yAlign == ALIGNCENTER)) { 055 g2d.drawString(s, (float) (xPos - (fm.getStringBounds(s, g2d).getWidth() / 2.0)), (float) (yPos + (g2d 056 .getFont().getSize2D() / 2.0))); 057 } else if ((xAlign == ALIGNCENTER) && (yAlign == ALIGNTOP)) { 058 g2d.drawString(s, (float) (xPos - (fm.getStringBounds(s, g2d).getWidth() / 2.0)), (float) (yPos + (g2d 059 .getFont().getSize2D()))); 060 } else if ((xAlign == ALIGNCENTER) && (yAlign == ALIGNBOTTOM)) { 061 g2d.drawString(s, (float) (xPos), (float) (yPos)); 062 } else if ((xAlign == ALIGNLEFT) && (yAlign == ALIGNCENTER)) { 063 g2d.drawString(s, (float) (xPos), (float) (yPos + (g2d.getFont().getSize2D() / 2.0))); 064 } else if ((xAlign == ALIGNLEFT) && (yAlign == ALIGNTOP)) { 065 g2d.drawString(s, (float) (xPos), (float) (yPos + (g2d.getFont().getSize2D()))); 066 } else if ((xAlign == ALIGNLEFT) && (yAlign == ALIGNBOTTOM)) { 067 g2d.drawString(s, (float) (xPos), (float) (yPos)); 068 } else if ((xAlign == ALIGNRIGHT) && (yAlign == ALIGNCENTER)) { 069 g2d.drawString(s, (float) (xPos - (fm.getStringBounds(s, g2d).getWidth())), (float) (yPos + (g2d.getFont() 070 .getSize2D() / 2.0))); 071 } else if ((xAlign == ALIGNRIGHT) && (yAlign == ALIGNTOP)) { 072 g2d.drawString(s, (float) (xPos - (fm.getStringBounds(s, g2d).getWidth())), (float) (yPos + (g2d.getFont() 073 .getSize2D()))); 074 } else if ((xAlign == ALIGNRIGHT) && (yAlign == ALIGNBOTTOM)) { 075 g2d.drawString(s, (float) (xPos - (fm.getStringBounds(s, g2d).getWidth())), (float) (yPos)); 076 } else { 077 // error 078 } 079 } 080 081 public static final void centerOnScreen(Window w) { 082 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 083 Dimension frameSize = w.getSize(); 084 if (frameSize.height > screenSize.height) { 085 frameSize.height = screenSize.height; 086 } 087 if (frameSize.width > screenSize.width) { 088 frameSize.width = screenSize.width; 089 } 090 w.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 091 } 092 093 public static final void drawString(Graphics2D g2d, double xPos, double yPos, int xAlign, String s) { 094 GraphicsUtil.drawString(g2d, xPos, yPos, xAlign, GraphicsUtil.ALIGNCENTER, s); 095 } 096 097 public static final void drawString(Graphics2D g2d, double xPos, double yPos, String s) { 098 GraphicsUtil.drawString(g2d, xPos, yPos, GraphicsUtil.ALIGNCENTER, GraphicsUtil.ALIGNCENTER, s); 099 } 100 101 public static final void drawString(Graphics2D g2d, int xPos, int yPos, String s) { 102 GraphicsUtil.drawString(g2d, xPos, yPos, GraphicsUtil.ALIGNCENTER, GraphicsUtil.ALIGNCENTER, s); 103 } 104 }