NadiaRashid
asked on
(URGENT!)How to draw multiple circles in java swing
Hi there
Im doing a project, and i need to draw multiple circles that represent data; i get the coords and radius(as an arraylist) from another program.
Im new and awful at java swing; and i need to write a method and constuctors to draw these circles, so i get the coords and radius from another program; i just dont know how to tell the java swing to draw all of them and i've been searching the sun website for days!
Please may you post some demo code on how to draw circles in jswing to give me a nudge in the right direction??
Thanks in advance!
Im doing a project, and i need to draw multiple circles that represent data; i get the coords and radius(as an arraylist) from another program.
Im new and awful at java swing; and i need to write a method and constuctors to draw these circles, so i get the coords and radius from another program; i just dont know how to tell the java swing to draw all of them and i've been searching the sun website for days!
Please may you post some demo code on how to draw circles in jswing to give me a nudge in the right direction??
Thanks in advance!
ASKER
Hi sivachirravuri
Thanx for such a prompt response. This posting is a bit complicated, could you break it down for me? i just need to understand how i write a method to draw a circle...im confused because some online examples use elipses and others use a Circle method...i really have no idea whats going on with swing.
thanks for your response once again....nadia
Thanx for such a prompt response. This posting is a bit complicated, could you break it down for me? i just need to understand how i write a method to draw a circle...im confused because some online examples use elipses and others use a Circle method...i really have no idea whats going on with swing.
thanks for your response once again....nadia
ASKER
correction, i mean fillOval NOT elipses...ooops :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thankx...i found a graphics one too...from sun.com
public void paint(Graphics g) {
// Dynamically calculate size information
// (the canvas may have been resized externally...)
Dimension size = getSize();
int d = Math.min(size.width, size.height); // diameter
int x = (size.width - d)/4;
int y = (size.height - d)/4;
// draw circle (color already set to foreground)
g.fillOval(x, y, d, d);
g.setColor(Color.black);
g.drawOval(x, y, d, d);
}
except now it's hiding all the other stuff i have in my GUI so i need to find a JPanel method for drawing a circle...anyone got anything???
thanks for your input sivachirravuri
cheers
public void paint(Graphics g) {
// Dynamically calculate size information
// (the canvas may have been resized externally...)
Dimension size = getSize();
int d = Math.min(size.width, size.height); // diameter
int x = (size.width - d)/4;
int y = (size.height - d)/4;
// draw circle (color already set to foreground)
g.fillOval(x, y, d, d);
g.setColor(Color.black);
g.drawOval(x, y, d, d);
}
except now it's hiding all the other stuff i have in my GUI so i need to find a JPanel method for drawing a circle...anyone got anything???
thanks for your input sivachirravuri
cheers
Welcome. Did you find my second input understandable...? I hope it will be useful
ASKER
just found a JPanel one here https://www.experts-exchange.com/questions/21581182/Adding-a-Graphics-element-to-a-JPanel.html?query=java+radius&clearTAFilter=true
will post whatever i come up with
will post whatever i come up with
ASKER
second input is understandable...now for this post i just put up...just realised its using graphics tooo...but i sorta have direction on how to draw a circle....so you certainly did help...thank you!
Check the following link or code. Here is the example.
http://forum.java.sun.com/thread.jspa?threadID=782595
Code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities
import javax.swing.event.MouseInp
public class CirclePanel extends JPanel
{
private final Map shapesToLocations = new LinkedHashMap();
private final Stroke stroke = new BasicStroke(3.0f);
private final Paint paint = Color.BLACK;
public static final void main(String[] args)
{
SwingUtilities.invokeLater
{
public void run()
{
CirclePanel p = new CirclePanel();
p.addShape(new Ellipse2D.Double(10.0, 10.0, 20.0, 20.0));
p.addShape(new Ellipse2D.Double(10.0, 40.0, 20.0, 20.0));
JFrame f = new JFrame();
f.setDefaultCloseOperation
f.setContentPane(p);
f.setSize(300, 300);
f.setLocationRelativeTo(nu
f.setVisible(true);
}
});
}
public CirclePanel()
{
MouseHandler h = new MouseHandler();
addMouseListener(h);
addMouseMotionListener(h);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = ((Graphics2D) g);
Paint p = g2d.getPaint();
Stroke s = g2d.getStroke();
g2d.setPaint(this.paint);
g2d.setStroke(this.stroke)
for (Iterator i = this.shapesToLocations.key
{
Shape shape = (Shape) i.next();
Point location = (Point) this.shapesToLocations.get
g2d.translate(location.x, location.y);
g2d.draw(shape);
g2d.translate(- location.x, - location.y);
}
g2d.setPaint(p);
g2d.setStroke(s);
}
public void addShape(Shape shape)
{
this.shapesToLocations.put
}
public void removeShape(Shape shape)
{
this.shapesToLocations.rem
}
private final class MouseHandler extends MouseInputAdapter
{
private Shape dragShape = null;
private Point lastPoint = null;
public void mousePressed(MouseEvent e)
{
lastPoint = e.getPoint();
Point p = new Point();
for (Iterator i = shapesToLocations.keySet()
{
Shape shape = (Shape) i.next();
Point location = (Point) shapesToLocations.get(shap
p.x = lastPoint.x - location.x;
p.y = lastPoint.y - location.y;
if (shape.contains(p))
{
this.dragShape = shape;
}
}
}
public void mouseReleased(MouseEvent e)
{
this.dragShape = null;
this.lastPoint = null;
}
public void mouseDragged(MouseEvent e)
{
if (this.dragShape != null)
{
Point location = (Point) shapesToLocations.get(this
location.x += (e.getX() - lastPoint.x);
location.y += (e.getY() - lastPoint.y);
this.lastPoint = e.getPoint();
repaint();
}
}
}
}