Results 1 to 3 of 3
  1. #1
    syntrax is offline Member
    Join Date
    Sep 2009
    Location
    http://www.kouje.com
    Posts
    16
    Rep Power
    0

    Cool simple MoustAction Listener help

    I'm trying to have make this applet where you have this house and when you click in it, the windows and doors only should turn black.

    My code compiles and the logic seems to be right, but for some reason it's not working. Can any 1 point out what am I missing? ty

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Random;
    
    public class Assignment14 extends JApplet
    {
    	Color mycolor = Color.black;
    	int x1[] = {70, 750, 400};
    	int y1[] = {250, 250, 50};
    	boolean changeColor = true;
    	
    	public void init()
    	{
    		addMouseListener
    		(
    			new MouseAdapter()
    			{
    				public void mousePressed(MouseEvent e)
    				{
    					repaint();
    				}
    			}
    		);
    	}
    	
    	public void paint(Graphics g)
    	{
    		
    			drawHouse(g);
    		
    			changeColor(g);
    	}
    	
    	public void drawHouse(Graphics g)
    	{
    		super.paint(g);
    				
    		// house.
    		g.setColor(mycolor);
    		g.drawRect(110, 250, 600, 330);
    		
    		// roof.
    		g.drawPolygon(x1, y1, 3);
    		
    		// left window.
    		g.drawRect(200, 300, 100, 100);
    		g.drawLine(250, 300, 250, 400);
    		g.drawLine(300, 350, 200, 350);
    		
    		// door.
    		g.drawRect(400, 430, 100, 150);
    		
    		// right window
    		g.drawRect(550, 300, 100, 100);
    		g.drawLine(600, 300, 600, 400);
    		g.drawLine(650, 350, 550, 350);
    	}
    	
    	public void changeColor (Graphics g)
    	{
    		int colorChange = 0;
    		g.setFont(new Font("Dialog", Font.BOLD, 15));
    		g.setColor(new Color(colorChange, colorChange, colorChange));
    	}
    }
    html code

    Java Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    	<head>
    	</head>
    	<body bgcolor="000000">
    		<center>
    			<applet
    				code	= "Assignment14.class"
    				width	= "800"
    				height	= "600"
    				>
    			</applet>
    		</center>
    	</body>
    </html>
    Last edited by syntrax; 12-13-2009 at 04:31 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your change color method doesn't do anything. You need have your mouse listener change a boolean variable with class scope, and you need to have an if block in your paint method (or here your drawHouse method which is equivalent) and have some drawing instructions to fill some rects if the boolean is true.

    Also, most of us don't draw directly in a JApplet, but rather in a JPanel that the JApplet displays. This allows your applets to have the painting advantages of Swing. e.g.,

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Ass14 extends JApplet {
       private JPanel panel = new JPanel() {
          @Override
          protected void paintComponent(Graphics g) {
             super.paintComponent(g);
             myPaint(g);
          }
       };
       private boolean fillBlue = false;
       
       private void myPaint(Graphics g) {
          g.setColor(Color.black);
          g.drawRect(100, 100, 200, 200);
          if (fillBlue) {
             g.setColor(Color.blue);
             g.fillRect(100, 100, 200, 200);
          }
       }
       
       public void init() {
          try {
             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                   createGUI();
                }
             });
          }
          catch (Exception e) {
             System.err.println("createGUI didn't successfully complete");
          }
       }
    
       private void createGUI() {
          panel.addMouseListener(new MouseAdapter() {
             
             public void mouseClicked(MouseEvent e) {
                fillBlue = !fillBlue;  // this will toggle the color
                panel.repaint();
             }
          });
          getContentPane().add(panel);
       }
    }

  3. #3
    syntrax is offline Member
    Join Date
    Sep 2009
    Location
    http://www.kouje.com
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 09:40 PM
  2. #key listener problem
    By mij1_7 in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 09:02 PM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Posting Permissions

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