Results 1 to 8 of 8

Thread: pls help!!

  1. #1
    cenevz is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default pls help!!

    Hi there,

    I am new in java, I have school problem stands like this:

    Write a code for drawing two lines. By every click, a segment from a Line should be drawn which connect the current location with the previews one. The coordinates of the Lines must be stored at Line.txt. Put Buttons for clearing the display, writing the coordinates of the lines and calculating the distance between the lines.

    my current code is as follows, but i am missing more than the half of the program, pls any help!!!

    the code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class Domasna extends JFrame implements MouseListener{
    int x,y,n,izbor;
    int r=1;
    public Domasna(){
    setBackground(Color.RED);
    setForeground(Color.BLACK);
    setPreferredSize(new Dimension(600,600));
    addMouseListener(this);

    JButton C1B = new JButton("Clear");
    C1B.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    izbor = 1;
    repaint();}});
    JPanel buttonPanel = new JPanel ();
    buttonPanel.add(C1B);
    this.add(buttonPanel,BorderLayout.NORTH);

    JButton Save = new JButton("SAVE");
    Save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    izbor = 2;
    repaint();}});
    JPanel buttonPanel2 = new JPanel();
    buttonPanel2.add(Save);
    this.add(buttonPanel2,BorderLayout.EAST);

    JButton Distance = new JButton("DISTANCE");
    Distance.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    izbor = 3;
    repaint();}});
    JPanel buttonPanel3 = new JPanel();
    buttonPanel3.add(Distance);
    this.add(buttonPanel3,BorderLayout.WEST);
    }
    public void mouseEntered(MouseEvent enevt) {}
    public void mouseExited(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}
    public void mouseClicked(MouseEvent event) {}
    public void mousePressed(MouseEvent event) {
    Graphics g = getGraphics();
    g.fillOval(event.getX()-r, event.getY()-r, 2*r, 2*r);
    }

    // public void paintComponent(Graphics g){
    // super.paintComponent(g);
    // }



    //Main method
    public static void main(String[] args){

    JFrame windo = new Domasna();
    windo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    windo.setSize(600,600);
    windo.setBackground(Color.WHITE);
    windo.setTitle("Domasna");
    windo.setVisible(true);



    //try{
    // FileWriter fws = new FileWriter("Line.txt");


    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    What's your question? Please see the link in my signature before posting again.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    cenevz is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Kavin thanks for the fast response, btw i am asking the code to be finished :)
    anyway thank u for the link i see it as useful.

  4. #4
    cenevz is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Kevin, here are my exact questions:

    1. how can i make line while clicking the mouse, and how can i stop this procedure?
    2. how can I save the coordinates of the points which I obtain while clicking the mouse?

    Regards,
    Z

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    Quote Originally Posted by cenevz View Post
    Kevin, here are my exact questions:

    1. how can i make line while clicking the mouse, and how can i stop this procedure?
    2. how can I save the coordinates of the points which I obtain while clicking the mouse?

    Regards,
    Z
    How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default

    Quote Originally Posted by cenevz View Post
    Kavin thanks for the fast response, btw i am asking the code to be finished :)
    that's up to you to do!

  7. #7
    cenevz is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    ok , i will :) thank u guys

  8. #8
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    cenevz.. i am new here and i've got a little suggestion on your code.. since almost all component in java can be associated with a mouselistener, i think it would be much better if you will not use the reserve word "implements" on the class which you are making so you wont be obliged to add those abstract methods needed.

    you can just use this code:

    <component>.addMouseListener( new MouseAdapter()
    {
    public void mouseClicked(MouseEvent e)
    {
    /* for right click */
    if (SwingUtilities.isRightMouseButton(e))
    {
    /* i just added this statement because i have read the you are asking on how to */
    /* get the point where you are clicking.. :D */
    Point p = e.getPoint();
    }
    /* for left click */
    if (SwingUtilities.isRightMouseButton(e))
    {
    /* i just added this statement because i have read the you are asking on how to */
    /* get the point where you are clicking.. :D */
    Point p = e.getPoint();
    }
    }
    }

Posting Permissions

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