wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can you give sample code to draw rectangle etc., etc.,? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can you give sample code to draw rectangle etc., etc.,?" Watch "Can you give sample code to draw rectangle etc., etc.,?" New topic
Author

Can you give sample code to draw rectangle etc., etc.,?

sabapathy
Greenhorn

Joined: Oct 16, 2000
Posts: 10
Can any one give a sample and simple code to draw rectangle on the screen using Frames(Applets are not covered by SCJP exam.so please give example in FRAMES).
Thanks.
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi sabapathy,
similar to applet u'll have to write all your drawing code in paint method of frame.. here is a small example.


sabapathy
Greenhorn

Joined: Oct 16, 2000
Posts: 10
Thank You Deekasha....
sabapathy
Greenhorn

Joined: Oct 16, 2000
Posts: 10
Hi Deekasha,
Can you tell me why the following code is giving compilation error.
Thanks for your time.
Sakthivel.
----------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
class Drawing extends Frame
{
public static void main(String h[])
{
Drawing dr = new Drawing();
dr.setSize(300,300);
dr.setVisible(true);
addWindowListener(new WindowHandler());
}
public void paint(Graphics g)
{
g.drawRect(0,0,200,100);
g.setColor(Color.cyan);
g.fillRect(50,50,150,50);
g.setColor(Color.black);
g.drawString("Hello Sabapathy",60,60);
g.drawOval(100,100,25,25);
g.setColor(Color.red);
g.fillOval(100,100,15,15);
}
class WindowHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
Sunitha Kumar
Greenhorn

Joined: Oct 17, 2000
Posts: 3
Hello Sabapathy,
In the above code you are instantiating non static inner class Window Handler inside the static main.
Non static inner class can be instantiated inside static method like new Outer().new Inner(). This is the reason for the compilation error.
Put the WindowHandler class outside the Drawing class and try compiling it.
Sunitha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you give sample code to draw rectangle etc., etc.,?
 
Similar Threads
draw rectangle in JPanel using mouse
how to draw rectangle like corelDraw?
How to display components as well as draw?
Draw a rectangle in JPanel using Mouse
how to write sine wave algorithm code