Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT DrawingCircle

Have Programming Question? Ask it here!
 
 
 


Mona
DrawingCircle
1 Answer(s)      5 years and 6 months ago
Posted in : Swing AWT



Advertisement
View Answers

February 5, 2009 at 1:30 AM


Hi friend,


import java.awt.*;
import javax.swing.*;

public class CircleDemo extends JPanel {

public CircleDemo() {
setPreferredSize(new Dimension(200, 200));
setBackground(Color.pink);
}


public void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircle(g, 30, 30, 20);
drawCircle(g, 30, 30, 15);
drawCircle(g, 30, 30, 10);
drawCircle(g, 80, 70, 5);
}

public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);
}
}

-------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


class CircleMain extends JFrame{
//instance variables
CircleDemo drawing = new CircleDemo();

//constructor
CircleMain() {
//Get content pane, set layout, add components
Container content = this.getContentPane();
content.setLayout(new BorderLayout());

content.add(drawing, BorderLayout.CENTER);

this.setTitle("Draw Circles");
this.setSize(200,150);
}//end constructor

public static void main(String[] args) {
JFrame myWindow = new CircleMain();
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}
}

--------------------------------------------

Visit for more information:

http://www.roseindia.net/java/example/java/swing/

Thanks.


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com