LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2013, 11:40 AM   #1
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
Question 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);


}}
 
Old 03-03-2013, 11:49 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Hi,

Okay, one time "please" is enough 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
 
Old 03-03-2013, 11:59 AM   #3
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
Angry

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
 
Old 03-03-2013, 12:23 PM   #4
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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
 
Old 03-03-2013, 01:29 PM   #5
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
Unhappy

i donot understand ,,


can you explain more on my code

or correct it <<
 
Old 03-06-2013, 01:39 PM   #6
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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

Last edited by ButterflyMelissa; 03-06-2013 at 01:41 PM.
 
  


Reply

Tags
java



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java 1.6 slow graphics yakoub Slackware 26 08-07-2007 11:18 PM
Graphics Layer Concept in java ppanyam Programming 1 08-16-2006 01:41 AM
JAVA: Graphics problem z24561 Programming 1 10-28-2005 10:37 PM
graphics with java simcox1 Mandriva 3 04-02-2005 11:55 AM
Java Graphics in Linux hiteshmaisheri Linux - Software 10 02-08-2004 10:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration