use of java.awt.Composite in project cytoscape-impl by cytoscape.
the class BoundedTextAnnotationImpl method paint.
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(textColor);
g2.setFont(font);
// Handle opacity
int alpha = textColor.getAlpha();
float opacity = (float) alpha / (float) 255;
final Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
int halfWidth = (int) (getWidth() - getTextWidth(g2)) / 2;
// Note, this is + because we start at the baseline
int halfHeight = (int) (getHeight() + getTextHeight(g2) / 2) / 2;
if (usedForPreviews) {
g2.drawString(text, halfWidth, halfHeight);
g2.setComposite(originalComposite);
return;
}
g2.drawString(text, halfWidth, halfHeight);
g2.setComposite(originalComposite);
}
use of java.awt.Composite in project cytoscape-impl by cytoscape.
the class GraphGraphics method clear.
/**
* Clears image area with background paint specified and sets an appropriate
* transformation of coordinate systems. See the class description for a
* definition of the two coordinate systems: the node coordinate system and
* the image coordinate system.
* <p>
* The background paint is not blended with colors that may already be on
* the underlying image; if a translucent color is used in the background
* paint, the underlying image itself becomes translucent.
* <p>
* It is mandatory to call this method before making the first rendering
* call.
*
* @param bgPaint
* paint to use when clearing the image before painting a new
* frame; translucency is honored, provided that the underlying
* image supports it.
* @param xCenter
* the X component of the translation transform for the frame
* about to be rendered; a node whose center is at the X
* coordinate xCenter will be rendered exactly in the middle of
* the image going across; increasing X values (in the node
* coordinate system) result in movement towards the right on the
* image.
* @param yCenter
* the Y component of the translation transform for the frame
* about to be rendered; a node whose center is at the Y
* coordinate yCenter will be rendered exactly in the middle of
* the image going top to bottom; increasing Y values (in the
* node coordinate system) result in movement towards the bottom
* on the image.
* @param scaleFactor
* the scaling that is to take place when rendering; a distance
* of 1 in node coordinates translates to a distance of
* scaleFactor in the image coordinate system (usually one unit
* in the image coordinate system equates to one pixel width).
* @exception IllegalArgumentException
* if scaleFactor is not positive.
*/
public final void clear(final Paint bgPaint, final double xCenter, final double yCenter, final double scaleFactor) {
if (m_debug) {
checkDispatchThread();
if (!(scaleFactor > 0.0d)) {
throw new IllegalArgumentException("scaleFactor is not positive");
}
}
if (m_gMinimal != null) {
m_gMinimal.dispose();
m_gMinimal = null;
}
if (m_g2d != null) {
m_g2d.dispose();
}
m_g2d = (Graphics2D) image.getGraphics();
if (m_clear) {
final Composite origComposite = m_g2d.getComposite();
m_g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
m_g2d.setPaint(bgPaint);
m_g2d.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
m_g2d.setComposite(origComposite);
}
// For detailed view, render high quality image as much as possible.
// Antialiasing is ON
m_g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Rendering quality is HIGH.
m_g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// High quality alpha blending is ON.
m_g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
// High quality color rendering is ON.
m_g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
m_g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
m_g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
// Text antialiasing is ON.
m_g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
m_g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
m_g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
m_g2d.setStroke(new BasicStroke(0.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f));
setTransform(xCenter, yCenter, scaleFactor);
m_g2d.transform(m_currXform);
m_currNativeXform.setTransform(m_g2d.getTransform());
m_cleared = true;
}
use of java.awt.Composite in project RSyntaxTextArea by bobbylight.
the class ChangeableHighlightPainter method paint.
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
@Override
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
// Set up translucency if necessary.
Graphics2D g2d = (Graphics2D) g;
Composite originalComposite = null;
if (getAlpha() < 1.0f) {
originalComposite = g2d.getComposite();
g2d.setComposite(getAlphaComposite());
}
try {
// Determine locations.
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
Paint paint = getPaint();
if (paint == null) {
g2d.setColor(c.getSelectionColor());
} else {
g2d.setPaint(paint);
}
// Entire highlight is on one line.
if (p0.y == p1.y) {
// Standard Swing views return 0 width for chars, but ours
// returns the char's width. Set this to 0 here since p1 is
// technically an exclusive boundary.
p1.width = 0;
Rectangle r = p0.union(p1);
g2d.fillRect(r.x, r.y, r.width, r.height);
} else // Highlight spans lines.
{
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
g2d.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g2d.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y - (p0.y + p0.height));
}
g2d.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// Never happens.
e.printStackTrace();
} finally {
// Restore state from before translucency if necessary.
if (getAlpha() < 1.0f) {
g2d.setComposite(originalComposite);
}
}
}
use of java.awt.Composite in project bigwarp by saalfeldlab.
the class BigWarpOverlay method paint.
public void paint(final Graphics2D g) {
// Save graphic device original settings
final Composite originalComposite = g.getComposite();
final Stroke originalStroke = g.getStroke();
final Color originalColor = g.getColor();
// get selected points
int[] selectedRows = table.getSelectedRows();
Arrays.sort(selectedRows);
boolean[] isSelected = new boolean[landmarkModel.getRowCount()];
for (int i : selectedRows) isSelected[i] = true;
/*
* Draw spots.
*/
if (viewer.getSettings().areLandmarksVisible()) {
final double radiusRatio = (Double) viewer.getSettings().get(BigWarpViewerSettings.KEY_SPOT_RADIUS_RATIO);
final double radius = viewer.getSettings().getSpotSize();
Color color;
Stroke stroke;
stroke = BigWarpViewerSettings.NORMAL_STROKE;
FontMetrics fm = null;
int fonthgt = 0;
Color textBoxColor = null;
if (viewer.getSettings().areNamesVisible()) {
fm = g.getFontMetrics(g.getFont());
fonthgt = fm.getHeight();
textBoxColor = Color.BLACK;
}
final int nRows = landmarkModel.getRowCount();
for (int index = 0; index < nRows; index++) {
if (landmarkModel.isActive(index))
color = viewer.getSettings().getSpotColor();
else
color = viewer.getSettings().getInactiveSpotColor();
g.setColor(color);
g.setStroke(stroke);
landmarkModel.copyPointSafe(spot, index, isMoving);
// if the viewer is moving but transformed, render the points
// at the location of the warped point ( if it exists ),
// otherwise, take the fixed point
// if ( isMoving && viewer.isInFixedImageSpace() )
// {
// if ( landmarkModel.isWarped( index ) )
// spot = landmarkModel.getWarpedPoints().get( index );
// else
// spot = landmarkModel.getPoints( false ).get( index );
// }
boolean copySuccess = false;
if (isMoving) {
if (viewer.isInFixedImageSpace()) {
if (landmarkModel.isWarped(index))
copySuccess = landmarkModel.copyWarpedPointSafe(spot, index);
else
copySuccess = landmarkModel.copyTargetPointSafe(spot, index);
} else
copySuccess = landmarkModel.copyMovingPointSafe(spot, index);
} else {
// fixed space
copySuccess = landmarkModel.copyTargetPointSafe(spot, index);
}
// if this point is not set, don't render it.
if (!copySuccess || Double.isInfinite(spot[0]))
continue;
transform.apply(spot, viewerCoords);
final double rad = radius * radiusRatio;
final double zv = viewerCoords[2];
final double dz2 = zv * zv;
if (!is3d || dz2 < rad * rad) {
final double arad;
if (is3d)
arad = Math.sqrt(rad * rad - dz2);
else
arad = rad;
// vary size
g.fillOval((int) (viewerCoords[0] - arad), (int) (viewerCoords[1] - arad), (int) (2 * arad + 1), (int) (2 * arad + 1));
if (isSelected[index]) {
g.drawOval((int) (viewerCoords[0] - arad - 2), (int) (viewerCoords[1] - arad - 2), (int) (2 * arad + 4), (int) (2 * arad + 4));
} else if (hoveredIndex == index) {
g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 128));
g.drawOval((int) (viewerCoords[0] - arad - 2), (int) (viewerCoords[1] - arad - 2), (int) (2 * arad + 4), (int) (2 * arad + 4));
g.setColor(color);
}
if (viewer.getSettings().areNamesVisible()) {
final int tx = (int) (viewerCoords[0] + arad + 5);
final int ty = (int) viewerCoords[1];
String name = landmarkModel.getNames().get(index);
int strwidth = fm.stringWidth(name);
if (isSelected[index] || hoveredIndex == index)
textBoxColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), 255);
else
textBoxColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), 128);
g.setColor(textBoxColor);
g.fillRect(tx - 1, ty - fonthgt + 2, strwidth + 2, fonthgt);
g.setColor(Color.BLACK);
g.drawString(name, tx, ty);
}
}
}
}
// Restore graphic device original settings
g.setComposite(originalComposite);
g.setStroke(originalStroke);
g.setColor(originalColor);
}
use of java.awt.Composite in project facetmodeller by pglelievre.
the class View3DPanel method paintComponent.
// -------------------- Overridden Methods --------------------
/**
* Paints graphics on the panel.
* @param g Graphics context in which to draw.
*/
@Override
public void paintComponent(Graphics g) {
// Only draw 3D if panel is open and ndim==3:
if (!controller.is3D() || !controller.getShowView3DPanel()) {
return;
}
// Remove image from the panel:
// removeAll();
// updateUI();
// Make sure the drawn flag is false:
drawn = false;
// Paint background:
super.paintComponent(g);
// Clear the list of projected nodes:
projectedPoints = null;
// Calculate the properties required for projection:
SceneInfo info = controller.getSceneInfo3D();
// no voi or no nodes in plc
if (info == null) {
return;
}
if (info.getScaling() == 0.0) {
return;
}
MyPoint3D spaceOrigin = info.getOrigin();
projector.setSpaceOrigin(spaceOrigin);
if (spaceOrigin == null) {
return;
}
// panel width
int w = getWidth();
// panel height
int h = getHeight();
double x = w;
double y = h;
// smallest panel dimension
double imageSizeScaling = Math.min(x, y);
// half panel width
x /= 2.0;
// half panel height
y /= 2.0;
// radius of circle surrounding the panel
double r = Math.sqrt(x * x + y * y);
MyPoint3D imageOrigin = new MyPoint3D(x, y, 0.0);
// imageOrigin.plus(controller.getPanX3D(),controller.getPanY3D(),0); // this does the panning via pan buttons
// this does the panning via dragging
imageOrigin.plus(getPanX(), getPanY(), 0);
projector.setImageOrigin(imageOrigin);
double sceneAndZoomScaling = info.getScaling() * zoomer.getScaling();
if (sceneAndZoomScaling == 0.0) {
return;
}
if (imageSizeScaling == 0.0) {
return;
}
projector.setSceneAndZoomScaling(sceneAndZoomScaling);
projector.setImageSizeScaling(imageSizeScaling);
dragSphere = new Circle(0, 0, r);
// Project all the nodes and reset the node IDs:
ModelManager model = controller.getModelManager();
int n = model.numberOfNodes();
projectedPoints = new MyPoint3D[n];
for (int i = 0; i < n; i++) {
// loop over each node
Node node = model.getNode(i);
// Set the node ID equal to i so I can use the ID's as indices into the projectedPoints array:
node.setID(i);
// Transform the point to image coordinates:
MyPoint3D p3 = node.getPoint3D();
MyPoint3D p;
if (p3 == null) {
// section not calibrated
p = null;
} else {
// p is a deep copy of node.getPoint3D()
p = spaceToImage(p3);
}
// Save the transformed point in the list:
projectedPoints[i] = p;
}
// If rotating then I'll only draw minimal information using 2D graphics,
// otherwise I'll draw everything using a zbuffer. I'll deal with this below
// by checking the status of the pt1 variable. I'll need some common information first:
// boolean showOther = controller.getShowOther(); // if true then draws outlines around other sections
// if true then draws outlines around other sections
boolean showOutlines = controller.getShowSectionOutlines();
// if false then only show nodes/facets associated with selected section(s)
boolean showAll = controller.getShowAllSections();
// Get drawing styles for overlays:
final int edgeWidth = controller.getLineWidth();
final int nodeWidth = controller.getPointWidth();
// final int centroidWidth = (int)Math.ceil(nodeWidth/2.0); // centroids drawn half the node width
final double normalLength = controller.getNormalLength();
final Color normalCol = controller.getNormalColor();
boolean normalThick = controller.getNormalThick();
boolean edgeThick = controller.getEdgeThick();
final int normalWidth;
if (normalThick) {
normalWidth = 8;
} else {
normalWidth = 4;
}
// Initialize the zbuffer or Graphics2D object:
Graphics2D g2 = (Graphics2D) g;
MyPoint3D pt1 = getPt1();
if (pt1 == null) {
zbuf = new ZBuffer3D(w, h, -Double.MAX_VALUE, getBackground());
} else {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setStroke(new BasicStroke(edgeWidth)); // line style for facet edges
w = nodeWidth;
}
// Get the other section(s) being displayed (if any exist):
Section currentSection = controller.getSelectedCurrentSection();
SectionVector otherSections;
// if ( showOther || showAll ) {
if (showOutlines || showAll) {
otherSections = controller.getSelectedOtherSections();
} else {
otherSections = null;
}
// Get the drawing options for colouring the nodes and facets:
int nodeColorBy = controller.getNodeColorBy();
int facetColorBy = controller.getFacetColorBy();
// Add facets to the scene: (this should be done first so that
// z-buffering can't draw a facet edge over a node)
boolean showFaces = controller.getShowFaces();
boolean showNormals = controller.getShowNormals();
boolean showNormalTails = controller.getShowNormalTails();
boolean showNormalHeads = controller.getShowNormalHeads();
if (pt1 == null) {
GroupVector facetGroups = controller.getSelectedFacetGroups();
if (facetGroups != null) {
for (int i = 0; i < facetGroups.size(); i++) {
// loop over each group of facets to display
Group group = facetGroups.get(i);
FacetVector facets = group.getFacets();
for (int j = 0; j < facets.size(); j++) {
// loop over each facet in the group
Facet facet = facets.get(j);
// Make sure that all the nodes in the facet are calibrated:
NodeVector nodes = facet.getNodes();
int nn = nodes.size();
// only 3D facets are drawn
if (nn < 3) {
continue;
}
boolean ok = true;
for (int k = 0; k < nn; k++) {
// loop over each node in the facet
Node node = nodes.get(k);
MyPoint3D p = node.getPoint3D();
// if the node's section is not calibrated
if (p == null) {
ok = false;
break;
}
if (!showAll) {
// Make sure the facet belongs to no other sections than those selected:
Section s = node.getSection();
// shouldn't be possible
if (s == null) {
ok = false;
break;
}
if (!s.equals(currentSection) && otherSections != null && !otherSections.contains(s)) {
ok = false;
break;
}
}
}
if (!ok) {
continue;
}
// Place the required transformed points into a new object:
n = facet.size();
MyPoint3D[] pts = new MyPoint3D[n];
for (int k = 0; k < n; k++) {
// loop over each node in the facet
// index into projectedPoints
int id = facet.getNode(k).getID();
pts[k] = projectedPoints[id];
if (pts[k] == null) {
// node is behind camera, or its section is not calibrated, so don't paint the facet
ok = false;
break;
}
}
if (!ok) {
continue;
}
// Calculate the shading:
MyPoint3D v = facet.getNormal();
if (v == null) {
continue;
}
// normalized
v = v.deepCopy();
if (v == null) {
continue;
}
// rotated into projected coordinates
v.rotate(projector.getRotationMatrix());
// dotted with the z axis (direction not important)
double d = Math.abs(v.getZ());
// too dark otherwise
d = 1.0 - (1.0 - d) * 0.8;
// Determine the painting colour for the facet:
Color col = getFacetPaintingColor(facetColorBy, facet);
// Apply the shading to the painting colour:
float[] hsb = new float[3];
Color.RGBtoHSB(col.getRed(), col.getGreen(), col.getBlue(), hsb);
// d is on [0,1]
hsb[2] *= (float) d;
col = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
// Process the facet through the zbuffer:
Color faceCol = null;
Color edgeCol;
if (showFaces) {
// Paint facet face as a coloured patch and paint edges as user requests:
faceCol = col;
edgeCol = controller.getEdgeColor();
} else {
// Paint edges of facet only, using facet colour:
// faceColor = null;
edgeCol = col;
}
zbuf.putFacet(pts, faceCol, edgeCol, edgeThick);
// Draw the facet normals:
if (showNormals) {
// Set up the points on either end of the normal vector line:
// facet centroid (point at start of normal vector line)
MyPoint3D p0 = facet.getCentroid().deepCopy();
if (p0 == null) {
continue;
}
// normalized normal vector (length of one)
MyPoint3D p1 = facet.getNormal().deepCopy();
if (p1 == null) {
continue;
}
// normal vector of spatial length normalLength
p1.times(normalLength);
// point at end of normal vector line
p1.plus(p0);
// Transform the points to image coordinates:
p0 = spaceToImage(p0);
p1 = spaceToImage(p1);
if (p0 == null) {
continue;
}
if (p1 == null) {
continue;
}
// Draw normal vectors as line objects:
if (pt1 == null) {
zbuf.putEdge(p0, p1, normalCol, normalThick);
// point at normal tail
if (showNormalTails) {
zbuf.putNode(p0, normalCol, normalWidth);
}
// point at normal head
if (showNormalHeads) {
zbuf.putNode(p1, normalCol, normalWidth);
}
} else {
g2.setPaint(normalCol);
g2.drawLine((int) p0.getX(), (int) p0.getY(), (int) p1.getX(), (int) p1.getY());
// circle at normal tail
if (showNormalTails) {
g2.fillOval((int) p0.getX() - normalWidth / 2, (int) p0.getY() - normalWidth / 2, normalWidth, normalWidth);
}
// circle at normal head
if (showNormalHeads) {
g2.fillOval((int) p1.getX() - normalWidth / 2, (int) p1.getY() - normalWidth / 2, normalWidth, normalWidth);
}
}
}
}
// for j
}
// for i
}
// if (facetGroups!=null)
}
// if (pt1==null) // not rotating
// Add nodes to the scene:
Composite defaultComposite = g2.getComposite();
if (pt1 != null) {
float alpha = (float) 0.5;
int type = AlphaComposite.SRC_OVER;
AlphaComposite composite = AlphaComposite.getInstance(type, alpha);
g2.setComposite(composite);
}
GroupVector nodeGroups = controller.getSelectedNodeGroups();
if (nodeGroups != null) {
for (int i = 0; i < nodeGroups.size(); i++) {
// loop over each group of nodes to display
Group group = nodeGroups.get(i);
NodeVector nodes = group.getNodes();
for (int j = 0; j < nodes.size(); j++) {
// loop over each node in the group
Node node = nodes.get(j);
Section s = node.getSection();
// shouldn't be possible
if (s == null) {
continue;
}
// not possible but I check for it anyway
if (!s.isCalibrated()) {
continue;
}
if (!showAll && !s.equals(currentSection) && otherSections != null && !otherSections.contains(s)) {
continue;
}
// index into projectedPoints
int id = node.getID();
// Determine the painting colour for the node:
Color col = getNodePaintingColor(nodeColorBy, node);
// Paint the node:
if (pt1 == null) {
if (projectedPoints[id] != null) {
// only paint if node is in front of the camera, and if its section is calibrated
zbuf.putNode(projectedPoints[id], col, controller.getPointWidth());
}
} else {
MyPoint3D p = spaceToImage(node.getPoint3D());
// to next iteration of for j
if (p == null) {
continue;
}
g2.setPaint(col);
g2.fillOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// for j
}
// for i
}
// Add regions to the scene:
if (pt1 == null) {
if (controller.getShowRegions()) {
for (int i = 0; i < model.numberOfRegions(); i++) {
Region region = model.getRegion(i);
Section s = region.getSection();
// shouldn't be possible
if (s == null) {
continue;
}
// not possible but I check for it anyway
if (!s.isCalibrated()) {
continue;
}
if (!showAll && !s.equals(currentSection) && otherSections != null && !otherSections.contains(s)) {
continue;
}
// Project the region point:
MyPoint3D p = spaceToImage(region.getPoint3D());
// region is behind camera
if (p == null) {
continue;
}
// Process the region through the zbuffer:
zbuf.putNode(p, region.getColor(), controller.getPointWidth());
}
}
}
// Draw the outlines of the selected sections:
if (pt1 == null) {
SectionVector sections = controller.getSelectedOtherSections();
// if ( showOther && sections!=null ) {
if (showOutlines && sections != null) {
for (int i = 0; i < sections.size(); i++) {
// loop over each section to display
Section section = sections.get(i);
// Construct the section outline in section image pixel coordinates:
// 4 corners
n = 4;
MyPoint2D[] pts2D = new MyPoint2D[n];
w = section.getWidth();
h = section.getHeight();
if (w <= 0 || h <= 0) {
continue;
}
w--;
h--;
pts2D[0] = new MyPoint2D(0, 0);
pts2D[1] = new MyPoint2D(w, 0);
pts2D[2] = new MyPoint2D(w, h);
pts2D[3] = new MyPoint2D(0, h);
// Transform the outline from 2D image pixels to space coordinates:
MyPoint3D[] pts3D = new MyPoint3D[n];
boolean ok = true;
for (int j = 0; j < n; j++) {
MyPoint3D p = section.imageCornerToSpace(pts2D[j]);
// null if section not calibrated
if (p == null) {
ok = false;
break;
}
pts3D[j] = p;
}
if (!ok) {
continue;
}
// Project the outline (from space to plotting pixel coordinates):
for (int j = 0; j < n; j++) {
MyPoint3D p = spaceToImage(pts3D[j]);
// point is behind camera
if (p == null) {
ok = false;
break;
}
pts3D[j] = p;
}
// for j
if (!ok) {
continue;
}
// Draw as black line objects:
for (int j = 0; j < n; j++) {
int j2 = j + 1;
if (j2 == n) {
j2 = 0;
}
zbuf.putEdge(pts3D[j], pts3D[j2], Color.BLACK);
}
}
// for i
}
}
// Draw the VOI:
if (pt1 != null) {
g2.setPaint(Color.BLACK);
g2.setComposite(defaultComposite);
}
if (controller.getShowVOI()) {
if (controller.hasVOI()) {
MyPoint3D[][] edges = controller.getVOIEdges();
for (int i = 0; i < VOI.N_EDGES; i++) {
MyPoint3D p1 = spaceToImage(edges[i][0]);
MyPoint3D p2 = spaceToImage(edges[i][1]);
// point(s) behind camera
if (p1 == null || p2 == null) {
continue;
}
if (pt1 == null) {
zbuf.putEdge(p1, p2, Color.BLACK);
} else {
g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
}
}
// for i
}
}
// Draw the axes:
if (showAxes) {
// Set up axis points:
// origin
MyPoint3D po = spaceOrigin.deepCopy();
// x axis
MyPoint3D px = MyPoint3D.plus(po, new MyPoint3D(AXIS_LENGTH, 0, 0));
// y axis
MyPoint3D py = MyPoint3D.plus(po, new MyPoint3D(0, AXIS_LENGTH, 0));
// z axis
MyPoint3D pz = MyPoint3D.plus(po, new MyPoint3D(0, 0, AXIS_LENGTH));
// Project with no scaling:
po = spaceToImage(po, 1.0);
px = spaceToImage(px, 1.0);
py = spaceToImage(py, 1.0);
pz = spaceToImage(pz, 1.0);
// Move to bottom left corner if desired:
if (!centreAxes) {
po.minus(imageOrigin);
px.minus(imageOrigin);
py.minus(imageOrigin);
pz.minus(imageOrigin);
MyPoint3D p = new MyPoint3D(AXIS_LENGTH + 5, getHeight() - AXIS_LENGTH - 5, 0.0);
po.plus(p);
px.plus(p);
py.plus(p);
pz.plus(p);
}
// Draw axes as coloured line objects:
if (pt1 == null) {
zbuf.putEdge(po, px, Color.RED);
zbuf.putEdge(po, py, Color.YELLOW);
zbuf.putEdge(po, pz, Color.GREEN);
} else {
g2.setPaint(Color.RED);
g2.drawLine((int) po.getX(), (int) po.getY(), (int) px.getX(), (int) px.getY());
g2.setPaint(Color.YELLOW);
g2.drawLine((int) po.getX(), (int) po.getY(), (int) py.getX(), (int) py.getY());
g2.setPaint(Color.GREEN);
g2.drawLine((int) po.getX(), (int) po.getY(), (int) pz.getX(), (int) pz.getY());
}
}
// There is nothing more to draw if rotating:
if (pt1 != null) {
drawn = true;
return;
}
// Get the zbuffer image:
BufferedImage image = zbuf.getImage();
// Return if no image exists:
if (image == null) {
return;
}
// Tightly fit the image inside the panel (a very simple fitting since they are the same size!):
tightFitImage(g, image);
// Return if no sections exist:
if (!model.hasSections()) {
drawn = true;
return;
}
// Return if no current section exists:
if (currentSection == null) {
return;
}
// Figure out where the cursor is:
boolean mouseInside2D = controller.getMouseInside2D();
// Get mouse click mode:
int mode = controller.getClickMode();
// Redefine w for use below:
w = 2 * nodeWidth;
// Set stroke for facet edge overlays:
BasicStroke dashedStroke = new BasicStroke(edgeWidth + 1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, (float) 10.0, new float[] { 2f, 16f }, (float) 0.0);
g2.setStroke(dashedStroke);
g2.setPaint(controller.getDefineFacetEdgeColor());
// Paint the edges of the current facet being defined:
Facet currentFacet = controller.getCurrentFacet();
if (currentFacet != null) {
// Create a path around the node coordinates:
if (currentFacet.size() > 1) {
GeneralPath path = addFacetToPath(currentFacet);
if (path != null) {
// May want to close the path:
if (mode == ClickModeManager.MODE_DEFINE_TRI_FACETS || mode == ClickModeManager.MODE_ADD_NODES_IN_FACETS) {
path.closePath();
}
// Draw the path:
g2.draw(path);
}
}
}
// Paint the edges of the facet closest to the cursor position:
if (mouseInside2D && currentFacet == null) {
Facet facet = controller.getClosestFacet();
if (facet != null) {
// Create a path around the node coordinates:
if (facet.size() > 1) {
GeneralPath path = addFacetToPath(facet);
if (path != null) {
// Close the path:
path.closePath();
// Draw the path:
g2.draw(path);
}
}
}
}
// Set stroke for node overlays:
g2.setStroke(new BasicStroke(edgeWidth));
// Paint circles around the nodes of the current facet being defined:
if (currentFacet != null) {
if (mode == ClickModeManager.MODE_DEFINE_TRI_FACETS) {
g2.setPaint(Color.WHITE);
} else {
g2.setPaint(Color.BLACK);
}
for (int i = 0; i < currentFacet.size(); i++) {
Node node = currentFacet.getNode(i);
// Only paint the node if it is in the current or other sections:
Section section = node.getSection();
if (section.equals(currentSection) || (otherSections != null && otherSections.contains(section))) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p == null) {
continue;
}
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// for i
}
// Paint circles around the nodes of the facet closest to the cursor position:
if (mouseInside2D && currentFacet == null) {
Facet facet = controller.getClosestFacet();
if (facet != null) {
// Paint circles around the node coordinates:
g2.setPaint(Color.WHITE);
for (int i = 0; i < facet.size(); i++) {
Node node = facet.getNode(i);
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p == null) {
continue;
}
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
// for i
}
}
// Paint a white circle around the current node:
Node node = controller.getCurrentNode();
if (node != null) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p != null) {
g2.setPaint(Color.WHITE);
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// Paint a white circle around the node closest to the cursor position:
node = controller.getClosestNode();
if (mouseInside2D && node != null) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p != null) {
g2.setPaint(Color.WHITE);
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// Redefine w for use below:
w = nodeWidth;
// Paint a cursor location indicator at the position of the candidate node:
node = controller.getCandidateNode();
if (node != null) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p != null) {
g2.setPaint(Color.WHITE);
int ix = (int) p.getX();
int iy = (int) p.getY();
g2.drawOval(ix - w / 2, iy - w / 2, w, w);
g2.drawLine(ix - w / 2, iy, ix + w / 2, iy);
g2.drawLine(ix, iy - w / 2, ix, iy + w / 2);
}
}
// Set drawn flag to true:
drawn = true;
}
Aggregations