-
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.
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
}
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?
-
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.