Results 1 to 6 of 6
  1. #1
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Smile Some introductory help please

    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import java.applet.Applet;

    public class TestApplet extends JApplet {
    Graphics g;
    boolean bool;
    public void init(){
    }
    public void start(){
    }
    public void stop(){

    }
    public void destroy(){

    }
    public void paint(Graphics g){
    if (bool){
    g.drawString("This Applet Works!! :D",20,getHeight()/2);
    g.drawLine(1, 1, 20, 20);
    g.draw3DRect(getWidth()/2, getHeight()/2, 50, 100, true);
    }else{
    }
    }
    void mouseEntered(MouseEvent e){
    bool = true;
    this.paint(g);
    }

    }


    First of all, am I using the mouse event correctly?
    Second, how would I pass Graphics g so I can paint/how would I paint from other functions? i.e. I have void somefunction() { paintMeASquare() }?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    First of all, am I using the mouse event correctly?
    No. See How to write a MouseListener.
    Second, how would I pass Graphics g so I can paint/how would I paint from other functions
    Pass the graphics object to the function. I'd also recommend not calling this.paint(g) in the paint method...recipe for an infinite loop. You should however call super.paint(g) (or super.paintComponent - depending upon the context)

  3. #3
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseListener.*;
    import javax.swing.*;
    import java.applet.Applet;
    
    public abstract class TestApplet extends JApplet implements MouseListener {
    	Graphics g;
    	boolean bool;
    	public void init(){
    		addMouseListener(this);
    	}
    	public void start(){
    	}
    	public void stop(){
    		
    	}
    	public void destroy(){
    		
    	}
    	public void paint(Graphics g){
    		if (bool){
    		g.drawString("This Applet Works!! :D",20,getHeight()/2);
    		g.drawLine(1, 1, 20, 20);
    		g.draw3DRect(getWidth()/2, getHeight()/2, 50, 100, true);
    	}else{
    		}
    	}
    	public void mouseEntered(MouseEvent e){
    		bool = true;
    		super.paint(g);
    	}
    
    }
    This is what I got, but I'm not seeming to be able to get this through my head momentarily... mind helpin'? :)

  4. #4
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Well, I figured out the mouse part... :D
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.MouseListener.*;
    import javax.swing.*;
    import java.applet.*;
    import java.applet.Applet;
    
    public class TestApplet extends Applet {
    	Graphics g;
    	boolean bool;
    	public void init(){
    		addMouseMotionListener(new MouseMotionHandler());
    		addMouseListener(new MouseHandler());
    	}
    	class MouseMotionHandler extends MouseMotionAdapter{
    		public void mouseMoved(MouseEvent e){
    			showStatus("Mouse inbound!");
    		}
    	}
    	class MouseHandler extends MouseAdapter{
    		public void mouseExited(MouseEvent e){
    			showStatus("Mouse out of bounds!!");
    		}
    	}
    	public void start(){
    	}
    	public void stop(){
    		
    	}
    	public void destroy(){
    		
    	}
    	public void paint(Graphics g){
    		if (bool){
    		g.drawString("This Applet Works!! :D",20,getHeight()/2);
    		g.drawLine(1, 1, 20, 20);
    		g.draw3DRect(getWidth()/2, getHeight()/2, 50, 100, true);
    	}else{
    		}
    	}
    	public void mouseEntered(MouseEvent e){
    		bool = true;
    		super.paint(g);
    	}
    
    }
    Now somebody mind telling me exactly how to work with this fantastic paint method? How to make variables point to lines & such, perhaps? :D?

  5. #5
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    One question, before that, though: how do I put up such a file (with multiple classes, as you can tell) on a website? Do I just upload this file, & not the others? Or What? :confused:

  6. #6
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Sorry for triple posting, but I just put the main file & all the other classes in the same directory on my site, & it worked!! :D So... is that the PROPER way of doing it?

Posting Permissions

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