Results 1 to 3 of 3

Thread: Working With Java Applets ...

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    ottawa canada
    Posts
    41

    Working With Java Applets ...

    hi,
    I just started working in java, using applets. Maybe i'm missing the general idea, but when I have a button on the page, it takes up the entire page and so I cant see the other labels on the applet (even tho I fixed the size of the button in init()). So I tried testing the init() function and found that if I change the button properties in the paint function, it works fine. So i made another function called displayInitComponents() where I re-size the button. but why doesnt the init function work for the button but work for the labels?

    here's my code:

    public class HelloWorldApplet extends JApplet implements MouseMotionListener {

    JLabel test = new JLabel();
    JLabel testid = new JLabel();
    JButton b = new JButton();

    public void paint(Graphics g) {
    super.paint(g);
    displayInitComponents();

    }

    public void init() {
    super.init();

    b.setText("button");
    test.setText ("hi1");
    testid.setText ("hi2");
    test.setBackground(Color.blue);
    testid.setForeground(Color.yellow);
    test.setLocation(10,120);
    testid.setLocation(10,130);
    testid.setSize(50,20);
    test.setSize(50,20);

    add(testid);
    add(test);
    add(b);

    validate();
    // Add the MouseMotionListener to your applet
    testid.addMouseMotionListener(this);
    test.addMouseMotionListener(this);
    b.addMouseMotionListener(this);
    addMouseMotionListener(this);
    repaint();
    }


    public void mouseDragged(MouseEvent me)
    { ... }

    public void mouseMoved(MouseEvent me)
    {...}

    void displayInitComponents(){

    b.setBounds(50,50,80,20);

    }


    Thanks
    Last edited by nima1985; Oct 19th, 2005 at 10:22 AM.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Working With Java Applets ...

    Looks like you're missing a layout manager.

    setLayout(new FlowLayout());
    setLayout(new GridLayout(parameters));
    setLayout(new BorderLayout());
    etc, etc, etc


    Not sure about setlocation, but if that positions the components according to the parameters like setBounds(), then you'll want to set the layout to null.

    So just go by this template:

    Code:
    //import statements
    
    public class Name extends JApplet implements SomeInterface
    {
       //global variables
    
       public void init()
       {
           //add components to container
    
    
          setLayout(null); //if using setbounds
          setVisible(true); //very important
       }
    }

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    ottawa canada
    Posts
    41

    Re: Working With Java Applets ...

    Hey! Thanks SOO much for the code, it solved the problem.
    I had another question though:

    I am trying to create items that can be moved on the screen by the user. So I made my applet implement MouseMotionListener.
    My two functions that override the mousemotionlistener's methods are:


    String d,n;
    String q="";
    int xpos, ypos;
    JLabel test = new JLabel();
    JLabel testid = new JLabel();

    public void mouseDragged(MouseEvent me)
    {
    d=me.getSource().toString();
    int i, j;
    for (i=0;i<(d.length()-1);i++){
    if (d.charAt(i)=='t'){
    if (d.charAt(i+1)=='e'){
    if (d.charAt(i+2)=='x'){
    if (d.charAt(i+3)=='t'){
    n=d.substring(i+5);
    for (j=0;j<(n.length()-1); j++){
    if (n.charAt(j)==',') {
    break;
    }
    q=q + n.charAt(j);
    }
    }
    }
    }
    }
    }

    xpos = me.getX();
    ypos = me.getY();


    //show the results of the motion
    repaint();
    }

    public void mouseMoved(MouseEvent me){

    }


    Basically in mouseDragged(), I try to get the name of the object being dragged and save it, and save the position the mouse is at.
    So when I drag a label (which were added to the mousemotionlistener

    testid.addMouseMotionListener(this);
    test.addMouseMotionListener(this);


    ) i want the label to show up as being moved, but everything else is still at it's old location. so in my paint() method, I call this function displayS():

    private void displayS(){

    if (q.compareTo("hi1")==0){
    test.setLocation(xpos,test.getY());
    testid.setLocation(testid.getLocation());
    }
    if (q.compareTo("hi2")==0){
    testid.setLocation(xpos,testid.getY());
    test.setLocation(test.getLocation());
    }
    }



    The problem is that when i drag the item, on the screen, as i drag, i can see another copy of the item but on the other side of the screen, and sometimes, when i stop dragging, the item i was dragging is not visible adn the other version of the item (on the other side of the page) is there.
    what is up wiht that ?

    thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width