-   Programming (
-   -   graphics in java (

nawal1991 03-03-2013 12:40 PM

graphics in java
hi every one
I want in this program to calculate the area and perimeter of circle

then draw circle , i have some problem in draw , it show the frame empty

please help me ,, am study the graphics in java first time ,, please help me


* To change this template, choose Tools | Templates
* and open the template in the editor.
package circle;
import java.awt.*;
import java.util.Scanner;
import javax.swing.JFrame;
public class Circle {
public static void main(String[] args) {
Scanner S = new Scanner(;
crcle c = new crcle();
System.out.println("Enter the radius");
double r = S.nextDouble();
System.out.println(" Area of circle=" + c.get_area() );
System.out.println(" perimeter of circle" + c.get_perimetet() );
JFrame frm = new JFrame();
frm.setSize(400, 400);
// TODO code application logic here

class crcle {
double radius; double Circle_area ; double perimeter;
public crcle ( ){radius =0.0; Circle_area=0.0;perimeter=0.0; }
public crcle (double R ){
radius =R;
public void set_radius(double R)
{radius =R;}
public void Compute_AREA ( double radius ){
Circle_area = radius*radius*3.14;}
public double get_area()
{ return Circle_area ;}

public void Compute_PERIMETER ( double radius ){
perimeter =2*radius*3.14;}
public double get_perimetet(){ return perimeter;}

public void Shape_Draw(Graphics g) {
// g.setColor(;
g.drawOval(150, 150, 70, 70);


ButterflyMelissa 03-03-2013 12:49 PM


Okay, one time "please" is enough :D and, welcome to the forum.
As far as I can see (only had a few moments to look at the code) you capture input from the console


Scanner S = new Scanner(;
crcle c = new crcle();
System.out.println("Enter the radius");
double r = S.nextDouble();
...then translate stuff to the graphics plane...strange approach...

This is an example...done as an applet...

Where did you get the tutorial from?


nawal1991 03-03-2013 12:59 PM

in your example , can i use the class name (DrawExample )without extend
public class DrawExample extends Applet

because in my programe i have alot of Inherit classes like

shape > 2D & 3D > then 2D Inherit oval , circle ...etc

how i can Compatible between it

ButterflyMelissa 03-03-2013 01:23 PM

in your example , can i use the class name (DrawExample )without extend

Yes you can, in fact, you should use a graphical approach to this, the input method is for console (non-graphical) use...

An example using Swing


import javax.swing.JFrame;
import javax.swing.JLabel;

//import statements
//Check if window closes automatically. Otherwise add suitable code
public class HelloWorldFrame extends JFrame {

public static void main(String args[]) {
new HelloWorldFrame();
HelloWorldFrame() {
JLabel jlbHelloWorld = new JLabel("Hello World");
this.setSize(100, 100);
// pack();
This should produce a graphical program, it's up to you to add the required components to get input, and output a circle...I'd google or "Swing tutorial"


nawal1991 03-03-2013 02:29 PM

i donot understand ,,

can you explain more on my code

or correct it <<

ButterflyMelissa 03-06-2013 02:39 PM

A tardy reply...full agenda...sorry


can you explain more on my code
..this I can try...that...


or correct it up to you, I'm afraid.

Java can run in several "modes", one being console mode. Text only. Using the System class to read data means: console. What you need to get a grasp of is understanding how to make an interface (unis Swing code, that is best- grab a tutorial). Furthermore, a Java "program" can run as program (as all the rest) or as applet (in a browser), the difference is that an applet is shielded of, or better: the system is shielded from the actions of an applet. No system modifications are done by an applet. It is by far the safest way...

Okay, so, the goal is to learn:
- how a Swing applet is structured
- how to lay out the components (input box, drawing surface and so) in an interface
- how to set up a page to display the applet
- how to draw the shape itself, e.g. generate output

Not impossible, believe me :)

Okay, now, this:


System.out.println("Enter the radius");
double r = S.nextDouble();
is pure console code...

This, on the other hand:


import java.awt.*;
import java.applet.*;
public class Checkerboard extends Applet {
public void paint(Graphics g) {

int row; // Row number, from 0 to 7
int col; // Column number, from 0 to 7
int x,y; // Top-left corner of square

for ( row = 0; row < 8; row++ ) {

for ( col = 0; col < 8; col++) {
x = col * 20;
y = row * 20;
if ( (row % 2) == (col % 2) )
g.fillRect(x, y, 20, 20);
is code of an APPLET (no Swing, it is not compulary, just nicer to work with)

I suggest you start from the beginning:

- classes
- extending them
- Swing (or AWT)

How long have you been at it?


All times are GMT -5. The time now is 11:52 PM.