Results 1 to 3 of 3
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default 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);
    			}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:
    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
    Again, thanks and best of luck with your projects!

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Thanks for this suggestions.

    Java Code:
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
    This code is awesome. :cool:

Similar Threads

  1. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 01:53 PM
  2. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 07:23 AM
  3. Select File
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 05:38 AM
  4. Replies: 4
    Last Post: 09-13-2008, 11:07 AM
  5. Circles (Loops)
    By Zebra in forum New To Java
    Replies: 1
    Last Post: 05-29-2008, 06:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •