Using doubles in Java Graphics Utility? - java

I was wondering if there is a way to plot a point using a double value in the built in java graphics utility. I am making a simple clock but I want it to be as precise as possible. The method drawLine(int, int, int, int) in Graphics obviously won't take a double as a parameter. Is there a work-around to this?
P.S. The doubles in question are the change in x and y for each hand on the clock as 1 second passes.

Graphics2D has an internal AffineTransform Matrix that you can alter. I have not tested the following code, but i think this or a variant might get close to what you want (Although really, the pixels are all at integer positions...)
Graphics2D g; // get a Graphics2D from somewhere
g.rotate(Math.PI/4);
g.drawLine(0,0,1,0); //draw a line at 45°
// now you should probably rotate back...

Pixels only exist at integer positions. think of it like a chess board, each square is only one colour and it means nothing to say half a square.
There are some things you can do (bi linear interpolation for example) to draw in between pixels but all they do is modify all the pixels around the point according to a suitable algorithm and the position you select.
If you want higher resolution then the simplest thing is just to increase the resolution of the image you are using. More pixels gives more possible precision, until you reach the limits of your display device...

Related

Why does LWJGL have a function that uses floats as coordinates?

I was fixing a problem in my code that I knew had something to do with this: GL11.glTexCoord2f(x,y);I changed it of course, but I wondered why it used floats as coordinates. I'm sure there is a reason, but it just seems extremely stupid to me. There is probably something I'm missing :P but whatever.
This is not specific to Java or LWJGL. As you might know, LWJGL is largely a 1:1 mapping of the original OpenGL API. And the different flavors of the glTexCoord function have been in OpenGL since version 1.0: https://www.opengl.org/sdk/docs/man2/xhtml/glTexCoord.xml
The reason of why texture coordinates are usually given as floating point values is that they refer to an "abstract" image (the texture). The size of this texture is not known. For example, you may have a mesh, and as its texture, you may use a JPG image that is 200x200 pixels large, or or an image that is 800x800 pixels large.
In order to allow arbitrary images to be used as the texture, the texture coordinates are almost always given as values between (0.0, 0.0) and (1.0, 1.0). They refer to the the total size of the image, regardless of which size this is.
Imagine a vertex with texture coordinates (0.5, 0.5). If the texture is 200x200 pixels large, then the actual "pixel" that will be used as this vertex is the pixel at (100,100). If the texture is 800x800 pixels large, then the pixel that will be used at this vertex will be the pixel at (400,400).
Using this sort of "normalized" coordinates (in the range [0.0,1.0]) is very common in graphics and other applications.
What else should it be using? The Double dataType may not be supported on all GPU's although there is some support comming up. And since a texture uv-coordinate should be between 0.0 and 1.0 (as a precentage of the height/width) I cant think of any other logical dataType that could be used here.
So is there any specific problem with using floats? And if so, please edit your post and provide more information, as for now we cannot help you any further.

What is the measure unit of Graphics2D or the 2D Components?

I'm working on a code that should allow me to draw an AC "Animated electricity" signal, and the time interval should be accurate in this case.
My question is; What is the measuring unit of the Point2D specifically, Is't pixels, milliseconds, or any unit else?
I'm trying to draw a Line2D, which should be drawn between two points!
I'd tried to take the '1' value as milliseconds and it's kind of worked, later on I discovered that the graphics elements is measured by pixels, I tried to convert millisecond to pixels with multiplying by pixel value, but it didn't give me the expected results.
My code as following expecting '1' value is Millie
<i>
for(i=0;i<20000;i++)
{
//System.out.println(volts[i]);
if(i!=0)
g2d.draw(new Line2D.Double(time-(timeScale/y),-(((voltScale/x)*50*volts[i-1])-300),time,-(((voltScale/x)*50*volts[i])-300)));
time+=(timeScale/y);
}
</i>
The Graphics2D class description gives a fairly good description of the units (in the "Coordinate Spaces" section:
https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html
Generally speaking, a unit in "User Space" (the Java2D coordinate system) will correspond to 1/72 inch on physical devices (such as your monitor or your printer).
Well, sense no one answered, I think in the Graphics2D object case, It won't take it as pixels, maybe the pixels can't be cut into fractions, and Graphics2D is able to use fractions. Unfortunately seems like that the developers whose worked on the Graphics2D class doesn't give any clear description about it.

Slow Moving Objects (Visual)

When I try to move graphical objects across the screen at steps which are not whole numbers (for example, 0.5 pixels per frame) this results in choppy and 'laggy' movement; as the object will simply move 1 pixel every two frames.
I understand why this is happening as the x / y values of an object must be Integers, but I wonder if there is anyway to create smooth slow movement, such as there is in Adobe Flash.
Graphics2D allows you to draw at sub-pixel accuracy and as long as your actual object is drawn using its primitive (as opposed to being a fixed bitmap), this should actually have a visible effect.
Make sure that the KEY_ANTIALIASING rendering hint is set to VALUE_ANTIALIASING_ON

Java Applet Graphics Resizing

So I've got an assignment that takes two inputs, males and females, and outputs matingPairs, the product of the two.
In addition to that, the instructions ask to draw a shape using one of those variables.
I've decided to draw circles for each value.
I first draw matingPairs, followed by the smaller male and female circles on top of the original, larger matingPairs circle.
The problem I'm running in to is obviously representing the graphic in the applet. If the numbers go higher than say 100, the graphic becomes too large for the applet.
I'm looking for a way to basically have the matingPairs circle always fill the applet, then have males and females dynamically adjust so their size is scaled relative to the matingPairs circle size. I'm using JApplet.
Thank you very much for any guidance. I'm really looking for a solution, rather a push in the right direction.
May be you should provide more instruction about how are you drawing the circles in the Graphics object.
The idea is to manage two bi-dimensional spaces with different scales; the first one is the input data and the second one represents the available area to draw such data. The first one can have data on any location, such (5, 5), (0.2, 0.3)or (1200, 3400). The key is to map the original coordinates of the first space into the second, using the proper transformation: scale + translation.
This transformation must be calculated prior to start drawing and applies to any point drawn.
The idea is to map the rectangle where input data resides to the available area in the graphics. If the graphics area is 200x200 pixels and the data could be from (0, 0) to (400, 400), just divide by 2 the coordinates of the points to draw. If the original data is not centered in (0, 0), use a translation.
So, do you need to know how to get the size of the applets canvas or how to scale the male/female circles accordingly?
Edit:
Drawing a circle to fill the 600x600 area should be easy. Just keep in mind that you often specify the top left corner of the circle and the width and height (i.e. the diameter) when calling drawOval() / fillOval() or similar methods.
The next question is: what does represent the size of the input (males/females) and output (pairs), the area or the radius of the circles? Whatever it is, it should be easy to calculate the input/output ratio and then multiply the fixed size of the output circle with it in order to get the size of the input circle.

Rotation and Scaling -- How to do both and get the right result?

I've got a set of Java2D calls that draw vectors on a graphics context. I'd like for the image to be doubled in size and then rotated 90 degrees.
I'm using the following code to do this:
Graphics2D g2 = // ... get graphics 2d somehow ...
AffineTransform oldTransform = g2.getTransform();
AffineTransform newTransform = (AffineTransform)oldTransform.clone();
newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
// ... do my drawing ...
This rotates and scales, however, the scale isn't applied the way I would like. It is as if it is rotated before scaling, thus making the image wider on the wrong axis.
Is there a better way to do this?
I believe those transforms are implemented like a stack - so the last transform is performed first. Try reversing the order of the rotate and scale transformations and you should get what you are looking for.
newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
Rotations are always performed about the origin. In order to rotate about a certain point you must translate the points.
This page explains the maths behind what you're trying to do and show why transformations need to be applied in a certain order.
Change the order in which you concatenate the transforms to control the order in which they are applied in the composite.

Categories

Resources