Results 1 to 3 of 3
  1. #1
    inewbie is offline Member
    Join Date
    Oct 2010
    Location
    Philippines
    Posts
    1
    Rep Power
    0

    Default need help :( applet master!!

    i have here my unfinished project for my subject COPRO-2(computer programming 2)


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class JPaintDemo extends JApplet
    implements ActionListener{

    //setting up components
    Container con = getContentPane();
    JButton a = new JButton("a");
    JButton b = new JButton("b");
    JButton c = new JButton("c");
    JButton d = new JButton("d");
    JButton e = new JButton("e");
    JButton f = new JButton("f");
    JButton g = new JButton("g");
    JButton h = new JButton("h");
    JButton i = new JButton("i");

    public void init() {
    setSize(300,200);
    con.setLayout(null);

    a.setBounds(10, 15, 45, 30);
    b.setBounds(130, 15, 45, 30);
    c.setBounds(245, 15, 45, 30);

    d.setBounds(10, 80, 45, 30);
    e.setBounds(130, 80, 45, 30);
    f.setBounds(245, 80, 45, 30);

    g.setBounds(10, 150, 45, 30);
    h.setBounds(130, 150, 45, 30);
    i.setBounds(245, 150, 45, 30);

    con.add(a);
    con.add(b);
    con.add(c);
    con.add(d);
    con.add(e);
    con.add(f);
    con.add(g);
    con.add(h);
    con.add(i);

    //adding action listener
    a.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    d.addActionListener(this);
    e.addActionListener(this);
    f.addActionListener(this);
    g.addActionListener(this);
    h.addActionListener(this);
    i.addActionListener(this);


    setVisible(true);
    }

    public void paint(Graphics gh)
    {
    a.repaint();
    b.repaint();
    c.repaint();
    d.repaint();
    e.repaint();
    f.repaint();
    g.repaint();
    h.repaint();
    i.repaint();
    }
    public void actionPerformed(ActionEvent ae)
    {
    repaint();
    }





    }
    and it should view like this:


    but the objective is..when you click button "a" and button "b" there must be a line connecting each other..and so on for the others.. and i don't know how to do it.. :(

    the objective should view like this (edit in paint only):


    Our Prof. always give us a work without teaching us the fundamentals.. but if i can do this before our finals.. i'll definitely achieve a higher grades than before..so please help me..

    sorry for my bad english.. i just really need a help guys :(

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    An approach: Save the locations of all the buttons in a collection/array.
    When the first button is clicked, get its location from above save and save it.
    When the second button is clicked, get its location and create a new object representing the line between the first and second buttons and save it.
    Clear all saved button info for next pair of clicks.
    Call repaint to call the paint method that will go to the saved line objects and draw them.

  3. #3
    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 inewbie View Post
    i have here my unfinished project for my subject COPRO-2(computer programming 2)....
    Another way to do what you're trying:

    • I would subclass JPanel and do all my drawing in the paintComponent method of the JPanel not in the paint method of the JApplet. Then place the JPanel into the JApplet's contentPane.
    • I would add my buttons to this same JPanel
    • In the button's action listener, I would add the button to a java.util.ArrayList<JButton> and call repaint on the JPanel.
    • In the paintComponent method, I'd use a for loop iterate through the ArrayList, finding the center of each button and drawing a line from one JButton to the next one on the list using the Graphics#drawLine(....) method.



    Our Prof. always give us a work without teaching us the fundamentals.. but if i can do this before our finals.. i'll definitely achieve a higher grades than before..so please help me..
    Sorry to say but it is your responsibility to study and learn the basics, whether or not your professor is good or bad. Also you'll want to know that your grade is your issue not ours. Rather we care if we help you learn Java, since if we've done so, we've succeeded.

    Much luck.

Similar Threads

  1. Master Handler Java error
    By Dradden in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 08:06 PM
  2. Netbeans Master/Detail form and TableSorter
    By jmlee in forum NetBeans
    Replies: 0
    Last Post: 01-14-2010, 09:12 PM
  3. The master list of self teaching java
    By fresh83 in forum New To Java
    Replies: 2
    Last Post: 12-27-2009, 01:09 AM
  4. JAVA Complete Master Training CD Library CBT
    By edubased in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-07-2009, 02:32 PM
  5. I need code for Use of Iteam Field Content Master file.
    By lavanya82 in forum Advanced Java
    Replies: 0
    Last Post: 03-05-2009, 01:18 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
  •