Results 1 to 6 of 6
  1. #1
    buffalo is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default drawLine - inconsistent endpoint

    In a particular applet, I have a need to draw lines with a precise length. There seems to be an inconsistency, however, whether the second endpoint is included or not. From experimentation I encountered both cases:
    1) drawing directly in an applet window or in an offscreen image with Graphics, the second endpoint is included.
    2) drawing in an offscreen image with Graphics2D the seconded endpoint is not included.

    Is this generally true and if so is it a bug in Java? (version 1.6.0_24)

  2. #2
    buffalo is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default correction

    The previous post is not correct. From further experimentation, the second endpoint is not included for Graphics2D and the color is ARGB (with alpha). The second endpoint is apparently included for all other conditions.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    For this sort of question it often pays to post a small runnable example of the strange behaviour (Short, Self Contained, Correct Example).

    This will help clarify if there is some problem with your usage of the classes (or misconception about what they do), or something genuinely weird with Graphics2D.

  4. #4
    buffalo is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default example

    The following example draws three lines: The top and middle are drawn with Graphics2D into an Image which is then copied to the applet window. The bottom line is drawn with Graphics directly into the applet window. The middle line has an RGB color and the other two have an ARGB color.
    The top line is one pixel shorter than the other two, apparently because it is missing the second endpoint.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.*;
    
    public class lines extends Applet{
    	static int width=100,height=100;
    	static Frame frame;
    	BufferedImage buf2;
    	Graphics2D b2g;
    
    public lines(){
    	}
    
    public static void main(String[] args){
    	lines ls=new lines();
    	frame=new Frame("Lines");
    	frame.setVisible(true);
    	frame.setVisible(false);
    	frame.setSize(width,height);
    	frame.add("Center",ls);
    	ls.init();
    	frame.setVisible(true);
    	ls.start();
    	}
    
    public void init(){
    	buf2=new BufferedImage(width,height/2,BufferedImage.TYPE_INT_ARGB);
    }
    
    public void paint(Graphics g){
    	Graphics2D b2g=(Graphics2D)buf2.getGraphics();
    	b2g.setStroke(new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    	b2g.setColor(Color.white);
    	b2g.fillRect(0,0,width,height);
    	b2g.setColor(new Color(0,0,0,127));
    	b2g.drawLine(20,10,30,10);
    	b2g.setColor(new Color(0,0,0));
    	b2g.drawLine(20,20,30,20);
    	g.drawImage(buf2,0,0,this);
    	g.setColor(new Color(0,0,0,127));
    	g.drawLine(20,30,30,30);
    	}
    }
    Last edited by buffalo; 04-26-2011 at 11:07 PM. Reason: formatted code with tags

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Perhaps it might be useful to read the section "Rendering Compatibility Issues" in the Graphics2D API docs. Certainly the lines appear the same length when I add

    Java Code:
    public void paint(Graphics g){
        Graphics2D b2g=(Graphics2D)buf2.getGraphics();
        [color=blue]b2g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);[/color]
        b2g.setStroke(new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
        // etc...

  6. #6
    buffalo is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default RenderingHint

    Thanks Pbrockway2, I tried inserting the RenderingHint and indeed you're right - the lines are the same length. I read the docs you suggested and found them somewhat informative but quite confusing. I'll read them again and perhaps Google for more information. I would like to get a good understanding of the overall issue so as not to be caught by surprise with similar problems in the future.

Similar Threads

  1. problem with drawline using arrow key
    By shazlin in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 02:12 AM
  2. javax.xml.ws.Endpoint how to restart
    By Rickyp in forum Advanced Java
    Replies: 0
    Last Post: 11-30-2010, 12:32 PM
  3. Inconsistent out-of-bounds error
    By geforce2000 in forum AWT / Swing
    Replies: 7
    Last Post: 12-23-2009, 09:27 AM
  4. htmlparser inconsistent behavior
    By sjchase in forum Advanced Java
    Replies: 0
    Last Post: 07-22-2009, 12:07 AM
  5. drawing ellipse by drawline method?
    By hopey in forum Java 2D
    Replies: 8
    Last Post: 04-19-2009, 12:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •