Closed Thread
Results 1 to 4 of 4

Thread: Help with drawLine() method and graphics in general!

  1. #1
    Blmaster is offline Learning Programmer
    Join Date
    Jul 2008
    Posts
    50
    Rep Power
    14

    Help with drawLine() method and graphics in general!

    ok could someone please help me with how u draw a line using graphics or the method drawLine() on a JFrame or JPanel?
    like if i have something like this:
    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Window	{
    	public static void main(String[] args)	{
    		JFrame window = new JFrame();
    		window.setSize(200, 200);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		panel.drawLine(50, 20, 100, 20); // This is where the problem is and 
    													//	I tried other ways but it wont work 
    													// can someone help me
    		window.add(panel);
    		window.setVisible(true);
    	}
    }
    Where the comments are is where my problem is....
    How do u draw a line on a panel or a JFrame? something with graphics?
    i tried Graphics g = new Graphics(); but doest work
    help would be greatly appreciated
    BTW: im just trying to make a line appear in the window!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Help with drawLine() method and graphics in general!

    You can find a great example piece just by using google search.

    I found this link: Java Drawline example

  4. #3
    ismetteren is offline Newbie
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Re: Help with drawLine() method and graphics in general!

    i am pretty sure that the drawLine() method not is in JPanel. I think you have to make a class wich extends JPanel, and then overwrite the paint(Grapichs g) method.

    Code:
    Class LinePanel extends JPanel {
        public void paint(Graphics g) {
            super.paint(g);
            g.drawline(50, 20, 100, 20);
        }
    }
    i havent tested that code or anything, but i hope it gives you an idea.

  5. #4
    Blmaster is offline Learning Programmer
    Join Date
    Jul 2008
    Posts
    50
    Rep Power
    14

    Re: Help with drawLine() method and graphics in general!

    ahh actually i went to class and i figured it out. ismetteren is right, you have to have two classes, one that extends JPanel and then has all the graphics in it and another class for main which calls sets up frame and calls the Jpanel class and then adds on to frame.

    - thanks for your help

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Using an inherited method in another class' method
    By ZipOnTrousers in forum Java Help
    Replies: 1
    Last Post: 11-13-2009, 09:39 AM
  2. Graphics in VB.NET Part 2 - Graphics
    By Vswe in forum Visual Basic Tutorials
    Replies: 4
    Last Post: 11-07-2009, 06:03 PM
  3. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 2
    Last Post: 09-19-2009, 06:18 AM
  4. VB Graphics Question (DrawLine)
    By birko19 in forum Visual Basic Programming
    Replies: 8
    Last Post: 09-12-2008, 12:22 PM
  5. Need Some General VB6 Help Please.
    By Skel in forum Visual Basic Programming
    Replies: 4
    Last Post: 06-06-2008, 09:22 AM

Tags for this Thread

Bookmarks

Posting Permissions

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