How to paint edges with different angle? - java

I'm using a CircleLayout for my graph using Jung2. I overrode the initiate() method so that the vertices are drawn on a certain position in the circle depending on its id. This means that vertices are spread irregular on the circle.
Now I have a problem: because of how the edges are painted, the graph doesn't look like a circle anymore.
is there a way to make the edges look like a circle again?

You need to supply a different (custom) edge renderer; see the code in jung.visualization.renderers for guidance. You supply it to the visualization system as follows:
VisualizationServer.getRenderer().setEdgeRenderer(yourCustomEdgeRenderer);
Alternatively, if you really just want it to look like a circle, you can do this:
(0) Draw a circle using a pre-render Paintable. (Demos show how this works.)
(1) Supply an edge rendering predicate that always returns false, i.e., ensure that none of the edges are rendered.
That will be much easier and simpler than drawing the appropriate arc of a circle in between each pair of connected vertices.

Related

How to draw the area around a shape

I worked on a very simple map editor phase for a game in java. The goal is to put some islands with different shape on the map. But there is some constraints:
islands must not be a specific distance far from another island (lets call it L)
islands must not be a specific distance close from another island (lets call it S)
In the game, the island is place with the mouse. The gamer can see areas where the island can be place or not as you can see.
My problem is that I realize my disalow area is not good. For example, the rectangle island have a rectangle disallow area (my first naive attempt) but in fact I must draw area of S around the rectangle ; that leads to a shape like this:
I'm able to draw these kind of areas as long as my shapes are just composed of lines. But my island can have cubic or quadratic curve (and even though i'll need this kind of area for other shapes later).
The closer I manage to do is that:
In this case, the disallow area around the circle must be ... a circle (simple geometry). But as you can see, I have a weird rounded rectangle.
I currently try to transform each segment of the pathiterator of a Shape to get the area. It's not as simple as scaling a shape (remember the rectangle case). I've allready try many ways to transform the shape and get the area.
Question:
Does someone have information, formula, clues, algorithms, libs to get this area from any java.awt.Shape (or PathIterator) and a distance?
http://www.java2s.com/example/java/java.lang/expand-or-shrink-a-shape-in-all-directions-by-a-defined-offset.html
This site describe how to use stroke to get the offset area.
There is just a single modification to solve my problem ; I have to use BasicStroke.JOIN_ROUND to get the good rectangular Shape.
Then I get:

How to draw a rotated rectangle

Can anyone share a code how to draw a rectangle like this?
(not vertical, not horizontal, somewhere between)
Because as I see you can only specify 4 ints to function DrawRect(), not 4 points.
The area of rectangle must be sensitive to touch (I use Contains() method)
As I tried, Matrix and Rotate() update only graphics, the rect's area remains the same it was
After transforming as #fortran suggested, you can use one of the Matrix.mapPoints overloads to find out what the new corners of your rectangle are. You'll probably have to find some fancy math and do hit testing yourself. Might be easier to call Matrix.mapPoints() on the inverse of the transformation used to draw the rectangle, passing the touched coordinates, and then hit test on the original rectangle.
Push a rotation transformation, draw the rectangle, pop it.
http://developer.android.com/reference/android/graphics/Canvas.html#rotate%28float,%20float,%20float%29
I found solution - the easiest way is to use Path class, make a free-turned rectangle by points and then create a region method, which has function Contains()
No need for math and hard work, pretty and easy.

Graph Editor (Java) tips on how to draw edges between vertices

For a school assignment we have to make a graph editor like the next one (given example):
Most of the assignment goes well, we're stuck on one point: drawing the edges between vertices. In the given example, the lines are nicely drawn from points relatively close to the connecting vertex. For example the top one has two lines on its bottom edge, one more to the left and one more to the right.
We are not sure on how to let the drawing of the lines keep the positions of the connecting vertex in account and how to adjust the positions of the beginning and end points on that (what calculations to make). Can someone help out?
In fact, that's rather simple: Draw the lines first, from the position of each vertex to the position of the respective other vertex (the "position" here refers to the center of the rectangle). Afterwards, draw the rectangles for the vertices. They will be painted over the edges, and the result will look exactly like the screenshot.
If you extend the connector lines, you can see they are drawn from the geometric center of the graph node rectangles:
Since you already seem to have a good positioning algorithm for your nodes, all you need to do is draw the connector lines first. Then draw the node rectangles on top of them.

Java - Drawing a dynamic shape outline

I wish to create a dynamic outline to a Shape (AWT) by adding objects (with draw functions) to appropriate positions on screen along the shape's perimeter. I want a roughly even distance between each object. (An alternative approach to the same affect will be fine.)
How might I acquire the locations for these objects? I know shapes have a path iterator, but I have no idea how to use it.
You might look at a library such as the one described in A Shape Diagram Editor.
If you want to experiment, GraphPanel is a simple object drawing program that features moveable, resizable, colored nodes connected by edges. If the nodes were a little smaller, they'd be moveable points on a Shape that can be iterated as shown here for Polygon.
Addendum: I want a roughly even distance between each object.
The class Node exposes a number of static methods that operate on a List<Node> such as selected. Existing implementations serve, for example, to translate or resize multiple selections as a unit. Functions for Align and Distribute could be implemented similarly. I'd look at LayoutManger as an example for the latter.
Use FlatteningPathIterator to get points for Shape's path.
Also you can use BasicStroke's method
public Shape createStrokedShape(Shape s)
to get Shape's outline with desire width.

Vertices selection and state of model after rotation

I'm currently writing an application that actually acts as a "cut" tool for 3D meshes. Well, I had some problems with it now which I am clueless on how to solve, since it is my first application.
I have loaded a model from an object file onto the canvas, then on the same canvas, I use the mouse drag event to draw lines to define the cutting point.
Let us say I want to cut a ball into half and I draw the line in the middle. How do I detect the vertices of the ball under the line.
Secondly, if I rotate/translate the ball, would all the the vertices information change?
Think of what you'd do in the real world: You can't cut a ball with a line, you must use a knife (a line has no volume). To cut the ball, you must move the knife through the ball.
So what you're looking after is a plane, not a line. To get such a plane, you must use some 3D math. What you have is the canvas orientation and the "side view" of the plane (which looks like a line).
So the plane you're looking for is perpendicular to the canvas. A simple way to get such a plane is to take the canvas orientation and create a plane which has the same orientation and then rotate the plane around the line by 90°.
After that, you can visit all edges of your model and determine on which side of the plane they are. For this, determine on which side of the plane the end points of the edge are. Use the cross product. If they are on the same side (both results of the cross products will have the same sign), you can ignore the edge. Otherwise, you need to determine the intersection point of the edge and plane. Create new edges and connect them accordingly.
See this page for some background on the math. But you should find some helper methods for all this in your opengl library.
if I rotate / translate the ball, would all the the vertices information change
Of course.
It's not going to be that easy.
I assume the line you are drawing induces a plane which then cuts the sphere.
To do so, you have to calculate the intersecting area of the sphere and the plane.
This is not a trivial task and I suggest using an existing framework for this or if you really want to do this yourself, read about basic intersection problems to get a feeling for this kind of problem. This paper offers a good introduction to various intersection tests.
In general boundary represended volumes, as in your case, are difficult to handle when it comes to more advanced manipulations. Cutting a sphere in half is easy compared to burring a small hole into it. Sometimes it's better to use a volume representation, like tetrahedral meshes or CSG.
Regarding your second question, you shouldn't rotate or translate the sphere, rotate and translate the camera.

Categories

Resources