Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Deekasha....
 
sabapathy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic