Total members 11894 |It is currently Sat Nov 23, 2024 11:06 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Draw Any Number of Circles using Mid-Point Algorithm Equations- On Mouse Events.
java code
package drawIT;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.event.MouseInputListener;

public class WindowDraw extends JFrame implements MouseInputListener {

private Point p1 = new Point();
private Point p2 = new Point();
private List list = new ArrayList();
private Circle circleObj;
private int clicks = 0;

public WindowDraw() {

setTitle("Drawing ");
addMouseListener(this);
setLocation(100, 100);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, 600, 600);
g.setColor(Color.red);
Circle currentCircle;
for (int i = 0; i < list.size(); i++) {
currentCircle = (Circle) (list.get(i));
currentCircle.CircleMidPoint(g);
}
}

public static void main(String[] args) {
new WindowDraw();
}

@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();

if (clicks == 0) {
circleObj = new Circle();
circleObj.setCenter(new Point(x, y));

clicks++;
} else {
circleObj.setP2(new Point(x, y));
circleObj.calRadius();
list.add(circleObj);
clicks = 0;
}
repaint();
}

@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
}
}

class Point {

private int x;
private int y;

public Point() {
}

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}
}

class Circle {

private Point center;
private Point p2;
private float radius;

public Circle() {
}

public Circle(Point center, Point p2) {
this.center = center;
this.p2 = p2;
radius = (float) Math.sqrt((double) ((center.getX()
- p2.getX()) * (center.getX() - p2.getX())) + ((center.getY()
- p2.getY()) * (center.getY() - p2.getY())));
}

public Point getCenter() {
return center;
}

public Point getP2() {
return p2;
}

public void setCenter(Point p1) {
this.center = p1;
}

public void setP2(Point p2) {
this.p2 = p2;
}

public void calRadius() {
radius = (float) Math.round(Math.sqrt((double) ((center.getX()
- p2.getX()) * (center.getX() - p2.getX())) + ((center.getY()
- p2.getY()) * (center.getY() - p2.getY()))));
}

public void setRadius(float radius) {
this.radius = radius;
}

public float getRadius() {
return radius;
}

void CircleMidPoint(Graphics g)
{
int xc = center.getX();
int yc = center.getY();
int R = (int) radius;

int x=0,y=R;
int d=1-R;
int c1=3, c2=5-2*R;
Draw8Points(g,xc,yc,x,y);
while(x<y)
{
if(d<0)
{
d+=c1;
c2+=2;
}
else
{
d+=c2;
c2+=4;
y--;
}
c1+=2;
x++;
Draw8Points(g,xc,yc,x,y);
}
}


void Draw8Points(Graphics g,int xc,int yc, int a, int b)
{
g.drawOval(xc+a, yc+b, 1,1);
g.drawOval(xc-a, yc+b, 1,1);
g.drawOval(xc-a, yc-b, 1,1);
g.drawOval(xc+a, yc-b, 1,1);

g.drawOval(xc+b, yc+a, 1,1);
g.drawOval(xc-b, yc+a, 1,1);
g.drawOval(xc-b, yc-a, 1,1);
g.drawOval(xc+b, yc-a, 1,1);

}


}

Attachment:
MidPoint.gif
MidPoint.gif [ 8.78 KiB | Viewed 5031 times ]




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Draw Circles using Mid-Point Algorithm Equations- On Mouse
 Draw Circles using using Polar form equations-On mouse Click     -  
 draw any number of circles using mouse clicks     -  
 Draw curves using Hermite Curve equations with mouse clicks     -  
 Mid-point Algorithm using j2me     -  
 draw any number of lines on mouse clicks     -  
 Applet get mouse clicks and draw circle     -  
 Draw Cardinal Spline With Mouse Clicks Points     -  
 Draw Bezier Curve and selecting the points with mouse click     -  
 Program to solve equations using double linked list     -  
 Point class in C++     -  



Topic Tags

Java Graphics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com