Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Draw line in JFrame

Source code below will draw a line from top left corner of a JFrame until bottom right corner of the JFrame.

************************************************************************
COMPLETE SOURCE CODE FOR : DrawLineInJFrame.java
************************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawLineInJFrame extends JFrame
{
public DrawLineInJFrame()
{
//Set JFrame title
super("Draw A Line In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw line in JFrame from top left corner until bottom right corner
g.drawLine(0,0,getSize().width,getSize().height);
}

public static void main(String[]args)
{
DrawLineInJFrame dlijf=new DrawLineInJFrame();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Java source code to determine when application exit by print a line of text on command prompt

*************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenApplicationExit.java
*************************************************************


public class DetermineWhenApplicationExit
{
public static void main(String[]args)
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
//Application will print "BYE" when it close
System.out.println("BYE");
}
});

//Make this application terminate
System.exit(0);
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

RELAXING NATURE VIDEO