LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   graphics in java (http://www.linuxquestions.org/questions/programming-9/graphics-in-java-4175452522/)

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


Quote:

/*
* 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(System.in);
crcle c = new crcle();
System.out.println("Enter the radius");
double r = S.nextDouble();
c.set_radius(r);
c.Compute_AREA(r);
System.out.println(" Area of circle=" + c.get_area() );
c.Compute_PERIMETER(r);
System.out.println(" perimeter of circle" + c.get_perimetet() );
JFrame frm = new JFrame();
frm.setSize(400, 400);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
c.Shape_Draw(null);
}}
// 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(Color.red);
g.drawOval(150, 150, 70, 70);


}}

Thor_2.0 03-03-2013 12:49 PM

Hi,

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

Quote:

Scanner S = new Scanner(System.in);
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?

Thor

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

Thor_2.0 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

Quote:

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");
add(jlbHelloWorld);
this.setSize(100, 100);
// pack();
setVisible(true);
}
}
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"

Thor

nawal1991 03-03-2013 02:29 PM

i donot understand ,,


can you explain more on my code

or correct it <<

Thor_2.0 03-06-2013 02:39 PM

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

Quote:

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

Quote:

or correct it
...is 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:

Quote:

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

This, on the other hand:

Quote:

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.setColor(Color.red);
else
g.setColor(Color.black);
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?

Thor


All times are GMT -5. The time now is 03:24 PM.