1.11M Members

How to draw a line

 
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

 
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);
 
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);
  }
}
 
0
 

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

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article