/*
* Title: japlot.jaxodraw
* Description: Graphical user interface for drawing Feynman diagrams
* @author Daniele Binosi
* @author Lukas Theussl
Copyright (C) 2003-2006, Daniele Binosi and Lukas Theussl
See the file LICENSE in the source distribution home directory
for a full copy of the GPL (GNU General Public License).
This file is part of japlot.jaxodraw.
japlot.jaxodraw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
japlot.jaxodraw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package japlot.jaxodraw;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
/** Draws agrid on the canvas. */
public class JaxoGrid {
/** The current screen size*/
private Dimension d;
/** The size of the grid, i.e., the distance between two grid points.*/
private int gridsize;
/** The type of grid: rectangular or hexagonal.*/
private boolean hexagonal;
// Needed for hexagonal X snapping
private boolean isEvenRow = true;
/**
* Constructor: Sets the dimension to the current screen size
* and the grid size to gs.
* @param hx A string that indicates whether the grid is hexagonal or not.
* @param gs The grid size to be set.
*/
public JaxoGrid(int gs, String hx) {
this.d = Toolkit.getDefaultToolkit().getScreenSize();
this.gridsize = gs;
if (hx.equals("hexagonal")) {
this.hexagonal = true;
} else {
this.hexagonal = false;
}
}
/** Returns the x coordinate of the grid point that is closest to the
* given coordinate.
* @param x The current x coordinate.
* @return The x coordinate of the grid point corresponding to x.
*/
public final int snapX(int x) {
int snappedX;
if (hexagonal) {
if (isEvenRow) {
int snappedX1 =
-Math.round(gridsize / 2.f)
+ (gridsize * (Math.round(x / ((float) gridsize))));
int snappedX2 =
Math.round(gridsize / 2.f)
+ (gridsize * (Math.round(x / ((float) gridsize))));
if (Math.abs(x - snappedX1) < Math.abs(x - snappedX2)) {
snappedX = snappedX1;
} else {
snappedX = snappedX2;
}
} else {
snappedX = gridsize * (Math.round(x / ((float) gridsize)));
}
} else {
//Normal grid X snapping
snappedX = gridsize * (Math.round(x / ((float) gridsize)));
}
return snappedX;
}
/** Returns the y coordinate of the grid point that is closest to the
* given coordinate.
* @param y The current y coordinate.
* @return The y coordinate of the grid point corresponding to y.
*/
public final int snapY(int y) {
int snappedY;
if (hexagonal) {
//Hexagonal grid Y snapping: IT IS IMPORTANT THAT THE SNAPPING
//OF A HEXAGONAL GRID IS DONE FIRST IN THE Y COORD (WHICH
//DETERMINES IF THE LINE IS EVEN OR ODD) AND THEN IN
//THE X COORD.
double tanfact = Math.tan(Math.toRadians(60));
snappedY =
(int) Math.round(((tanfact * gridsize) / 2) * Math.round(
y / ((tanfact * gridsize) / 2)));
if ((int) (Math.round(y / ((tanfact * gridsize) / 2)) % 2) == 1) {
isEvenRow = true;
} else {
isEvenRow = false;
}
} else {
//Normal grid Y snapping
snappedY = gridsize * (Math.round(y / ((float) gridsize)));
}
return snappedY;
}
/** Draws the grid.
* @param g2 The graphics context to draw the grid.
*/
public final void jaxoDrawGrid(Graphics2D g2) {
int width = d.width;
int height = d.height;
double tanfact = Math.tan(Math.toRadians(60));
float[] dash = {1.f, (float) (gridsize - 1)};
g2.setStroke(new BasicStroke(1.f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.f, dash, 0.f));
g2.setColor(JaxoColor.GRAYSCALE120);
for (int i = 0; (i * gridsize) < (height - 1); i++) {
if (hexagonal) {
//Hexagonal Grid
g2.drawLine(0, (int) Math.round((i * tanfact * gridsize) / 2),
width - 1, (int) Math.round((i * tanfact * gridsize) / 2));
g2.drawLine(-gridsize / 2,
(int) Math.round(((i + 1) * tanfact * gridsize) / 2),
width + (gridsize / 2),
(int) Math.round(((i + 1) * tanfact * gridsize) / 2));
i += 1;
} else {
//Normal Grid
g2.drawLine(0, i * gridsize, width - 1, i * gridsize);
}
}
g2.setStroke(new BasicStroke(1.f));
}
/** Sets the size of the grid to the given value.
* @param gs The gridsize to be set.
*/
public final void setGridSize(int gs) {
this.gridsize = gs;
}
/** Returns the current size of the grid.
* @return The grid size.
*/
public final int getGridSize() {
return gridsize;
}
/** Sets the type of the grid (false: rectangular, true: hexagonal).
* @param hx The gridtype to be set.
*/
public final void setGridType(boolean hx) {
this.hexagonal = hx;
}
/** Returns the type of the grid.
* @return The gridtype (false: rectangular, true: hexagonal).
*/
public final boolean getGridType() {
return hexagonal;
}
}
Ads help maintain this website.