Shape class for border points in java - java

I want to use shape to diagnose the border points in a map, How can i use this class in java for diagnosing these points so that i can put something on these areas or move something on them ? How can i do it ?

FlatteningPathIterator
From comment
* The FlatteningPathIterator class returns a flattened view of
* another {#link PathIterator} object. Other {#link java.awt.Shape Shape}
* classes can use this class to provide flattening behavior for their paths
* without having to perform the interpolation calculations themselves.

Related

Using Enum in java to store a fixed set of mutable objects

I am creating an Android app that draws to the canvas. For this, I have defined rectangular screenareas that I want to draw to the canvas. As I am always drawing a fixed set of screenareas, I was thinking of using Enum (as Enum is designed for fixed sets).
Here is my enum:
public enum LayoutEnum {
FULLSCREEN(
new ScreenArea(
new Rect(
0,
0,
MainActivity.getDevice().getWidth(),
MainActivity.getDevice().getHeight()),
Attributes.BG_PAINT)),
LOGO_AREA(
new ScreenArea (
new Rect(
(int) (0.3 * FULLSCREEN.getScreenArea().getArea().width()),
(int) (0.3 * FULLSCREEN.getScreenArea().getArea().width()),
(int) (FULLSCREEN.getScreenArea().getArea().width() - 0.3 * FULLSCREEN.getScreenArea().getArea().width()),
(int) (0.7 * FULLSCREEN.getScreenArea().getArea().width())),
Attributes.BG_PAINT)
);
private ScreenArea screenArea;
LayoutEnum(ScreenArea screenArea) {
this.screenArea = screenArea;
}
public ScreenArea getScreenArea() {
return screenArea;
}
}
ScreenArea is a simple class that holds a Rect and a Paint and contains a draw method (and some getters and setters).
The question I have, is: is this a good approach?
On one hand I am working with a fixed set of variables. On the other hand, these variables are mutable and I can change their attributes (e.g., using the getters and setters). For example, I can call FULLSCREEN.getScreenArea().getPaint().setColor(Color.BLUE)
When you look at Enum it says it is
a special data type that enables for a variable to be a set of
predefined constant
So I do have a fixed set, it is predefined, but not necessarily constant.
My original approach was to define a class called Layout which contained a HashMap of Screenarea's. In that case, I was using e.g., Layout.get("fullscreen").draw(canvas) to draw the screenarea to the canvas. In this new approach I am using e.g., FULLSCREEN.getScreenArea().draw(canvas).
One of the reasons I would like to switch is to introduce a typesafe solution. Of course, it would also be possible to switch from a HashMap to an EnumMap and store the names of my screenareas in an Enum.
Hope you can point me in the right direction: a direction that not only works (the above is already working) but is also acceptable and doesn't smell.

Can't figure out what flattnes parameter does in PathIterator?

I am writing my own PathIterator implementation.
Currently I implemented method with flatness with flatness-less one:
#Override
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return getPathIterator(at);
}
i.e. I am ignoring it.
What is the simplest way to work with flatness parameter?
My current iterator gives a series of parallel vertical lines (not connected between). How important for me to implement flatness-aware version of the iterator? Where is it used?
The getPathIterator() cited should return only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE segment types. If your Shape has no other segment types, e.g. SEG_CUBICTO or SEG_QUADTO, the flatness is irrelevant. As a concrete example, the Rectangle2D implementation specifies that "Since rectangles are already flat, the flatness parameter is ignored."

How to document my method in Java like Java docs?

I want that when i mouse over a method i would be able to see my documentation of what the method does like when i put the mouse over Java's method I know that /** */ is how its done but:
How do you explain what the Params Stands for?
How do you create a new line, or make a word bold or italic?
In most major IDEs, such as IntelliJ's IDEA, Apache Netbeans or Eclipse; you can type
/**
and press enter and it will generate the Javadoc for your method, including parameters, return values, etc. You just need to put in the descriptions.
The same applies for class declarations (the Javadoc comment always relates to the following element)
For instance
/**
* create_instance
* #param array of attributes for instance containing web, db, arrival_rate, response_time for instance
* respectively.
* #return Instance object
*/
How do you explain what the Params Stands for?
Use #param tag:
/**
* #param paramName Explanation of the param
*/
public void foo(String paramName);
How do you create a new line, or make a word bold or italic?
Use standard HTML, i.e. <p></p>, <br/>, <strong> and <em> (or less semantic <b> and <i>)

Implementing Polygon2D in Java 2D

I'm creating a 2D game in Java using the Java2D library for drawing, and I really need a float-precision Polygon object that I can use both to draw game objects and to do collision detection on them. Unfortunately, Java's Polygon object comes in int precision only, and there is no equivalent Polygon2D like there is with Rectangle and Rectangle2D. I've already done enough research to see that I have a few options, but none of them seem very good.
Use Path2D. According to a Java developer posting in this forum, the lack of Polygon2D was an oversight, but its suggested replacement is Path2D. Unfortunately, Path2D doesn't provide a way to access its individual vertices or edges, which I need in order to do collision detection (specifically I need to get a vector orthogonal to each edge).
Implement my own Polygon2D that implements the Shape interface so that I can still pass it to Graphics2D.draw(Shape). This looks like it would be pretty difficult. The Shape interface requires tricky-to-implement methods like contains(Rectangle2D) and getPathIterator(AffineTransform). For getPathIterator in particular, it seems that in order to implement it I'd need to return an object of type PathIterator, but there are no concrete implementations of the PathIterator interface available in the public AWT packages.
Wrap Path2D in an object that "remembers" the individual vertices and provides them to the client. This worked for me when I needed an Area that remembered its component shapes: I wrapped it in a CompoundShape class that implemented the Shape interface and forwarded all the Shape methods to Area's implementation of them, while keeping track of each Shape that was added to the Area in an ArrayList. The problem with this is that if I keep track of the individual vertices in two arrays of floats, there is no way to expose them to the user without the possibility of the user changing the vertices - and since that would happen by direct array access, the internal Path2D wouldn't get notified of the changes.
Copy Polygon.java. The actual source code of Java's Polygon class is available on grepcode.com, and I could simply replace the vertex-related ints with floats throughout to get a Polygon2D. Unfortunately, when I tried this, the line import sun.awt.geom.Crossings; threw a compiler error saying "The type Crossings is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar." According to this question that happens because Sun's license agreement prevents you from replacing core Java classes with your own, but Polygon doesn't try to do that - it simply creates an object of type sun.awt.geom.Crossings, no replacing or extending happens, and I made sure to put my copy of Polygon in a package not called "java".
What's the best way to proceed with this? I'd appreciate either suggestions for how make one of these options work or an idea for another option that doesn't have the problems these encounter.
I would also recommend Path2D. GeneralPath is a legacy class; don't use it.
Path2D does provide access to the vertex values, albeit it a roundabout fashion. You need to use a PathIterator:
PathIterator pi = path.getPathIterator(null);
float[] value = new float[6];
float x = 0, y = 0;
while (!pi.isDone()) {
int type = pi.currentSegment(values);
if (type == PathIterator.SEG_LINETO) {
x = values[0];
y = values[1];
}
else if (type == PathIterator.SEG_CLOSE) {
x = 0;
y = 0;
}
else {
// SEG_MOVETO, SEG_QUADTO, SEG_CUBICTO
}
pi.next();
}
When you're ready to get fancy, you can expand that else to support the quadratic and cubic curves.
I assume you don't need those at this time as you're talking about polygons.
Also, Path2D has some handy static methods for testing whether the path intersects a rectangle and whether the path contains a rectangle or point. Sadly, there are no methods for testing for a path intersecting or containing another path.
Perhaps have the internals of the polygon at a different scale?
Multiply by a large number and typecast to int when writing to it, divide by the same large number when reading?
Can you use a 3rd party library? If so, might I suggest to use the Slick 2D Polygon class. What I would do is internally, use this class for your actual Polygon to check intersection with contains and then when you need to draw, just cast the float values to int and draw the Java2D Polygon.
I know this might not be the optimal solution, but it might work for what you're doing.

Graph data structure

Let's Say I have MyClass{ private LargeMatrix mtrx; hashCode(){...}}
JGraphT (maybe all Graph data structures) seems to be using Hash table to map the vertices. So will that affect the speed when I'm using the MyClass instead of String l1,l2,l3 ?
What are the pros and cons in that case ? Should I override hashcode(remove matrix hashcode ) ? Is there a graph that uses references instead of hashtable ?
so My code was:
package ann;
import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleDirectedGraph;
/**
* #author marmoush
*
*/
public class Network
{
DirectedGraph<String, DefaultEdge> diGraph;
String l1="hello1";
String l2="hello1";
String l3="hello3";
/**
*
*/
public Network()
{
diGraph = new SimpleDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
diGraph.addVertex(l1);
diGraph.addVertex(l2);
diGraph.addVertex(l3);
diGraph.addEdge(l1, l2);
System.out.println(diGraph.containsEdge(l1,l2));
// TODO Auto-generated constructor stub
}
}
Exception in thread "main" java.lang.IllegalArgumentException: loops not allowed
at org.jgrapht.graph.AbstractBaseGraph.addEdge(Unknown Source)
at ann.Network.<init>(Network.java:28)
at test.TestNetwork.main(TestNetwork.java:9)
Because (I think) l1.hashCode()==l2.hashCode()
EDIT:
Matrices might be zeros sometimes or ones, they change over time so I would try to come up with something that differentiate between those objects, and that seems to be stupid solution. Why can't the graph just select vertices by there position in a vector or something ?
Should I reinvent the wheel ? with a graph that uses Vectors instead of hashtables ? or there is a work around ?
I would create a Vertex class and implement equals() and hashcode() appropriately. The speed impact is not that big, it can even be faster if the id of the vertex is numeric.
well it does make sense,
your objectl1 is the same as l2, even if it is stored in the memory at a different location, for the graph it is the same vertex.
is there a reason why you need to identical vertices in the graph? maybe there is a workaround
Strings are immutable in Java, so l1 and l2 are guaranteed to point to the exact same location in memory. It's a nice feature of the language, and it speeds up string processing a lot, but it will screw you up in situations like this.
That's why you're getting the loop exception. I suspect that you want an undirected graph structure here.
For a reference, see the Java Language Spec section 3.10.5:
Literal strings within the same class (§8) in the same package (§7)
represent references to the same
String object (§4.3.1).
Literal strings within different classes in the same package
represent references to the same
String object.
Literal strings within different classes in different packages
likewise represent references to
the same String object.
The problem is that this kind of graph doesn't allow loops. You have to change the kind of graph to AbstractBaseGraph where you can set the variable loopAllowed to true, or you could try to change this variable in your SimpleDirectedGraph.
Problem: I wasn't able to change the variable in the SimpleDirectedGraph, but you can use other types of graphs that allow you do it.
I hope I could help you.

Categories