Results 1 to 2 of 2

Thread: Circle and line

  1. #1
    c_walker is offline Member
    Join Date
    Oct 2009
    Rep Power

    Default Circle and line

    Hello Java Forum participants! :D

    I need help again:confused:. My program has something to do with Graphics, Mouse and Key Listeners. I am to create a program wherein users can draw lines from the center of a movable circle to any point within the canvas. The line should only appear after pressing any point within or outside the circle. Also, the circle should still move although a line has been drawn from it. Once you released the mouse, the line should disappear. Users can also create lines while dragging the mouse. My problems are listed below my codes.

    Java Code:
    public class Circline extends JFrame implements KeyListener
        Circlinec a;
        public Circline()
            a = new Circlinec();
            setTitle("Draw Lines From Circle");
            public void keyReleased(KeyEvent k){} // does nothing
            public void keyTyped(KeyEvent k){} // does nothing
        	public void keyPressed(KeyEvent k)
                	if (k.getKeyCode() == KeyEvent.VK_W || k.getKeyCode() == KeyEvent.VK_UP){
               	    }else if (k.getKeyCode() == KeyEvent.VK_Z|| k.getKeyCode() == KeyEvent.VK_DOWN){
                	}else if (k.getKeyCode() == KeyEvent.VK_A|| k.getKeyCode() == KeyEvent.VK_LEFT){
                	}else if (k.getKeyCode() == KeyEvent.VK_D|| k.getKeyCode() == KeyEvent.VK_RIGHT){
        public static void main(String[] args) 
            (new Circline()).setVisible(true);
        class Circlinec extends Canvas implements MouseListener, MouseMotionListener
            int px, py, rx, ry;
            int w, h, ow, oh;
            boolean token;
            public Circlinec()
                w = 800;
                h = 600; 
                ow = oh = 45;
                setSize(w, h);
                token = true;
                px = w/2;
                py = h/2;
            public void paint(Graphics g)
                /*if (rx!=px && ry!=py)
                	g.setColor(Color.YELLOW);  [COLOR="DarkRed"]-----> If I uncomment this, lines can be drawn but my circle cannot be moved(up, down, left & right)[/COLOR]
            /*public void calcLine(MouseEvent e)
            	rx = e.getX();
            	ry = e.getY();
            public void moveUp()
            	py -= 5;
            public void moveDown()
            	py += 5;
            public void moveRight()
            	px += 5;
            public void moveLeft()
            	px -= 5;
            public void mousePressed(MouseEvent e)
            	if (token)
            		rx = getX();
            		ry = getY();
            public void mouseReleased(MouseEvent e) {}
            public void mouseDragged(MouseEvent e){}
            public void mouseClicked(MouseEvent e) {} // does nothing
            public void mouseEntered(MouseEvent e) {} // does nothing
            public void mouseExited(MouseEvent e) {} // does nothing
            public void mouseMoved(MouseEvent e) {} // does nothing
    Problems: 1) How can I draw a line from the center of the circle to any point within the canvas ONLY after pressing a point in the canvas?
    2) What should I do in order for the circle to still move while I am drawing a line from its center?
    3) Do I need to use doiuble buffering? In what part of my code can I apply it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    First and foremost, you should not be using Canvas with a JFrame since the former is an AWT component while the latter is a Swing component. Please change your Canvas to a JPanel and override paintComponent not paint within the JPanel.

Similar Threads

  1. how to add a circle or rectangle in a jtextarea
    By nicnicnic in forum Java 2D
    Replies: 10
    Last Post: 10-17-2009, 04:09 PM
  2. drawing a dashed circle
    By Alarmmy in forum SWT / JFace
    Replies: 0
    Last Post: 07-13-2009, 09:46 AM
  3. How to write numbers around a circle
    By pheonix in forum New To Java
    Replies: 8
    Last Post: 06-11-2009, 10:20 AM
  4. Generate numbers around a circle?
    By pheonix in forum New To Java
    Replies: 4
    Last Post: 06-05-2009, 05:08 PM
  5. In which circle is the Point lying?
    By nidhirastogi in forum New To Java
    Replies: 1
    Last Post: 07-02-2008, 11:12 PM

Posting Permissions

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