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.
Related
I am using canvas.drawLine to draw some line in android but the lines are too sharp but i need a curved edges
Here the 1 is what i have and 2 is what i want to achieve, means a line with curved edges rather than straight edges
How can I achieve that ??
EDIT 2:
I am trying to use the Canvas object to to draw a line. but the lines have a sharp edge, I need a rounded off edge
I am using the Paint object
mPaint = new Paint();
mPaint.setColor(Color.BLACK)
Any help would be appreciated great.
Use the Paint.setStrokeCap() method. You need Paint.Cap.ROUND. The default one is Paint.Cap.BUTT. There is a similar Path property that is called path join. It determines how to draw the parts of the path where it's constituent segments join. To set it use Path.setPathJoin(). You might need it in the future.
Good luck.
You can use below
pitchPaint.setStrokeCap(Paint.Cap.ROUND)
I currently have an arraylist of points from a freehand drawing on a canvas. I was wondering if there is a simple algorithm to detect if that shape represents a circle.I have already researched this a little and the main items I am pointed at are either the Hough transform or having bitmap images but both of these seem a little over the top for what I need it for. Any pointers to algorithms or implementation would be very helpful.
thanks in advance sansoms,
If I interpret your question correctly, you want to know if all the points are on a circle.
As illustrated in the picture, we pick three points A, B, C from the list and compute the origin O of the presumed circle. By checking the distance between O and each point from the list, we can conclude whether these points are on a circle.
If you do not know what the user wanted to draw (e.g., a circle, an ellipse, a line, or a rectangle), you could use some basic optimization algorithm to find the shape best matching the hand-drawn points.
for each basic shape (oval, rectangle, triangle, line, etc.), create a random instance of that shape and measure the error w.r.t. the given points
optimize each of the shapes (individually) until you have the oval best matching the given points, the rectangle best matching the points, the best triangle, etc.
pick the shape that has the lowest error and draw it
Maybe this answer can give you some ideas: https://stackoverflow.com/a/940041/12860
In short: calculate the second derivative. If it is quite constand, it is probably a circle.
Reading your comment, an easier method to draw a circle is for the user to click the center point, then drag the radius of the circle. It's a lot less calculation, and easier for the user to draw.
You can do the same thing with a rectangle, or any other convex polygon.
I'd like to create shadow effects around my JComponent and especially around my JTextField's (JXTextField's since i use SwingX api).
I already know about the DropShadowBorder class from SwingX but it's not exactly what i want to achieve. I decided to do it myself by using rounded rectangle and gradients.
My idea is to create a rounded rectangle, apply a gradient to it and then draw the component on top of this rectangle with a given offset to create the shadow effect.
The problem is that i'm only aware of the GradientPaint class that allow me to specify start and end point of my gradient. Unless i'm wrong, i think i can't achieve this effect which such a gradient, i think i would need a gradient that start at the center and then fade toward the edges. Is there a way to do such a gradient with the actual API or do i need to write it myself?
Thanks.
EDIT: The is to do a text field that looks like:
The shadow is more important on the bottom than on the right and left sides.
There is no easy way to achieve exactly what you want.
To create an exact effect of such shadow you will have to do several things:
Paint a black/gray (color of your shadow) rounded rectanle on a separate image sized to component plus some additional spacing at the sides
Blur that image to create a shadow from the flat rounded rectangle
Render that image under the field by either using your own UI or just replacing field's paintComponent method
Let me explain each step a bit more:
You need a separate image so that the background/component won't get blurred together with the shadow. Plus you cannot apply any filter directly to the Graphics - you need an Image.
You can read a good explanation of how-to-blur here: http://www.jhlabs.com/ip/blurring.html
You need to place (paint) the shadow image before the component itself and that is possible in two ways: paint it on the panel/container that contains the field or replace the field paintComponent method or UI itself.
If you need a radial gradient, there is one: RadialGradientPaint
http://docs.oracle.com/javase/6/docs/api/java/awt/RadialGradientPaint.html
(You specify a center point and a radius here)
BTW, what's wrong with DropShadowBorder? (it is very similar to what you want, and you could always take the source code and modify it)
I'm making a project for college, where I have to make my own primitive vector editor, and I have two questions:
Is it right to make vector line object by saving it's start point coordinates, end point coordinates, color and width of points, which it will consist of? Point is also my class, which is drawn on JPanel.
If it is right, how can I make this line selectable? The only thing I can think of is to check mouse coordinates to be inside of line width.
I also have a restriction not to use any standard functions for drawing lines, splines, etc.
Yes that's right. Or you could represent it in polar coordinates
the best thing to do is actually turn your line into an Area, which implements Shape and thus contains, which is the method you want. Area is a great abstraction because it can represent any shape but everything gets manipulated in the same way.
You can use Line2D Shape. To check selection you can gt stroked Shape from BasicStroke and check whether the stroked Shape contains clicked point.
To Point 1: You also need to save the direction of the vector.
To Point 2: There are some frameworks like GEF which do the job for you. Here you have to change your model. You need two classes: Point and Connection
You can use a class Vector if you have one with start and end Point for do it if you want.
You can have a method like that:
private static Shape generateVector(Point start, Point end)
In this method you can use one object of ´Area´ for build the vector, with Line2D. Tree lines if you want an arrow.
Or you can use a GeneralPath for build it.
For select a particular vector with the mouse, you can get the coordinates, with getX() and getY() on the MouseEvent and ask in the shape object with method contains and repaint with other color.
Good luck!
I'm new to OpenGL. I'm using JOGL.
I would like to create a sky for my world that I can texture with clouds or stars. I'm not sure what the best way to do this is. My first instinct is to make a really big sphere with quadric orientation GLU_INSIDE, and texture that. Is there a better way?
A skybox is a pretty good way to go. You'll want to use a cube map for this. Basically, you render a cube around the camera and map a texture onto the inside of each face of the cube. I believe OpenGL may include this in its fixed function pipeline, but in case you're taking the shader approach (fixed function is deprecated anyway), you'll want to use cube map samplers (samplerCUBE in Cg, not sure about GLSL). When drawing the cube map, you also want to remove translation from the modelview matrix but keep the rotation (this causes the skybox to "follow" the camera but allows you to look around at different parts of the sky).
The best thing to do is actually draw the cube map after drawing all opaque objects. This may seem strange because by default the sky will block other objects, but you use the following trick (if using shaders) to avoid this: when writing the final output position in the vertex shader, instead of writing out .xyzw, write .xyww. This will force the sky to the far plane which causes it to be behind everything. The advantage to this is that there is absolutely 0 overdraw!
Yes.
Making a really big sphere has two major problems. First, you may encounter problems with clipping. The sky may disappear if it is outside of your far clipping distance. Additionally, objects that enter your sky box from a distance will visually pass through a very solid wall. Second, you are wasting a lot of polygons(and a lot of pain) for a very simple effect.
Most people actually use a small cube(Hence the name "Sky box"). You need to render the cube in the pre-pass with depth testing turned off. Thus, all objects will render on top of the cube regardless of their actual distance to you. Just make sure that the length of a side is greater than twice your near clipping distance, and you should be fine.
Spheres are nice to handle as they easily avoid distortions, corners etc. , which may be visible in some situations. Another possibility is a cylinder.
For a really high quality sky you can make a sky lighting simulation, setting the sphere colors depending on the time (=> sun position!) and direction, and add some clouds as 3D objects between the sky sphere and the view position.