Java 2D Shading / Filling - java

I have created a "blob" from Bezier curves (screenshot below) and would now like to shade it in such a way that it appears pseudo-3D, with darker shading on all "left" edges and lighter on all "right" edges, and perhaps pure white "light spots" on the surface itself. For example: I'd be interested in how to achieve the shading used in this video.
Can anyone recommend a good way to achieve this? I am guessing that standard Graphics2D.fill and setPaint methods may not be sophisticated enough.
Also, can anyone recommend some good resources (preferrably free / online) for learning more on this?
EDIT
Some additional information: To achieve the flat fill effect below I'm creating an Area object and am adding the individual Ellipse2D Shapes to it using add(new Area(ellipse)) and then finally adding the central polygon area to avoid leaving a white space in the middle.
alt text http://www.freeimagehosting.net/uploads/bc8081cbf2.png

The IPhone apps have access to OpenGL-ES which allows significant latitude in shading and rendering what is basically a coloured iso-surface with emissive lighting. Java2d will definitely not be sophisticated enough unless you are willing to write a whole software 3d library for it.
Mixing 2d and 3d is also possible.

I used a custom RadialGradientPaint in this kineic model to get a pseudo-3D effect. I believe a more general implementation is available in Java 6.

Related

Animate a cube turning via mathematics (Java)

For a school project i have made a program that can solve a rubiks cube (you know, that cube with all the colors). But now my teachers asked if i could do some research and try out 3d animation for one of the sides. But they want to see the mathematical way to do it. I have found a way to move the corners with the use of polar coordinates. But i do not know how i could render a cube in 3d and be able to animate it.
So my question is: how can i render things like a cube in 3d (or are there any good libraries for it) and how could i use these polar coordinates to animate it?
And is there a good tutorial out there for java 3d rendering?
i must say that i have absolutely no experience with 3d rendering, so it might be a bit difficult. But i really would like to try it out.
thanks in advance
Depending on whether you want to take an existing implementation or if you want to build code for 3D animations from scratch, you might try this tutorial. Graphics programming in this area can be quite involved; a full answer of the question is beyond the scope of this site. However, there are a few main areas.
Usage of vector math for transformation of objects (translation, rotation). This can be done directly or via projective geometry using 4-by-4 matrices. The latter is easier for concatenation of transformations.
Backface culling to remove faces of the object which cannot be seen by the camera.
Using a projection and a camera model to transform 3D coordinates to 2D coordinates.
Using a rasterizer to render the 3D vector information to the screen. Here Bresenham's algorithm might be a good start.
I would suggest you look at one of the 3D libraries. One that I've used a lot and found to be excellent is JMonkeyEngine (JME) which is designed for games but would work well for your needs. It also has an excellent tutorial that takes you from basic to very complex. In fact the first step in the tutorial is a revolving coloured cube!
JME takes a huge amount of the work out of 3D modelling. You build a scene in code with materials, lights etc. and JME does all the work to render it. You can even build your models in a tool such as Blender so you don't even need to do the modelling in code. But I will warn you that using modelling tools is definitely not for the faint hearted.

Square BoundingBox with OpenGL JOGL Java

I'm trying to make a project in OpenGL using JOGL.
If you see my image http://imgur.com/DDHoXEz, I have 4 viewports with different projections but all Teapots are out of "scale", and I want to make something like a bounding box, a square with side 1, that contains all objects on the viewports, to make a scale out of the square.
Any tips?
Unless you're going to use the base teapot model for programs (which you shouldn't), I don't think this is something to spend your time on. When you get into actually using your own models, you will have direct control over the scale.
I would recommend at this point learning about different drawing methods in OpenGL (e.g., GL_TRIANGLE_FAN, GL_LINE_LOOP). Then move on to learning about vertex arrays and maybe write an OBJ importer. I can point you in the right direction if you'd like.
Here is a good place to get started on different drawing techniques.
Happy coding!

What is the android equivalent of java.awt.geom.Area?

I want to build complex shapes as the intersection of two circles and a rectangle. After researching a bit, java.awt.geom.Area class seems perfect for this task.
I was dismayed, however, when I discovered that the awt package doesn't come with the android SDK. Does anyone know of any alternatives for android that allows me to create complex shapes by defining the union and intersection of simpler shapes?
Note: Using graphics clipping to draw the shape doesn't work because I don't just want to draw the shapes, I also want to store the shapes in memory to do collision detection and other interactions.
Android Alternatives to java.awt.geom.Area
EDIT: #numan pointed out an excellent option using some classes in the Android SDK that I was unaware of at the time of the original answer:
https://developer.android.com/reference/android/graphics/Region.html
https://developer.android.com/reference/android/graphics/Region.Op.html
Region allows you to define geometric areas, and then you can use Regions op() method with Region.Op enum to calculate intersections and more complex shapes.
Some other options
You can use a Canvas to draw custom shapes, particularly using the clip* methods:
http://developer.android.com/reference/android/graphics/Canvas.html
Here are some pages about 2d graphics in Android:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable
http://developer.android.com/guide/topics/graphics/opengl.html
Some other good options if your graphics remain the same (or roughly the same) are XML-based:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables-from-xml
And one solution I find quite neat, is using 9-patch drawables:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
Collision detection
It might be overkill for your purposes, but there are a number of game physics libraries:
http://www.andengine.org http://code.google.com/p/andengineexamples/
http://bulletphysics.org
http://www.emini.at/
http://www.dremsus.com/index.php/2012/01/box2d-game-demo-in-android/
Android, libgdx and box2d basics
Or you can roll your own solution:
http://cooers.blogspot.com/2012/08/simple-collision-detection-in-2d.html
http://content.gpwiki.org/index.php/Polygon_Collision
http://www.codeproject.com/Questions/244344/Collision-Detection-in-Android
Collision detection for rotated bitmaps on Android
It really depends on the purpose; for games, you'd probably be best to just use a library; but if collision detection is the only feature you need, you'd be better off doing it yourself to save resources.
Extra Credit: Some indexes of Android libraries
http://www.appbrain.com/stats/libraries/dev
http://www.theultimateandroidlibrary.com/
http://www.openintents.org/en/
Android UI tooklit uses Skia for graphics rendering and skia uses the Region abstractions for set operations on shapes (e.g intersection, union, subtract. see Region.Op class) shapes from Paths or Rects.
Region class will also get your far for simple collision detection with Region.quickContains or Region.quickReject methods.

Java advanced image manipulation

I am having a problem with manipulating images in java 7. I have researched the problem I am having for three weeks and have failed to find a solution,
I am trying to set an image over an area, basically setting the corner posistions.
I am using a BufferedImage.
This is for a 3D game where I am writing the 3D conversion code. I have managed to create a 3d world, and populate it with cubes, filling in the sides with graphics.fillPolygon():
What I would like to do is draw an image that fills a polygon's shape.
Any help I will be grateful for (even if it is formating this post better).
You can use transformations on your graphics. Here is a little tutorial on it. With that you can transform already drawn stuff. If you want to transform multiple images in different ways, you can draw them on different canvases and transform then separately. Then you can merge the images together...
But I would recommend using a 3D engine (JOGL, jMonkeyEngine, or others) for that (unless you want to learn about the geometric calculations with this task). It is also much faster to use OpenGL than drawing the stuff by yourself and doing the calculations in code (most probably meaning: on the CPU and not the GPU).
Have you tried using the drawImage? instead of .fillPolygon?

Java/Graphics: Plotting 3d coordinates?

I'm coding a programm, that will produce 3d coordinates for a rocket and I would like to do a rudimentally graphic output for this.
it just has to be scaleable and rotationable, so that you can change the view manually.
the postions should be connected by lines and
it ould be nice to have spheres for earth and moon ( and perhaps addtional celestial objects).
I think, there should be some ready stuff for this kind of plott already available, but I couldn't find one.
So that's why I'm here to ask you, if you know such a thing.
and if there isn't I would kike to ask you how a bignner like me should start this?
I ust coded for console apllications, because there was no need for a real graphics output.
thank you in advance for any tip! :)
Andreas
I don't think there are sand-box ready things you can use to draw customizable 3d objects in Java, if you can live without strange things you can you just a graph library able to draw 3d graphs like for example jMathTools (link).. otherwise you should go into J3D with opengl and similar things.
I don't think they exist just because doing simple things is trivial if you work with OpenGL or similar APIs..
Doing what you need with OpenGL is not complex at all, just a GL_LINE_STRIP to draw the trajectory and some primitives if you need earth, moon and so on.. rotating and scaling come implicitly moving the camera of your viewport..
Take a look at: Java3D or JOGL
I can't believe nobody has mentioned this yet, but the NASA WorldWind project seems like exactly what you need: http://worldwind.arc.nasa.gov/java/ You can extend it with JOGL if needed, or you can use some of the vast modeling objects already available to mark trajectory and location in 3D coordinates, complete with zooming/rotating and the like. Having accurate Earth layers is nice as well =)
Use JOGL! It is easy to install, and if you know Java it should be easy. There are a few things in JOGL that require you to be decent in trigonometry. This tutorial might help you get started: http://www.youtube.com/watch?v=rT02jFYrXv0

Categories