I have a strange behaviour when drawing a Path2D on a JPanel.
Some of the shapes get kind of a tail as you can see on this screenshot:
When I change the type to Line2D.Double, it is as I'd expect it:
Here's the code that draws the path / line:
Path2D.Double path = new Path2D.Double();
Graphics2D g = (Graphics2D)this.getGraphics();
for(int i=0; i<geom.size(); i++)
{
double x = ddGeom.getX(geom.get(i));
double y = ddGeom.getY(geom.get(i));
if(i==0)
path.moveTo(x-draw_center.x, y-draw_center.y);
path.lineTo(x-draw_center.x, y-draw_center.y);
}
g.draw(path);
Do you have an idea where the 'tails' in Screenshot1 come from? I use SDK Version 6.
Thank you very much for your help
Edit: When changing the code snippet to
if(i==0)
path.moveTo(x-draw_center.x, y-draw_center.y);
else
path.lineTo(x-draw_center.x, y-draw_center.y);
most (maybe 75%) of the tails disappear. Any idea why this happens?
I finally got it. Thanks to HovercraftFullOfEels hint 'strange Stroke' I played around with my strokes.
Original stroke:
BasicStroke stroke = new BasicStroke(2.0f);
Changed to:
BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
With the new Stroke all the 'tails' disappeared. I'm still not understanding why this happens, but if someone has the same problem, this workaround could help.
I'd still be very interested in an explanation for this behaviour.
Thank you for your great help
What you are seeing in your first image looks almost like ''miters''. Miters are a way to draw line joins in a path where the two outer borders of the lines that are joined are extended until they intersect and the enclosing area is filled as well.
Is it possible that your geometry contains consecutive points with almost the same coordinates? The following example exhibits the same problem because of the last two points with have almost identical coordinates.
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setContentPane(new Container() {
#Override
public void paint(Graphics graphics) {
Graphics2D g2 = (Graphics2D) graphics;
g2.setStroke(new BasicStroke(5));
g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
g2.setRenderingHint(KEY_STROKE_CONTROL, VALUE_STROKE_PURE);
Path2D.Double path = new Path2D.Double();
path.moveTo(200, 100);
path.lineTo(100, 100);
path.lineTo(101, 100.3);
g2.draw(path);
}
});
frame.setVisible(true);
Related
I'm writing a simple game for studying purposes. Everything goes fine except one little thing...
I cant figure out how to rotate square without this ugly jumping
here a simplified version of my program illustrating an issue, here i use one timer, but in original program i have 2 timers one for handle game state and second for repaint:
public class soQuestion extends JLabel {
double r;
#Override
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.clearRect(0,0, getWidth(), getHeight());
g.translate(getWidth()/2, getHeight()/2);
g.rotate(r);
g.translate(-20, -20);
g.fillRect(0, 0, 40, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
soQuestion question = new soQuestion();
frame.add(question);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new javax.swing.Timer(10, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
r += 0.005;
question.repaint();
}
}).start();
}
}
jumping is more visible if rotation delta values is small, for fast rotated objects is less visible..
all rendering hints i used has no effect
PS: Sorry for my english
PPS: i can provide more details if needed, how it looks in full version:
Thanks for all participants!
It is fully my mistake and inattention. I was copied this code section from some source :
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
And before ask a question here i couldn't saw that i used hint related to text rendering.
Special thanks to mr #Pshemo for suggesting of using this rendering hint:
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
I was know about it before asking, but i'm a human and could not figure out where a mistake for an a hour and asked.
PS: sorry for my english
I'm a new contributor, so if its not the best help, sry but i am trying :)
have you tried a solution where you are using Math.toRadiants() in your g.rotate() function? in this video: Java: Rotating and scaling images
the image rotates without jumping, it's at 6:42 in the video.
So in your solution, it would look like that: g.rotate(Math.toRadiants(r += 0.005));
it's going to be long, so thanks in advance for reading and/or answering :)
Edit: This How to draw a continuous curve of repeated ovals on speedy mouse cursor dragging? has an answer I am not completely fine with, is there no other way of doing it than drawing straight lines in between the rectangles or ovals?? (doesn't matter to me if rectangle or oval).
I have searched a lot and didn't find what I was looking for, so here we go:
I have to create a drawing application for university and I am practically finished, my one problem is nevertheless that the mouseDragged event does not seem to be recognizing movement fast enough(?). That means, if the user draws freehand with a brush and moves the mouse too fast, the programm wont draw a "continous line of rectangles". Instead there will be many gaps in the drawing. This is how my paint() override and mousemotionlistener (for currentTool 1 which is supposed to equate a brush/pen) look like:
public void paint(Graphics g){
g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 1.0f));
Iterator<Color> sc = strokeColours.iterator();
Iterator<Color> fc = fillColours.iterator();
Iterator<Boolean> fill = fillq.iterator();
for (Shape s : shapes){
g2d.setStroke(new BasicStroke(4));
g2d.setPaint(sc.next());
g2d.draw(s);
g2d.setPaint(fc.next());
if (fill.next()){
g2d.fill(s);
}
}
if (temp!=null){
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.5f));
g2d.setPaint(strokeColour);
g2d.draw(temp);
if(isfilled){
g2d.setPaint(fillColour);
g2d.fill(temp);
}
}
}
the motionlistener:
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
mx2=e.getX();
my2=e.getY();
if (currentTool==1){
Shape point= new Rectangle2D.Double(mx2-(0.5*penStroke),my2-(0.5*penStroke),0.5+penStroke,0.5+penStroke);
shapes.add(point);
addColours(strokeColour);
}
else if(...){...
}
repaint();
}
} );
}
NB: the addColours(Color c) funtion is manually defined to add strokeColour to both strokeColours and fillColours as well as adding "true" to fillq. penStroke is defined through the use of a slider and only used for this "tool".
Look at Shape, some of the child classes will do Bezier curves and such. Try CubicCurve2D.Float, QuadCurve2D.Double or such, for the last detected mouse points. Note that not all "control" points of curves lie on the curve itself, but without (much) math one can get good results.
I got an extended JLabel class where I draw my Map using the code below :
the new AffineTransform() is the identity to left my image as it is (0,0,w,h)
mygraphics2D = (Graphics2D) getGraphics();
graphics2D.scale(2.0,2.0) ;
graphics2D.rotate(....
graphics2D.drawImage(myImageIcon.getImage(),new AffineTransform(), this);
now when I click on my JLabel using this event :
public void mouseClicked(MouseEvent e) {
x =e.getX() ;
y = e.getY();
NewX = ????
NewY = ????
}
I want to retrieve my new coordinates "the scaled,rotated ... coords" I tried
Point2D ptSrc = new Point2D.Double(x, y);
Point2D ptDst = new Point2D.Double(0, 0);
mygraphics2D.getTransform().transform(ptSrc, ptDst);
but the ptDst is different from the (scaled,rotated,..) coordinates, any help please !!!
It sounds like you need both a forward and inverse transform to translate between the two co-ordinate systems. In this example, the scaling equations are explicit; in this alternate approach, a second AffineTransform is used.
Its not so hard ;-)
When you repaint the Component save the AffineTransform after the transforming with g2.getTransform()
Then call the function invert() on it
In the mouseClicked() event us the following code:
Point2D p= trans.transform(new Point2D.Double(evt.getX(), evt.getY()), null);
System.out.println("click x="+p.getX()+" y="+p.getY());
Thats it!
I found these:
http://www.javalobby.org/java/forums/t19387.html
http://www.java.net/node/685054
Don't know if they will help or not.
i am trying to draw some lines. Problem is about colors. For example. I have several lines of red color, and than i draw one line of blue color (or reversed). And sometimes, that lines those is more, is opaque for that last one.
I tried to make new color and set color with alpha composite 0.7 - for those more lines, and one color i left default - opaque (alpha 1.0). At first i draw more lines, and than last one. But that lines "overwrite" that one. Is there some solution to fix this problem?
I draw that lines on glasspane.
edit: that code is robust, so it is difficult to post it, and it is one part of thesis.
principle is 2 color for example
Color basicColor;
Color similarColor;
than i have paint method and 2 hashmaps as attributes - some points are stored.
i iterate over this map, remember that one point and similar to him, all other connect with
graphics2D.drawLine(x1,y1,x2,y2) and than change color and paint last one line with another color. I am modifying stroke too, to make it more significant.
I hope it will be enough...
edit2:
i have some Point similarPoint than some robust paint method and here is graphics modifying
iterator iterate over list of points' lists.
Point similar = null;
Iterator<Point> secondIterator;
graphics.setColor(colorOfSimilar);
while (iterator.hasNext()) {
Point point = iterator.next();
if (point.equals(similarPoint)) {
similar = similarPoint;
} else {
secondIterator = secondMap.get(point).iterator();
while (secondIterator.hasNext()) {
Point secondPoint = secondIterator.next();
graphics2D.drawLine(point.getX(), point.getY(),
secondPoint.getX(), secondPoint.getY());
}
}
}
if (similar != null) {
secondIterator = secondMap.get(similar);
graphics2D.setColor(hooverColor);
graphics2D.setStroke(new BasicStroke(2.5f));
while (secondIterator.hasNext()) {
Point secondPoint = secondIterator.next();
graphics2D.drawLine(similar.getX(), similar.getY(),
secondPoint.getX(), secondPoint.getY());
}
graphics2D.setColor(colorOfSimilar);
graphics2D.setStroke(new BasicStroke(1.0f));
}
i wrote it in notepad so sorry about some mistakes (i think brackets etc.), but this is mechanism of modifying, around that is other methods for iterate and other, but it is not important. Problem with stroke doesn´t exist, because at first i did it without stroke.
Thanks for any idea.
The result depends on which compositing rule is specified in the graphics context using setComposite(). This utility may be useful in understanding the various modes. It may also help you in preparing an sscce that exhibits the problem you describe.
Addendum: Here's an example that shows how one might use AlphaComposite.Src mode for this.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** #see http://stackoverflow.com/questions/7823631 */
public class X extends JPanel {
private static final int SIZE = 300;
private static final int INSET = 64;
private static final AlphaComposite OVER_HALF =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
private boolean src;
public X(boolean src) {
this.src = src;
this.setBackground(Color.lightGray);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Line2D line1 = new Line2D.Double(INSET, INSET,
getWidth() - INSET, getHeight() - INSET);
Line2D line2 = new Line2D.Double(getWidth() - INSET,
INSET, INSET, getHeight() - INSET);
g2.setStroke(new BasicStroke(64,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2.setComposite(OVER_HALF);
g2.setColor(Color.red);
g2.draw(line1);
if (src) {
g2.setComposite(AlphaComposite.Src);
}
g2.setColor(Color.blue);
g2.draw(line2);
}
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 0));
frame.add(new X(false));
frame.add(new X(true));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Hope this question could emphasize more about the fading out effect of Jlabel (swing).
Certainly, yes... I already follow some guide and some answers given from This Link Thread, but mine is quite a bit different. It's not just only A text inside the JLabel, there's an image i put on.
I proceed to follow on the Thread Located out of stackoverflow. And yet, it gives me a fade out effect. But there's horrible thing occured; the white background.
How to solve this out?
I share the interface here...
The First screenshot taken here is the earlier phase when the fade out have not occured yet. While,
The Second screenshot taken here is the unwanted result i mentioned.
Tobe honest, I used the Trident Library to do animatiing;
So, whenever the user click over the image it will execute this code;
Timeline tm = new Timeline(jll_btnProceed);
tm.addPropertyToInterpolate("intensity", 1.0f, 0.0f);
tm.setDuration(1000);
tm.play();
and... the JLabel itself, I used to override it using this source code;
/**
*
* #author Gumuruh
*/
public class JLabelFader extends JLabel {
private float intensity = 1.0f;
public void setIntensity(float intensity) {
this.intensity = intensity;
this.setOpaque(false);
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
final Composite oldComposite = g2.getComposite();
g2.setComposite(AlphaComposite.SrcOver);
final Color c = getBackground();
final Color color = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * (1.0f - intensity)));
g2.setColor(color);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setComposite(oldComposite);
}
}
My hand and my head can't stop for making this trully solved. Thus I tried to follow up some example from the Java Filthy Rich Client ebook and then applying the source code given below, but first I need to COMMENT the protected void paint(Graphic g) method written above, and simply adding this source code;
private BufferedImage buttonImage = null;
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
if (buttonImage == null || buttonImage.getWidth() != getWidth()
|| buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
super.paint(gButton);
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D) g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
}
in which at the end... giving me nice fade out effect. But At first, it gave me the 2nd horrible effect which is BLACK BACKGROUND rendered first. Can't believe me?? Okay, Here is First screen shot AFTER applying code from ebook. and here is the nice fade out effect result.
If there's somebody telling me;
"YOUR PNG IS NOT TRANSPARENT!".
Please, dont say like that. Because, I followed the creation of PNG inside the Photoshop nicely using this Tut.
Now, My head's spinned, but my heart laughed and can't handle it over. OMG. Geeezzz....!
And the New Stories begun...
* UPDATED FROM HERE AND BELOW *
Ehm, depply thank you very much to our friend called... MKorbel,
from his thread given at this link. Providing a clear example of fading out effect the Swing JButton and I tried to implement it into my JLabel, and violaaa...!!
IT works.
Let's give a big clap for MKorbel. :D
SO anyway, how could I fix the earlier code? Pretty simple, just COMMENT the Paint() method, and use again the paintComponent() method and it should be overriden with the new source code below;
#Override
public void paintComponent(java.awt.Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity));
if (rectangularLAF && isBackgroundSet()) {
Color c = getBackground();
g2.setColor(c);
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g2);
g2.dispose();
}
Now the JLabel become easy to be changed with its intensity -variable.
Fading out and Fading in is accomplished.
Okay. Now everything seems "OKAY". BUt hold a moment, there's something strangely occured again here. Have you noticed it? Hmmm....
I tried to give a Mouse Event (Hover On) into the JLabel that we override the paintComponent() method discussed earlier.With this line of code;
myJLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
Logically, it should change the Cursor when Hover ON. But, Here comes another strange effect. (Really sorry, but this is stil the continue of the main case). The strange effect now is the Mouse Cursor can't be changed when we Hover On the Jlabel. It still can't change the Cursor. Seems the paintComponent() method effecting the way Cursor react. Is that true?