use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.
the class DragDisk method draw.
@Override
public void draw(Graphics g) {
if (showingDisk) {
pixelRadius = (int) (.01 * getGraph().X_SIZE);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 100));
g2d.fillOval(graph.X_SIZE / 2 - pixelRadius, graph.Y_SIZE / 2 - pixelRadius, pixelRadius * 2, pixelRadius * 2);
g2d.setColor(Color.BLACK);
g2d.drawString("Click to", graph.X_SIZE / 2 - (g2d.getFontMetrics().stringWidth("Click to") / 2), graph.Y_SIZE / 2 - g2d.getFontMetrics().getHeight() + 7);
g2d.drawString("Drag", graph.X_SIZE / 2 - (g2d.getFontMetrics().stringWidth("Drag") / 2), graph.Y_SIZE / 2 + (g2d.getFontMetrics().getHeight() - 7) * 2);
} else {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 50));
g2d.fillOval(graph.X_SIZE / 2 - pixelRadius, graph.Y_SIZE / 2 - pixelRadius, pixelRadius * 2, pixelRadius * 2);
}
}
use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.
the class GraphComponent method drawLineSeg.
protected void drawLineSeg(double x1, double y1, double x2, double y2, Color color, Graphics g) {
//right now this ignores the LINE_SIZE currently set, but it draws much faster than before
//I'll modify it to handle line thickness soon
g.setColor(color);
int screenX1 = gridxToScreen(x1);
int screenX2 = gridxToScreen(x2);
int screenY1 = gridyToScreen(y1);
int screenY2 = gridyToScreen(y2);
double slope = (y1 - y2) / (x1 - x2);
if (Double.isNaN(x1) || Double.isNaN(x2) || Double.isNaN(y1) || Double.isNaN(y2)) {
return;
}
if (x1 > graph.X_MAX) {
screenX1 = graph.X_PIC_ORIGIN + graph.X_SIZE;
screenY1 = gridyToScreen(y1 - slope * (x1 - graph.X_MAX));
}
if (x2 > graph.X_MAX) {
screenX2 = graph.X_PIC_ORIGIN + graph.X_SIZE;
screenY2 = gridyToScreen(y2 - slope * (x2 - graph.X_MAX));
if (screenX2 == screenX1) {
//they were both offscreen
return;
}
}
if (x1 < graph.X_MIN) {
screenX1 = graph.X_PIC_ORIGIN;
screenY1 = gridyToScreen(y1 + slope * (graph.X_MIN - x1));
}
if (x2 < graph.X_MIN) {
screenX2 = graph.X_PIC_ORIGIN;
screenY2 = gridyToScreen(y2 + slope * (graph.X_MIN - x2));
if (screenX2 == screenX1) {
//they were both offscreen
return;
}
}
if (y1 < graph.Y_MIN) {
screenY1 = graph.Y_PIC_ORIGIN + graph.Y_SIZE;
//screenX1 = gridxToScreen(x1 + slope * (graph.Y_MIN - y1));
}
if (y2 < graph.Y_MIN) {
screenY2 = graph.Y_PIC_ORIGIN + graph.Y_SIZE;
//screenX2 = gridxToScreen(x2 + slope * (graph.Y_MIN - y2));
if (screenY2 == screenY1) {
//they were both offscreen
return;
}
}
if (y1 > graph.Y_MAX) {
screenY1 = graph.Y_PIC_ORIGIN;
}
if (y2 > graph.Y_MAX) {
screenY2 = graph.Y_PIC_ORIGIN;
if (screenY2 == screenY1) {
//they were both offscreen
return;
}
}
int thickness = (int) (graph.LINE_SIZE * graph.DOC_ZOOM_LEVEL);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(thickness));
g.setColor(color);
g.drawLine(screenX1, screenY1, screenX2, screenY2);
g2d.setStroke(new BasicStroke());
}
use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.
the class LineGraph method draw.
@Override
public void draw(Graphics g) {
//used to temporarily store the value stored in the independent and dependent vars,
//this will allow it to be restored after graphing, so that if in the terminal a
//value was assigned to x, it will not be overriden by the action of graphing
// Number xVal = getIndependentVar().getValue();
// Number yVal = getDependentVar().getValue();
super.clearPts();
Graphics2D g2d = ((Graphics2D) g);
g.setColor(getColor());
if (hasFocus()) {
graph.LINE_SIZE = 3;
} else {
graph.LINE_SIZE = 2;
}
double lastX = 0, lastY = 0, currX = 0, currY = 0;
if (!linePoints.isEmpty()) {
lastX = linePoints.get(0).getx();
lastY = linePoints.get(0).gety();
}
for (GridPoint pt : linePoints) {
currX = pt.getx();
currY = pt.gety();
if (gridxToScreen(currX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(currX) >= graph.X_PIC_ORIGIN && gridyToScreen(currY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(currY) >= graph.Y_PIC_ORIGIN) {
//if the current point is on the screen, add it to the list of points
if (lastY <= graph.Y_MIN) {
addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
}
if (lastY >= graph.Y_MAX) {
addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
}
addPt(gridxToScreen(currX), gridyToScreen(currY));
} else if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= graph.Y_PIC_ORIGIN) {
//if the last point is on the screen, add the correct boundary for this point to the list
addPt(gridxToScreen(lastX), gridyToScreen(lastY));
if (currY <= graph.Y_MIN) {
addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
}
if (currY >= graph.Y_MAX) {
addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
}
} else if (lastY >= graph.Y_MAX && currY <= graph.Y_MIN) {
//if the last point was off the the top of the screen, and this one is off
//the bottom, add the two to the list of points
addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
} else if (currY >= graph.Y_MAX && lastY <= graph.Y_MIN) {
//if the last point was off the the bottom of the screen, and this one is off
//the top, add the two to the list of points
addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
}
drawLineSeg(lastX, lastY, currX, currY, getColor(), g);
lastX = currX;
lastY = currY;
}
// g2d.setStroke(new BasicStroke(graph.LINE_SIZE * graph.DOC_ZOOM_LEVEL,
// BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
// if ( this.getxVals().size() > 0){
// GeneralPath polyline =
// new GeneralPath(GeneralPath.WIND_EVEN_ODD, this.getxVals().size());
// polyline.moveTo (this.getxVals().get(0), this.getyVals().get(0));
// for (int i = 1; i < this.getxVals().size(); i++) {
// polyline.lineTo( getxVals().get(i), getyVals().get(i));
// };
// g2d.draw(polyline);
// }
graph.LINE_SIZE = 2;
g2d.setStroke(new BasicStroke(1));
}
use of java.awt.Graphics2D in project jna by java-native-access.
the class DragHandler method createDragImage.
/** Create an image from the given icon. The image is provided to the
* native handler if drag images are supported natively.
* @param gc current graphics configuration.
* @param icon Icon on which to base the drag image.
* @return image based on the given icon.
*/
protected Image createDragImage(GraphicsConfiguration gc, Icon icon) {
int w = icon.getIconWidth();
int h = icon.getIconHeight();
BufferedImage image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, w, h);
// Ignore pixels in the buffered image
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, ghostAlpha));
icon.paintIcon(dragSource, g, 0, 0);
g.dispose();
return image;
}
use of java.awt.Graphics2D in project groovy by apache.
the class JCaricature method paintComponent.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (empty) {
return;
}
Graphics2D g2 = (Graphics2D) g.create();
Image image = getImage("face", getFaceStyle());
int iw = image.getWidth(null);
int ih = image.getHeight(null);
// g2.translate(iw / 2, ih / 2);
g2.translate(getWidth() / 2, getHeight() / 2);
if (iw != getWidth()) {
float forcedScale = (float) getWidth() / (float) iw;
g2.scale(forcedScale, forcedScale);
}
float scale = getScale();
if (scale != 1) {
g2.scale((double) scale, (double) scale);
}
int rotation = getRotation();
if (rotation != 0) {
g2.rotate(Math.toRadians(rotation));
}
drawImage(g2, "face", getFaceStyle());
drawImage(g2, "hair", getHairStyle());
drawImage(g2, "eyes", getEyeStyle());
drawImage(g2, "nose", getNoseStyle());
drawImage(g2, "mouth", getMouthStyle());
g2.dispose();
}
Aggregations