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:
Where the comments are is where my problem is....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); } }
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!
You can find a great example piece just by using google search.
I found this link: Java Drawline example
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.
i havent tested that code or anything, but i hope it gives you an idea.Code:Class LinePanel extends JPanel { public void paint(Graphics g) { super.paint(g); g.drawline(50, 20, 100, 20); } }
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks