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)
Related
I want to draw a curved Label, But don't know how. Is there a way to get a curved Label?
For example, I have this background. I need to draw the 'ARCADIAN DEER' not straight, but curved. Any suggestions on what way I can think?
This is probably not the best way, but probably the easiest. Assuming you are using Scene2D - make each letter it's own Label, then make a custom helper Group to position and rotate the labels as you like. You might even be able to use a HorizontalGroup for automatic placement along X.
I want to create a clickable image, my image has some different clickable parts in it, like this one:
I want to draw a custom shape like :
A,B,C,D,E,F
and make sure when user click on of this something happen.
the problem is I don't have any kind of idea to, how create shapes like the shapes in the image make sure it just fix on the image and in different screen size don't see a massed up thing.
Will there be more than many of such images?
If no I suggest you to create mask image for each region where black part of image represents the region and white part excludes rest.
To draw image:
create custom View
in constructor don't forget to use setWillNotDraw to true so you can do custom drawing
override View.onDraw method where you can draw main image and all others with some filters via setColorFilter.
To handle click events:
override onTouchEvent method
get touch position
compare touch position with point color in mask image
To optimise:
create mask image downscaled by some scale factor
during comparison divide touch position by scale factor
This is not ideal, but solution with vectors is non trivial I think
Take it as image and setOnclickListner for that image
I would like to create a fading color effect, something like a cloud. A transition between
two Color. I found a link. But, at this topic, the writer define an bitmap pixel by pixel. Is there an other way to do this, like a fadingDrawable class? Or this is the most effective way of fading?
Without knowing what specific libraries you're using, I can't be too specific, but as far as I know:
Most graphics libraries include some gradient functionality - this will probably be relatively fast.
And if they don't, I can't see any way to do it other than drawing the gradient pixel by pixel
I'm doing a simulator project that tests several A* based algorithms and show how they work and their results.
The algorithms are all multi-agent and run on a grid map environment.
I used a JPanel for the grid which contains a two dimensional array of Cells where each Cell is a custom class that extends the Component class and use the paint method to draw the stuff i need inside each cell.
For the drawing inside the cell I use method such as Graphics.fillRect or Graphics.drawImage to fill each cell with a certain color or icon).
I'm using a special Icon for the start position and goal position of every agent on the grid.
My problem is that I want to be able to draw more than one item on the same cell.
For example I want to be able to show the path of one of the agents by painting the cells along the path in a special color and the path might go through a start position of a different agent, so I want to be able to fill the cell with the color and have an icon drawn on top.
In another example I want to be able to mix two colors using alpha blending.
If I use graphics.fillRect() with one color that has alpha and then use it again with a different color with alpha value it won't work since the last fillRect() will override the first call.
Is there a way I can achieve what I need using the same Cell Component I created or should I implement it differently?
Perhaps there is a better solution to this problem?
I would really appreciate any advice on this matter.
If you draw a rectangle with 50% alpha and then draw another one, the second one will override it instead of blending with it.
It depends on the mode. This convenient utility shows the result of blending different colors using the modes defined in AlphaComposite. The available source code may offer some insights for your project.
Addendum:
the stuff I was trying to composite was on the same Component.
The example cited does exactly this, as does this example. If AlphaComposite does not meet your requirements, you can always vary hue, saturation and/or value; this example composes a color table based on saturation.
I am trying to use JXMapViewer (from swingx-ws) with Open Street Maps. I was wondering if it would be possible to display the map tiles in the JXMapViewer based on heading up, rather than on North up. For example, the normal car GPS navigation systems let you do that.
I've looked through the documentation and there doesn't seem to be a straightforward way to do this. Is there something else that accomplish this, besides JXMapViewer?
Nevermind, I found a solution. Here is how I did it(if anybody is interested) :
I subclassed JXMapViewer, and overrode the paint method.
In the paint method contents of the JPanel are converted to a BufferedImage which is then rotated according to an angle and then painted on top of the panel.
so super.paint()-> BufferedImage-> apply an affineTransformation to it-> draw the new Image.
Of course, you would also need to override the convertGeoPositionToPoint and convertPointToGeoPosition methods taking into account the fact that the image is rotated.