How to make axes equal size in regards to pixels? - java

I'm using JChart2D for visualization. I would like to make the X and Y axis equal in terms of pixels. By doing so its much easier to see how far the points are from each other in euclidean distance (see image). As it is now, the x-axis is more stretched out. They already have the same range in terms of values.
The framesize defines the pixel values right now, i.e:
jFrame.setSize(800,600);. Everything then dynamically fits inside the frame where the Chart2D resize itself to make space. I find the pixels manually, but that seems stupid. Also, if i have a scenario with a lot of clusters, the legends line will wrap and thus resize the chart.
I've googled a lot and looked at the documentation, but I've been unable to find a solution.
So how do i set the chart to a fixed size, that's independent from the other items in the frame?
Image example:

What do you expect if you would make it and still insist on the JFrame to be non-rectanguar to be displayed in the remaining 200 px in x range? The void grey?
You could achieve this e.g. by setting a GridBagLayout to the JFrame.getContentPane() and define a grid of two vertical cells - one grid to take 600px x 600px and the second to take 200px x 600px. But then you have the void in the 2nd pane. HTH, Achim

By doing so its much easier to see how far the points are from each
other in euclidean distance
not really, the Euclidean Distance requires at the end the position of the 2 points you are calculating and you have it... otherwise you wouldnt be able to plot anything in the canvas..
another aproach that you can use is implement a ViewPort over the canvas but that makes sense only for zoom in/out images and tht is not your case...

Related

Draw curved custom object in LIBGDX?

I've recently been looking into LibGDX and seem to have hit a wall, seen in the picture, the blue dot represents the users finger, the map generation it self is where i seem to get stuck, does LibGDX provide a method of dynamically drawing curved objects? I could simply generate them myself as images but then the image is hugely stretched to the point of the gap for the finger can fit 3! But also would need to be 1000's of PX tall to accommodate the whole level design.
Is it such that i should be drawing hundreds of polygons close together to make a curved line?
On a side not i'll need a way of determining when the object has from bottom to top so i can generate another 'chunk' of map.
You don't need hundreds of polygons to make a curve like you drew. You could get away with 40 quads on the left, and 40 on the right, and it would look pretty smooth. Raise that to 100 on each side and it will look almost perfectly smooth, and no modern device is going to have any trouble running that at 60fps.
You could use the Mesh class to generate a procedural mesh for each side. You can make the mesh stay in one spot, locked to the camera, and modify it's vertices and UVs to make it look like you are panning down an infinitely long corridor. This will take a fair amount of math up front but should be smooth sailing once you have that down.
Basically, your level design could be based on some kind of equation that takes Y offset as an input. Or it could be a long array of offsets, and you could use a spline equation or linear equation to interpolate between them. The output would be the UV and X coordinates which can be used to update each of the vertices of your two meshes.
You can use the vertex shader to efficiently update the UV coordinates, using a constant offset uniform parameter that you update each frame. That way you don't have to move UV data to the GPU every frame.
For the vertex positions, use your Mesh's underlying float[] and call setVertices() each frame to update it. Info here.
Actually, it might look better if you leave the UV's and the X positions alone, and just scroll the Y positions up. Keep a couple quads of padding off top and bottom of screen, and just move the top quad to the bottom after it scrolls off screen.
How about creating a set of curved forms that can be put together variably. Like the gap in the middle will at the top and bottom of each image be in the middle (with the same curvature at end and beginning points)...
And inbetween the start and end points you can go crazy on the shape.
And finally, you can randomly put those images together and get an endless world.
If you don't want to stop in the middle each time, you could also have like three entry and exit points (left, middle, right)... and after an image that ends left, you of course need to add an image that starts left, but might end somewhere else...

Scale factor of a function plotter

I have done my own function plotter with java which works quite well.
All you have to do is to iterate over the with (pixels) of the panel and calculate the y-value. Then plot it with a poly-line onto the screen and that's it.
But here comes my problem: There is a scale factor between the number of pixels and the value which I want to plot.
For example I'm at the 304' iteration (iterating over the with value of the plot panel). Now I calculate the corresponding value for this pixel position (304) by the rule of three. This gives me 1.45436. Then I calculate the sin based on this value. Which is transcendetal number. Then I use again the rule of tree to determine which y-pixel this value corresponds to. Doing so, I have to round because the pixel is an integer. And there is my data loss. This data loss may give me the following result:
This looks not really nice. If I play around with resizing the window I sometimes get a smooth result.
How can I fix this problem? I've actually never seen such plots in any other function plotter.
If you do this in Java, you might consider composing your data points to a Path2D. That would have floating point coordinates, and the drawing engine would take care of smoothing things down. You might have to disable stroke control, though.

How to change the position of a view?

I have two views called x and y they are both black lines (for example I made the height of the x line is 1dp and width 230dp and as background filled with the color black).
Now i want to move the position of the lines programmatically (for example I want the y line 50dp to the right of the orginal position).
Can someone help me how to do this?
I have tried things such as setpadding but the line doesn't move.
Thanks in advance!
(ps: my minimum sdk is set for 7 so i can't use the newest api's).
Old Answer
Have a look at the Absolute Layout, it allows you to position
child elements using x, y coordinates. It is deprecated but it's the
only way in Android to do real x,y coordinate positioning.
I would ask what the main point behind what you are trying to do is
though? It sounds like you started with a goal, were led down a path
and now are asking how to get to the end of that path, rather than
asking how to do what you need to do.
Edited
For drawing graphs have a look instead at https://stackoverflow.com/questions/2271248/how-to-draw-charts-in-android.
Using a Layout class to draw a chart will only lead to a really slow app, since the layout classes are designed for creating relatively static layouts, not drawing full graphics.
Instead use either the Canvas and draw your drawables yourself or use the graphing packages listed in the SO question I linked above.

Java: drawing nonoverlapping shapes randomly within area

I need to draw some graphics objects on a JPanel in a JApplet, they are Graphics2D objects.
Here is some visual help
1) I would like to draw circles near the boundaries of the panel at random coordinates so that every time the applet is started it looks different.
I would like in some way to say that exclude this part, don't draw there - blue circle in image.
2) Would be nice if I could evenly distribute them so that if there are 4 circles, then the distance between the circles is almost the same, but still random to some amount.
3) If the first 3 or 4 circles are drawn then draw the other smaller and random sized circles are drawn all over the panel that isn't used.
It seems like a strange idea but would it be possible to make an algorithm that gives you the centre coordinates based on some constraints like
(x-100)^2 + (y-100)^2 >200 && 0< x <400 && 0< y < 400
Now I do it like this:
divide the whole area into a grid where every slot in the gird is free or taken and inside that slot draw the circles at random coordinates, but still within that slot.
That idea doesn't look so good and I would like to do it better.
I'm implementing this in java but general ideas are also welcome.
If you have only circles, the "not overlapping" test is easy: Two circles are not overlapping if and only if the distance of there centers is bigger than the sum of their radiuses (for equality they are touching). For overlapping the border: the distance from the border must be bigger than the radius.
So, you could simply go on and generate random coordinates (either with fixed or random radiuses), then check if any are overlapping. If yes, either start again, or throw away only the overlapping ones and go on.
If you have not too many circles, this simple algorithm is enough. If it starts taking too long, you might think about using some spatial data structure, so you will only have to check the circles near to your new point.
You may want to create the list of circles to paint not inside of the paintComponent method, but instead in the init() or start() method (or some thread called from there), so the painting will not take too long, and the circles will not change for every (maybe even partial) repaint.

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.

Categories

Resources