954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to draw a line

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

bsunkel
Newbie Poster
12 posts since Nov 2004
Reputation Points: 10
Solved Threads: 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);

OurNation
Master Poster
780 posts since Aug 2004
Reputation Points: 16
Solved Threads: 9
 

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);
  }
}
shedai
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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

abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

>> 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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You