Rotating coordinates - java

I am working with shapes, that are defined in the Graphiti-Framework. It supports the following:
Rectangle(int x, int y, int width, int height), whereas x/y defines the lower-left point,
Text (which is a rectangle as well),
Ellipse(int x, int y, int width, int height), so the same as the rectangle,
Line (int[] points), so an array with points as parameter
Polygon(int[] points), basically the same as line but the first and last point are connected.
My purpose is to rotate this elements. Unfortunately the framework I am using does not support rotation.
What in you opinion is the best solution to realise that?

Related

Check if a rectangle is near another rectangle

I have a my rectangle.
The application generates another rectangle.
It can be more smaller or larger than my rectangle.
How can I tell when its rect near of the mine using their X, Y, Weight and Hight?? I do not want to know if is into my rectangle.
Draw 1 or more non-visible shapes that are relative to your rectangle's position that fit your definition of "near", then check to see if these shape(s) intersect with the application-generated rectangle in question.
For example, one way you might implement this is drawing a non-visible rectangle that surrounds your rectangle, then checking to see if the surrounding rectangle intersects with the application-generated rectangle.
I found the solution!
I have calculated the middle point of my rectangle.
If the rectangle generated have into the point, is near!
You can use the Math formula to calculate the distance between two points like this:
double getDistance(int x, int y, int x2, int y2) {
double distance;
distance = Math.sqrt( Math.pow( Math.abs(x2 - x) , 2 ) + Math.pow( Math.abs(y2 - y) , 2 ) );
return distance;
}

how to use drawArc()

i want to draw this arc in a panel that the preferedSize is set to (200,50):
i found it hard to understand what does the parameters in drawArc(). how to draw this one?
To understand the arguements read the javadoc: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawArc(int,%20int,%20int,%20int,%20int,%20int)
drawArc(int x, int y, int width, int length, int startAngle, int arcAngle)
Used to draw an arc inside an imaginary rectangle whose upper left corner is at (x,y). The arc is drawn from the startAngle to startAngle + arcAngle and is measured in degrees. A startAngle of 0º points horizontally to the right (like the unit circle in math). Positive is a counterclockwise rotation starting at 0º

Recursion Draws

See Above for description
However my code is adding circles to the array in incorrect colours:
I have a Color baseColor, which contains a variable int baseGreen. This int is reduced during each recursive call, with the intention of changing the type of green for each set of 3 circles.
If anyone is able to hazard a guess as to why this is happening I would be very grateful. Thanks.
tracking base color is unnecessary as you are passing it into your method.
this is a simple way of make the color progressively darker
public void createCircles(int x, int y, int rad, Color parentColor){
Circle myCircle = new Circle(x, y, rad, parentColor);
...
if(!(rad<1)){
...
Color myColor = parentColor.darker();
createCircles(x - (2*rad), y, rad/3, myColor);
createCircles(x, y, rad/3, myColor);
createCircles(x + (2*rad), y, rad/3, myColor);
}
}

Make oval/rectangle using float/double values

I want to draw a figure using float or double values, to be precise.
I use:
g.drawOval(0, 0, 10, 10);
to draw a circle, but I only can use integer values.
Is there any statement that use float/double values that do the same?
Here is a picture: Problem
The circles have to be centered, and I can't. Any solution?
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class Bulls_EYE extends JPanel
{
int red, green, blue;
int height, width;
int heightOval = 475, widthOval = 475;
Random rValue = new Random();
public void paint (Graphics g)
{
super.paint(g);
for (int idx = 0; idx < 100; idx++)
{
g.setColor(new Color(red = 1 + rValue.nextInt(255), green = 1 + rValue.nextInt(255), blue = 1 + rValue.nextInt(255)));
g.fillOval(width+2*idx, height+2*idx, widthOval-5*idx, heightOval-5*idx);
}
}
}
I think it's an interesting question but needs more context. Drawing primitives are usually expressed in pixel coordinates so fractions of a pixel do not make much sense.
If you want precision like a CAD application note that what is displayed on the screen is only an approximation of the underlying model due to the limitations of the display.
You can represent your models precisely in memory (with limitations in floating point representation) and draw the approximation on the screen.
Update
Based on your last update:
We know from the JavaDoc that fillOval takes as parameters (x, y, w, h) where x, y are the upper left coordinates, and w, h are the width and height.
If for each concentric circle you move the upper left coordinates inward, in this case by 2 px, to keep them centered, you must also reduce the width and height by twice that amount. Change the following line:
g.fillOval(width+2*idx, height+2*idx, widthOval-5*idx, heightOval-5*idx);
To
int dx, dy, dw, dh;
dx = 2*idx;
dy = 2*idx;
dw = 2*dx; // note this is 4*idx not 5*idx like you have currently
dh = 2*dy;
g.fillOval(width+dx, height+dy, widthOval-dw, heightOval-dh);
Note that your width and height variables being used in the first and second parameters really doesn't have anything to do with width and height but instead are providing a beginning offset from the origin where the oval is drawn.
There is no reason you should do this, because when drawing an oval with the given coordinates, they are referred to pixels on the screen. Since you can't draw between pixels, 1 is the smallest unit you can use. If you want to round the values before drawing, you can use
g.drawOval(Math.round(a),Math.round(b),Math.round(x),Math.round(y)
which will round the float a, b, x and y before drawing the oval. The only reason I can see is that you calculate the coordinates and the result is a float, then you need to round it like above.
You can use the Arc2D class for drawing circles with float/double precision, since it is a Shape and the Graphics2D class can draw shapes.
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Shape circle = new Arc2D.Double(
// Same values as used in the fillOval function,
// but with double precision.
x, y, width, height,
// Draw a full circle (yes, in degrees).
0, 360,
// Connect the endpoint with the startpoint.
Arc2D.CORD
);
// Paint the circle.
g2d.fill(circle);
}
In a similar way, you can draw rectangles by using the Rectangle2D class.
Also, please use the paintComponent function instead of the paint function, as explained here.

how to draw the arc for three point angle in java

In java I have three points denoting two line with making an angle. Now I have to create the angle arc about 10 pixel apart from common point. with showing angle on the arc. I am able to calculate the angle but how to draw the arc and to show the angle on the arc. Please tell me some code view or link where I can find solution for this. CODE snippet is as below.
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
Point p1=new Point(100,100);
Point p2=new Point(200,100);
Point p3=new Point(100,0);
Line2D line1=new Line2D.Double(p1, p2);
Line2D line2=new Line2D.Double(p1, p3);
g2.draw(line1);
g2.draw(line2);
double angle=getAngle(line1,line2);
System.out.println(angle);
//g2.drawArc(110, 100, 20, 20, 100, 30);
}
public double getAngle(Line2D line1,Line2D line2){
double angle1=Math.atan2(line1.getY1()-line1.getY2(), line1.getX1()-line1.getX2());
double angle2=Math.atan2(line2.getY1()-line2.getY2(), line2.getX1()-line2.getX2());
return Math.toDegrees(angle1-angle2);
}
I don't know how to use DrawArc to draw exact arc which i want and also to put the angle on that.
Thanks & Regards.
From the documentation drawArc's arguments are:
int x, int y, int width, int height, int startAngle, int arcAngle
the x and y are your common point (p1), and your width and height are probably both 10 (to draw a circular arc with radius 10pixels)
The angle that you have computed is the last argument (arcAngle) which measure the sweep of the arc in a counter-clockwise direction. So that last part you need to work out is the start angle, which is probably your angle1 or angle2 (0 in this case is the positive x-axis or 3 o'clock position).
Keep in mind, as written you will sometimes be drawing an arc of > 180 degrees, you will need more logic if you want to always find the smallest angle between the two lines.
As for the text you can use drawString and figure out the x and y using some trigonometry with half your sweep angle and your desired radius. Though for optimal placement you might need to figure out what quadrant you are drawing in and adjust from there.

Categories

Resources