Results 1 to 2 of 2
Thread: Circle and line
- 01-27-2010, 03:06 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
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.
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?Java Code:public class Circline extends JFrame implements KeyListener { Circlinec a; public Circline() { a = new Circlinec(); setTitle("Draw Lines From Circle"); setSize(800,600); add(a); addKeyListener(this); } 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){ a.moveUp(); }else if (k.getKeyCode() == KeyEvent.VK_Z|| k.getKeyCode() == KeyEvent.VK_DOWN){ a.moveDown(); }else if (k.getKeyCode() == KeyEvent.VK_A|| k.getKeyCode() == KeyEvent.VK_LEFT){ a.moveLeft(); }else if (k.getKeyCode() == KeyEvent.VK_D|| k.getKeyCode() == KeyEvent.VK_RIGHT){ a.moveRight(); } } 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() { setBackground(Color.BLACK); w = 800; h = 600; ow = oh = 45; setSize(w, h); token = true; px = w/2; py = h/2; addMouseListener(this); addMouseMotionListener(this); } public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillOval(px-ow/2,py-oh/2,ow,oh); /*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] g.drawLine(px,py,rx,ry); } */ } /*public void calcLine(MouseEvent e) { rx = e.getX(); ry = e.getY(); repaint(); } */ public void moveUp() { py -= 5; repaint(); } public void moveDown() { py += 5; repaint(); } public void moveRight() { px += 5; repaint(); } public void moveLeft() { px -= 5; repaint(); } public void mousePressed(MouseEvent e) { if (token) { rx = getX(); ry = getY(); } //calcLine(e); } 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 }
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?
-
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
-
how to add a circle or rectangle in a jtextarea
By nicnicnic in forum Java 2DReplies: 10Last Post: 10-17-2009, 04:09 PM -
drawing a dashed circle
By Alarmmy in forum SWT / JFaceReplies: 0Last Post: 07-13-2009, 09:46 AM -
How to write numbers around a circle
By pheonix in forum New To JavaReplies: 8Last Post: 06-11-2009, 10:20 AM -
Generate numbers around a circle?
By pheonix in forum New To JavaReplies: 4Last Post: 06-05-2009, 05:08 PM -
In which circle is the Point lying?
By nidhirastogi in forum New To JavaReplies: 1Last Post: 07-02-2008, 11:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks