1,105,229 Community Members

How to draw a line

Member Avatar
bsunkel
Newbie Poster
12 posts since Nov 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI,

All I want to do is put a straight line under my 3 labels which I have loaded into a JFrame.

After my JFrame has loaded I use the method repaint() which is supposed to call:

public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
comp2D.drawLine(0,60,100,60);


}

Which does not draw my line. The method repaint() does not even allow me to go to the method paintComponent(Graphics comp). What am I doing wrong

Any help would be appreciated

Member Avatar
OurNation
Master Poster
773 posts since Aug 2004
Reputation Points: 5 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

This is how I do my lines but I just started java a month ago and it is the only way I have been introduced to.

public void paint(Graphics g) {
g.drawLine(25, 0, 25, 50);
g.setColor(Color.black);
Member Avatar
shedai
Newbie Poster
2 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

public void draw (Graphics g)

function is not supported for the JFrame. Indeed JFrame does not have any painting capability.

You can either use the draw function for Applets or you can use a Swing component with draw capability like JPanel.

Below code should work :

import javax.swing.*;
import java.awt.*;
public class cizim extends JPanel{
  //www.bilgisayarkavramlari.com
  public void paintComponent(Graphics g){
    g.drawLine(0,0,90,90);
  }
  public static void main(String args[]){
    JFrame jf = new JFrame();
    jf.add(new cizim());


    jf.setSize(500,500);
    jf.setVisible(true);
  }
}
Member Avatar
abelLazm
Postaholic
2,087 posts since Feb 2011
Reputation Points: 183 [?]
Q&As Helped to Solve: 128 [?]
Skill Endorsements: 4 [?]
 
0
 

Don't post in tool old dead threads if you have any new thing to share then create a new thread

Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
2
 

>> Don't post in tool old dead threads if you have any new thing to share then create a new thread

There's nothing in the rules that forbids people to post in an old thread as long as the reply is on-topic and not just another question (thread-hijacking).
The post made by "shedai" is on-topic and might help people who have the same problem and find this thread on google.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article