Java draw line with border - java

I'm developing a JApplet in which the user can draw some lines over an image.
Lines can be red or green, but I need to highlight them because I don't know the background color.
So I thought that I can draw a white "border" to the line, and I tried to do this creating other two white lines to the left and to the rigth of the original one. But the result is poor.
Is there a better way to accomplish this goal?

As mentioned by #Jesper, draw the line first using a thicker Stroke (as seen in this answer).
The black outline on the letters has width 2.
g.setStroke(new BasicStroke(2f));

Related

How can I make DashPathEffect or PathDashPathEffect space dashes consistently

You can see in the image that these two dashed lines do not coincide. They are drawn along the same path, starting from the same place with the same DashPathEffect. The only difference is the sweepAngle. I've also tried PathDashPathEffect using a circle as the shape but the result is the same.
I want the dots drawn exactly on top of each other. I can't delete the path underneath, because then at the start of the yellow line, the dots will not align. Is this a bug?

Scrolling background sides appearance of a line

in my game (using libgdx) i use a scrolling background the scrolling Background it's work good for some backgrounds like this :
Good one
but when i draw my own background using inkscape or photoshope if i use a line in the sides of the background or something like shadow the probelm of a black line appear , except if i use a picture with one color and drawing in the middle . that's mean the problem is from the picture but why in the others background it's work good (There is no problem in the code and measurements)???
Appearance of a line
If code would be attached, would be great. But looks like, when drawing sprite, need draw one pixel less at the beginning, or draw one pixel more end in the end of sprite.

balls bouncing from a line drawn by mouse dragging

I have a space with bouncing balls, and when i drag mouse I want a line to be drawn in it so that balls start bouncing from it as well.
I can draw a line consisting of small circles, and add their inside area together, and if a ball intersects this area change direction. This works. The problem is that circles don't produce solid line (as mouseDragged events are fired too rarely), but if I use Line2D instead, it doesn't enclose any area.
In every mouse dragged event I can interpolate data between startPoint and endPoint and draw a circle at obtained x,y. It works, but everything freezes terribly
I can check if ball's covering rectangle contains certain color (and if so, change direction), but again how should it be done -- checking every pixel doesn't seem efficient at all?
Is there a way/best way to deal with it?
but if I use Line2D instead, it doesn't enclose any area.
Use a Path2D or a Polygon that has a (very thin) width. It will look like a line, but have an area.
Then use it in the code seen in this answer.

Java Paint Program

I have started a Java Paint program that seems to be working fine... There is just one problem. In my program I have it set up so that it repaint()'s ovals using MouseListener methods and overrides paintComponent(Graphics g). The problem is when I move my mouse to fast it begins to separate my ovals instead of making one smooth line when the mouse is dragged. Is there a way to fix this.
P.S. Keep in mind that I much rather use the fillOval method not the drawLine, because I still would like to set the stroke.
Thanks in advance
See Custom Painting Approaches for the two common ways to do painting. The example draws a Rectangle without problems as the mouse moves.
You can still set a stroke to use to draw a line between two points. You should store the previous mouse position and interpolate between the last position and the current position to create a Line2D shape. Then create a stroke that has the desired width of your oval, and apply that stroke to Graphics context, then draw the line. This link has more information about strokes and shapes.
If you really want to continue drawing ovals, you could interpolate along the line between the start/end points and draw multiple ovals in a loop.

Automatically crop image boundaries

I'm looking for an automatic way to grab a piece of a bitmap and extract a certain part of it.
Let me explain:
If I have this image:
http://imgur.com/B9U9E
It has a big white border. (see link for better white result) I'm looking for a way to only grab the dialog above. So no white borders around the dialog. Is there a way to do this by code or with a library?
You should know that the image could have any form and positioned anywhere on the white dialog.
So a user draws something on the white panel and i need the program to automatically make a rectangle about where the users drew on the canvas and save that bitmap where the user drew on the canvas (Everything in between that rectangle).
Pseudocode
Define the background color.
Scan from the left, right, bottom, top and store the locations of the transitions from background to drawing.
The rectangle defined by (left, bottom) and (right, top) defines the cropping area
For a Java code example, please see: How to auto crop an image white border in Java?
Look into Bitmap.createBitmap.

Categories