ronak07 ronak07 - 1 month ago 17
Java Question

drawLine using AWT

I just started with GUI using AWT. The frame is opening but the Line is not being displayed.

frame image

import java.awt.*;
import java.awt.event.*;

class A extends Frame
{
public static void main(String args[])
{
Frame f= new Frame();
f.setTitle("New Frame");
f.setSize(1000,1000);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}});

}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawLine(60,500,230,5);
}
}

Answer

You're creating a Frame object, not an A object, and so your paint method is never called.

Instead of

Frame a = new Frame();

try

A a = new A();

Side comment: your paint method override should call the super's method within it.

Having said this, why are you using AWT, a library that has now been superseded by not one but two newer graphics libraries, first Swing and now JavaFx? AWT is beyond dead at this point.