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
);
Related
I would like to draw a ring pie chart with its shading which color is start from BaseColor.WHITE to BaseColor.GREEN.
I used the pdfShading function. It fails when the X and Y axis' coordinate value are the same.
The pie chart isn't generated.
PdfShadingPattern pattern;
PdfShading axial;
axial = PdfShading.simpleAxial(canvas.getPdfWriter(), 0, 76, 0, 76,BaseColor.WHITE, BaseColor.GREEN);
pattern = new PdfShadingPattern(axial);
canvas.setShadingFill(pattern);
canvas.setShadingStroke(pattern);
canvas.fill();
To draw a circle you need to use simpleRadial instead of simpleAxial. Like:
axial = PdfShading.simpleRadial(canvas.getPdfWriter(), 0, 76, 5, 0, 76, 5,BaseColor.WHITE, BaseColor.GREEN);
So I have this program to test the possibility of an object to slide down in a ramp given its friction, object mass and ramp angle. However I need to animate the box if the force is positive. Just a simple animation moving the box from that point to the end of the ramp. But I can't. Please help
private void drawTransform(Graphics g, double modifier) {
// redtowhite = new GradientPaint(0,0,color.RED,100, 0,color.WHITE);
Rectangle rect = new Rectangle(130,350, 350, 15);
Rectangle box = new Rectangle((int) (rect.getX()+300), 300, 50, 50);
AffineTransform at = new AffineTransform();
at.rotate(-Math.toRadians(modifier), rect.getX(), rect.getY() + rect.height);
// Transform the shape and draw it to screen
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.DARK_GRAY);
// g2d.fillRect(0, 0, 350, 600);
g2d.fill(at.createTransformedShape(rect));
g2d.draw(at.createTransformedShape(box));
}
Screenshot:
If all you want to do is move the box, this can be done by simply updating it's X position. You should be able to manipulate the rectangle's X position directly using something like "box.x++". Alternatively you could create a variable and reference that to provide the initial X co-ordinate, then updating that variable will "move" the box. One issue is this will only move the box along the X axis, hence you will also need some kind of constant downward force acting as gravity. This is easy to achieve, just minus the box's Y position value when it is not colliding with the ground, or your ramp.
Another approach is velocity based movement using vectors, however you mentioned that the animation should be simple. If you do want a smoother animation velocity based movement will provide this but you will need to perform a little research first.
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
I would like to draw some shapes on a JPanel by overriding paintComponent. I would like to be able to pan and zoom. Panning and zooming is easy to do with AffineTransform and the setTransform method on the Graphics2D object. After doing that I can easyli draw the shapes with g2.draw(myShape) The shapes are defined with the "world coordinates" so it works fine when panning and I have to translate them to the canvas/JPanel coordinates before drawing.
Now I would like to change the quadrant of the coordinates. From the 4th quadrant that JPanel and computer often uses to the 1st quadrant that the users are most familiar with. The X is the same but the Y-axe should increase upwards instead of downwards. It is easy to redefine origo by new Point(origo.x, -origo.y);
But How can I draw the shapes in this quadrant? I would like to keep the coordinates of the shapes (defined in the world coordinates) rather than have them in the canvas coordinates. So I need to transform them in some way, or transform the Graphics2D object, and I would like to do it efficiently. Can I do this with AffineTransform too?
My code for drawing:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.blue);
AffineTransform at = g2.getTransform();
at.translate(-origo.x, -origo.y);
at.translate(0, getHeight());
at.scale(1, -1);
g2.setTransform(at);
g2.drawLine(30, 30, 140, 20);
g2.draw(new CubicCurve2D.Double(30, 65, 23, 45, 23, 34, 67, 58));
}
This is an off the cuff answer, so it's untested, but I think it will work.
Translate by (0, height). That should reposition the origin to the lower left.
Scale by (1, -1). That should flip it about the x axis.
I don't think the order of operations matters in this case.
I'm trying to work out how to make SWT widgets (e.g. Label, Frame) be some shape other than rectangular.
I've made a custom shaped main window using the setRegion() method. Now I would like the widgets in the window to follow the same shape. I've tried using the setRegion() method on the widgets themselves (they inherit it) but nothing happens.
How do I make an SWT widget have a custom shape?
I've managed to work it out. It seems my difficulty was arising from a misunderstanding of how to define a custom region. The setBounds() method defines the coordinate system for the custom region. I was assuming the region and setBounds were using the same coordinate system and the result was not displaying the widget at all.
The picture displays the effect. The green label on the left curves like the main window, while the grey frame on the bottom right corner doesn't.
The image appears in the preview but not the posted answer, try:
http://img87.imageshack.us/my.php?image=curvecroppedwf8.png
The code fragments to do this:
Display display = Display.getDefault();
Shell shell new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
// make the region for the main window
// circle is a method that returns a list of points defining a circle
Region region = new Region();
region.add(350, 0, 981, 51);
region.add(circle(380,51,30));
region.add(circle(951,51,30));
region.add(380, 51, 571, 30);
shell.setRegion(region);
Rectangle rsize = region.getBounds();
shell.setSize(rsize.width, rsize.height);
Composite main = new Composite(shell, SWT.NULL);
// make the label
cpyLbl = new Label(main, SWT.NONE);
cpyLbl.setText("Copy");
cpyLbl.setBackground(SWTResourceManager.getColor(38,255,23));
Region cpyRegion = new Region();
cpyRegion.add(0, 0, 161, 51);
cpyRegion.add(circle(28,51,28));
cpyRegion.add(28, 51, 133, 28);
cpyLbl.setRegion(cpyRegion);
// the top left of the bounds is the 0,0 of the region coordinates
// bounds are in screen coordinates (maybe shell coordinates?)
cpyLbl.setBounds(352, 0, 161, 79);
cpyLbl.setVisible(true);