Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help regarding GeneralPath

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help regarding GeneralPath

    Hi,

    I've been having some computer graphics lectures recently where we start off with 2D. I am able to draw certain shapes with the drawLine-method in paint, but I am still unable to have any visual content other than the background color when I draw a few lines. The code below is real simple and I have been trying to figure out what's wrong with it for hours now and I'd really appreciate some feedback.

    Problem is
    - I can't get the two lines printed
    - I don't understand what's wrong with the code
    - I'm sleepy

    public class generalpathtest extends JApplet{
     
    	public static void main (String[]args) {
    		JFrame frame = new JFrame();
    		frame.setTitle("GeneralPath");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JApplet applet = new generalpathtest();
    		applet.init();
    		frame.getContentPane().add(applet);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public void init() {
    		JPanel panel = new generalpanel();
    		getContentPane().add(panel);
    	}
    }
     
    class generalpanel extends JPanel {
     
    	public generalpanel() {
    		setBackground(Color.white);
    		setPreferredSize(new Dimension(300,400));
    	}
     
    	public void paintCompontent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
    		GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
    		g2.setColor(Color.black);
     
    		float w = 500, h = 500;
     
    		float x1 = 0.0f*w;	float y1 = 0.0f*h;
    		float x2 = 0.5f*w;	float y2 = 0.5f*h;
    		float x3 = 0.2f*w;	float y3 = 0.1f*h;
     
    		path.moveTo(x1, y1);
    		path.lineTo(x2, y2);
    		path.lineTo(x3, y3);
    		path.closePath();
     
    		g2.draw(path);
    		g2.translate(120, 120);
    		g2.fill(path);
    	}
    }

    regards
    Ole Martin
    Last edited by olemagro; January 22nd, 2010 at 03:54 AM. Reason: typo


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help regarding GeneralPath

    Figured it out. It was caused by a tiny, yet fatal error in the paintComponent method name. It said "paintCompontent".
    Last edited by olemagro; January 22nd, 2010 at 09:07 AM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help regarding GeneralPath

    Hello olemagro.

    Welcome to the forums. I'm glad you resolved your issue!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help regarding GeneralPath

    Cheers! Grammar is overrated.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help regarding GeneralPath

    For what its worth, you can use the @Override keyword to let the compiler know you are overriding, nice for situation where you may misspell a function and not actually be overriding it:

    //in a class the extends JComponent
    @Override public void paintComponent(Graphics g)//compiles
    {
     
    }
    ////////// versus
    @Override public void paintCompontent(Graphics g)//compiler will throw a compilation error
    {
     
    }

  6. #6
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help regarding GeneralPath

    Quote Originally Posted by copeg View Post
    For what its worth, you can use the @Override keyword to let the compiler know you are overriding, nice for situation where you may misspell a function and not actually be overriding it:

    //in a class the extends JComponent
    @Override public void paintComponent(Graphics g)//compiles
    {
     
    }
    ////////// versus
    @Override public void paintCompontent(Graphics g)//compiler will throw a compilation error
    {
     
    }
    I didn't know that. I can imagine that being helpful while overriding important method implementations like that.

    Thanks

  7. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Robot with the head of Einstein

    Albert Hubo - a robot with the head of the Einstein Forum Asia-Pacific Economic Cooperation (photo Yonhap, Won-Gun Shin and AP Photo / David Guttenfelder).



    [url=http://toxboe.net/users/16907]order avelox with discount in us Fontana avelox

    Robot with the head of Einstein