Results 1 to 3 of 3
Thread: Draw circles, select circles
- 05-15-2010, 07:49 PM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
Draw circles, select circles
Hi.
I have written small program.
In this program we can draw circles and select them.
I will post it here. it might be a useful for someone.
class: Circle.java
Java Code:import java.awt.Graphics; import java.awt.Point; public class Circle { public Circle(Point center, Point endPoint) { this.center = center; this.radius = (int)center.distance(endPoint); selected = false; } public void drawCircle(Graphics g) { g.drawOval(center.x - radius, center.y - radius, 2*radius, 2*radius); } public boolean selectCircle(Point point) { int newRadius = (int)center.distance(point); if(Math.abs(radius - newRadius) < 4) return true; return false; } public void setSelectedCircle(boolean set) { selected = set; } public boolean getSelectedCirlce() { return selected; } private Point center; private int radius; boolean selected; }
class: DrawCircles.java
Java Code:import java.awt.Color; import java.awt.Point; import java.awt.Graphics; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; import java.util.ArrayList; import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; public class DrawCircles { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { MFrame frame = new MFrame(); frame.showGUI(); } }); } } class MFrame extends JFrame { MPanel panel = new MPanel(); public MFrame() { add(panel); } public void showGUI() { setTitle("Draw Circles, Select Circles"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500,500); setLocation(200,200); setVisible(true); } } class MPanel extends JPanel { Circle circle; Point startPos = null; Point endPos = null; Point relativePos = null; ArrayList<Circle> arrayOfCircles = new ArrayList<Circle>(); public MPanel() { addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent arg0) { if(!arrayOfCircles.isEmpty()) { for(int i = 0; i < arrayOfCircles.size(); i++) { if(arrayOfCircles.get(i).selectCircle(arg0.getPoint())) arrayOfCircles.get(i).setSelectedCircle(true); else arrayOfCircles.get(i).setSelectedCircle(false); } repaint(); } } @Override public void mouseEntered(MouseEvent arg0) { } @Override public void mouseExited(MouseEvent arg0) {} @Override public void mousePressed(MouseEvent arg0) { startPos = arg0.getPoint(); } @Override public void mouseReleased(MouseEvent arg0) { endPos = arg0.getPoint(); if(startPos != null) { arrayOfCircles.add(new Circle(startPos, endPos)); } } }); addMouseMotionListener(new MouseMotionListener() { @Override public void mouseDragged(MouseEvent arg0) { if(startPos != null) { relativePos = arg0.getPoint(); circle = new Circle(startPos, relativePos); repaint(); } } @Override public void mouseMoved(MouseEvent arg0) { } }); } public void paint(Graphics g) { super.paint(g); if((startPos != null) && (relativePos != null)) { g.setColor(Color.GREEN); circle.drawCircle(g); } for(int i = 0; i < arrayOfCircles.size(); i++) { if(arrayOfCircles.get(i).getSelectedCirlce()) g.setColor(Color.RED); else g.setColor(Color.BLUE); arrayOfCircles.get(i).drawCircle(g); } } }
-
The code looks great -- thanks for posting it!
A few suggestions, if you don't mind:
1) Override the JPanel's paintComponent method, not the paint method. This is safer since you have less of a chance of messing up painting of the component's children or borders.
2) Of course change the first method call in the method above from super.paint(g) to super.paintComponent(g) .
3) In the paintComponent method, get a Graphics2D object by casting the Graphics object, and use it to set the rendering hints to do anti-aliasing. This will result in smoother drawing.
Visually, I'm suggesting this:
Again, thanks and best of luck with your projects!Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); //!! get Graphics2D object and set anti-aliasing to "on" Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //... continue with
- 05-17-2010, 02:02 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
Similar Threads
-
newbie drawing two circles simultaneously
By nadeemshafi9 in forum Threads and SynchronizationReplies: 7Last Post: 01-09-2011, 01:53 PM -
How to retain value in struts 2 using <s:select></s:select> tag
By SaiPrasad@Sella in forum Web FrameworksReplies: 0Last Post: 02-09-2009, 07:23 AM -
Select File
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 11-06-2008, 05:38 AM -
Beginner Java graphics - filling concentric circles with color
By GenkiSudo in forum New To JavaReplies: 4Last Post: 09-13-2008, 11:07 AM -
Circles (Loops)
By Zebra in forum New To JavaReplies: 1Last Post: 05-29-2008, 06:38 AM


LinkBack URL
About LinkBacks

Bookmarks