JavaFX - Maintain Shape Borders after using Shape.union() - java

Is there a way to maintain the borders/strokes on a JavaFX Shape after using the union function? For example here is my code:
Shape rect = new Rectangle(150, 150);
rect.setFill(Color.WHITE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(4);
Line line = new Line(0, 40, 150, 40);
line.setStrokeWidth(2);
Shape combined = Shape.union(line, rect);
combined.setFill(Color.WHITE);
combined.setStroke(Color.BLACK);
pane.getChildren().add(combined);
Expected Output:
Actual Output:
Is there anyway that I can union the two together so that I can drag and drop them together?

your problem is combined.setFill(Color.WHITE); ,because it clear all previous shape changes .
Try something like this
Line line = new Line(0, 40, 150, 40);
Shape rect = new Rectangle(150, 150);
Shape combined = Shape.subtract(rect,line);
combined.setFill(Color.WHITE);
combined.setStroke(Color.BLACK);
rect.setFill(Color.WHITE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(4);
line.setStrokeWidth(2);
line.setStroke(Color.BLACK);
line.setFill(Color.BLACK);
pane.getChildren().add(combined);
out put will like be this
For more info about shape.union,subtract,intersect go here

Related

Java - Only Two Rounded Corners on a JFrame

I would like to round the top two corners on a JFrame for a project I am currently working on. I am currently rounding all four corners using setShape(new RoundRectangle2D.Double(0, 0, 200, 252, 30, 30)); but I do not want the bottom two rounded I want the to be a normal corner.
you can combine shapes to get this.By combining roundered rectangle with a normal rectangle you can make a rectangle without bottom two rounded corners.
for example
public class example extends JFrame{
public example() {
this.setUndecorated(true);
this.getContentPane().setBackground(Color.red);
Area shape1 = new Area(new RoundRectangle2D.Double(0, 0, 200, 252, 30, 30));
Area shape2 = new Area(new Rectangle(0, 252-30, 200, 100));
shape1.add(shape2);
this.setShape(shape1);
this.setSize(300, 400);
}
public static void main(String[] args) {
new example().setVisible(true);
}
}
alternatively you can give smaller height to the frame than RoundRectangle rectangle .so you can't see bottom of the RoundRectangle .and then you can get desired output

Java iText Rotate a Link Rectangle

I need to rotate a link rectangle using Java iText.
The original link rectangle appears in red. The rotated link rectangle appears in green.
My code:
PdfReader reader = new PdfReader( "input/blank.pdf" );
PdfStamper stamper = new PdfStamper( reader, new FileOutputStream( "output/blank_stamped.pdf" ) );
Rectangle linkLocation = new Rectangle( 100, 700, 100 + 200, 700 + 25 );
PdfName highlight = PdfAnnotation.HIGHLIGHT_INVERT;
PdfAnnotation linkRed = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "red" );
PdfAnnotation linkGreen = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "green" );
BaseColor baseColorRed = new BaseColor(255,0,0);
BaseColor baseColorGreen = new BaseColor(0,255,0);
linkRed.setColor(baseColorRed);
linkGreen.setColor(baseColorGreen);
double angleDegrees = 10;
double angleRadians = Math.PI*angleDegrees/180;
stamper.addAnnotation(linkRed, 1);
linkGreen.applyCTM(AffineTransform.getRotateInstance(angleRadians));
stamper.addAnnotation(linkGreen, 1);
stamper.close();
But this code does not rotate the recangle.
Please take a look at the following screen shot:
I have added 5 annotations to a simple Hello World file.
The first two are link annotations. Their position is defined by the rectangles linkLocation1 and linkLocation2:
Rectangle linkLocation1 = new Rectangle(30, 770, 120, 800);
PdfAnnotation link1 = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation1, PdfAnnotation.HIGHLIGHT_INVERT, action);
link1.setColor(BaseColor.RED);
stamper.addAnnotation(link1, 1);
Rectangle linkLocation2 = new Rectangle(30, 670, 60, 760);
PdfAnnotation link2 = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation2, PdfAnnotation.HIGHLIGHT_INVERT, action);
link2.setColor(BaseColor.GREEN);
stamper.addAnnotation(link2, 1);
The green rectangle looks like a rotated version of the red rectangle, but that's not really true: we just defined the "clickable" area that way. I don't understand why you'd want to get this effect by introducing a rotation. Why? Because a rotation always needs a rotating point. Suppose that you would introduce a rotation, what would be your rotation point? The (0, 0) coordinate? That would lead to strange results, wouldn't it?
Introducing a rotation for does make sense for some types of annotations though. In my example, I introduced three stamp annotations:
Rectangle linkLocation3 = new Rectangle(150, 770, 240, 800);
PdfAnnotation stamp1 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation3, "Landscape", "Confidential");
stamper.addAnnotation(stamp1, 1);
Rectangle linkLocation4 = new Rectangle(150, 670, 240, 760);
PdfAnnotation stamp2 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation4, "Portrait", "Confidential");
stamp2.setRotate(90);
stamper.addAnnotation(stamp2, 1);
Rectangle linkLocation5 = new Rectangle(250, 670, 340, 760);
PdfAnnotation stamp3 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation5, "Portrait", "Confidential");
stamp3.setRotate(45);
stamper.addAnnotation(stamp3, 1);
In this case, I introduce a rotation angle using the setRotate() method. This rotates the CONFIDENTIAL stamp inside the rectangle we defined. As you can see, this makes sense because the annotation does have actual content: the rotation has an impact on the way you read the word CONFIDENTIAL. In the case of the clickable area of the link annotation, there is no such content to be rotated.
If this doesn't answer your question, please rephrase your question because I don't think anyone can answer it in its current state.
Update
Please take a look at ISO-32000-1 aka the PDF specification. You'll discover that a rectangle is defined using 4 values: the x and y coordinate of the lower-left corner of the rectangle and the x and y coordinate of the upper-right corner of the rectangle. These are the two starting points of the horizontal and vertical sides. You want a rectangle that has sides that aren't horizontal/vertical. Obviously that isn't possible as you'd need the coordinates of 4 corner points to achieve that (8 values, not 4). You can achieve this using a polygon defined by QuadPoints.
See ITextShape Clickable Polygon or path

Android Touch Bounding Rectangle and Texture Location

Just using the coord system my rectangle for my touch bounding box never seems to align with the coordinated and placement of my textures.
What is the simplest way to draw bounding box rectangles so that I can line them up with my drawn textures?
Im using OPEN-GLES
EG..
playBounds = new Rectangle( 240, 400, 157, 177);
batcher.drawSprite(240, 400, 157, 177, Assets.mainMenu);
I found out that because the bounding rectangles are based on the lower left corner and texture coords are based on the center, the below seems to be the best solution.
playBounds = new Rectangle(240, 400, 157, 177);
batcher.drawSprite(
playBounds.lowerLeft.x + playBounds.width / 2,
playBounds.lowerLeft.y + playBounds.height / 2,
playBounds.width,
playBounds.height,
Assets.mainMenu
);

How do I annotate my graph using JUNG's annotation system?

I cannot seem to find any documentation on annotating my JUNG graph. The most I've cobbled together is:
AnnotationRenderer renderer = new AnnotationRenderer();
AnnotationPaintable annotate = new AnnotationPaintable(viewer.getRenderContext(), renderer);
Shape square = new Rectangle(0, 0, 50, 50);
Annotation<Shape> a = new Annotation<Shape>(square, Annotation.Layer.UPPER, null, true,
new Point2D.Double(0, 0));
annotate.add(a);
How do I take this and start drawing the actual annotations within the AnnotationPaintable object?
The answer is to add it to your visualization viewer:
viewer.addPostRenderPaintable(...)

Order of rectangles to display and can they be changed?

I working on a project in which I have to simulate a memory manager and show some memory snapshots. I have created a draw class via examples I have found here in which I override the paintComponet(). Everything draws fine.
I would like to be able to draw a rectangle to represent a memory partition and then overlay another rectangle over top to represent an incoming job (ie Job1 is in this partition3). What seems to occur is that I add the partition first (which will always be the case) and then when I add the job it will sit behind the partition block. Is there a way other than drawing the Job first to shift these after the job is created?
Here is the paint override
#Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// set up rendering to allow anti-aliasing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// create the rectangle to represent the memory partition block
// x = address position h = amount of memory (y & w are predefined for the display block)
Rectangle2D rect = new Rectangle2D.Double(x, y, w, h); // create the rectangle
g2d.setPaint(partColor); // set it's color
g2d.fill(rect); // fill it in
// create the transparency for the text
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
g2d.setComposite(comp);
// draw the text with color, type and size and center the text in the block created above
g2d.setPaint(Color.black);
g2d.setFont(new Font("Tahoma", Font.PLAIN, 12));
g2d.drawString(text, (int)((w / 2) - (text.length()/2)), (int)h/2);
}
The call to draw is in my window class (this will place the partition in front of the job) but I need to order to be reversed without changing the order of the calls.
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
snapPanel.add(part1);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
Is there some reason you can't do this?
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
snapPanel.add(part1);
A more general answer would be to add a z component to each of your rectangles. Then you can loop through your rectangles in the paintComponent method, drawing them in z order.

Categories

Resources