Results 1 to 4 of 4
  1. #1
    Mark101 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default JApplet buttons wont work

    Hi I am writing a program to draw a house in japplet and create buttons to change the weather from snow to rain to shine. Here is the instructions:
    Write a well structured and documented applet to draw a house similar to one above with the sun on the background. Also create two buttons named sun and snow that as you click on the sun button shows the sun set and as you click on snow button starts to snow without showing the sun. Turn in the copies of source codes, .java, including html file along with a copy of applet viewer image. Upload your zip file as usual. The zip file includes actual .java, .class, .html and applet viewer image(s) files. Name the zip file by using your last name but use your first name and the word "house" to name .java, .class and .html files(ryanhouse.java or tracihouse.html). This assignment is due on Tuesday March 16, 2010. Your grade would be based on how creative you are in designing the house. For extra credit add some trees, lights, dog(s). You can add some other gadgets to make your house colorful, spicy and attractive.

    Please help point me in the right direction.

    Heres my code


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



    public class MarkHouse extends JApplet
    {
    private JButton snowB,rainB,shineB;
    private SnowHandler snowHandler;
    private RainHandler rainHandler;
    private ShineHandler shineHandler;
    public void paint (Graphics g)
    {


    terrain(g);
    house (g);
    }

    public void init()
    {


    snowB = new JButton("Snow");
    snowB.addActionListener(snowHandler);
    rainB=new JButton("Rain");
    rainB.addActionListener(rainHandler);
    shineB = new JButton("Shine");
    shineB.addActionListener(shineHandler);
    Container pane = getContentPane();

    pane.setLayout(null);
    snowB.setLocation(1000,250);
    rainB.setLocation(1000,350);
    shineB.setLocation(1000,450);

    snowB.setSize(80,30);
    rainB.setSize(80,30);
    shineB.setSize(80,30);


    pane.add(snowB);
    pane.add(rainB);
    pane.add(shineB);

    setVisible(true);
    }
    public void terrain(Graphics g)
    {
    setBackground(new Color(0,0,225));
    g.setColor(new Color (225,225,225));
    g.fillOval (10,30,150,35);
    g.fillOval (15,15,140,30);
    g.fillOval (325,48,160,35);
    g.fillOval (330,24,150,30);
    g.setColor(new Color(225,225,0));
    g.fillOval(780,50,100,100);
    g.setColor(new Color(0,255,0));
    g.fillRect(000,370,10000,10000);
    }
    public void house (Graphics g)
    {
    g.setColor(new Color(128,64,255));
    g.fillRect(100,250,400,300);
    g.setColor(new Color(0,0,0));
    g.fillRect(160,150,60,90);
    g.fillRect(245,430,110,120);
    g.setColor(new Color(255,255,0));
    g.fillOval(250,500,10,10);
    g.setColor(new Color(255,255,255));
    g.fillRect(125,275,100,100);
    g.fillRect(370,275,100,100);
    g.setColor(new Color(0,0,0));
    g.drawLine(175,275,175,375);
    g.drawLine(125,325,225,325);
    g.drawLine(420,275,420,375);
    g.drawLine(370,325,470,325);
    g.setColor(new Color(128,128,128));
    int x[] = {98,300,501};
    int y[] = {250,140,250};
    g.fillPolygon(x,y,3);
    g.setColor(new Color(0,0,0));
    g.drawLine(265,160,334,160);
    g.drawLine(160,200,409,200);
    g.drawLine(115,240,484,240);
    }
    private class SnowHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (e.getActionCommand().equals("Snow"))
    {
    setBackground(new Color(64,64,64));

    repaint();
    }
    else if (e.getActionCommand().equals("Rain"))
    {
    setBackground(new Color(0,0,0));
    repaint();
    }
    else if (e.getActionCommand().equals("Shine"))
    {
    setBackground(new Color(0,0,225));
    repaint();
    }

    }

    }
    private class RainHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (e.getActionCommand().equals("Rain"))
    {
    setBackground(new Color(0,0,0));
    repaint();
    }

    }
    }
    private class ShineHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (e.getActionCommand().equals("Shine"))
    {
    setBackground(new Color(0,0,0));
    repaint();
    }

    }
    }

    }
    Last edited by Mark101; 03-16-2010 at 05:22 PM.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    You never initialize the event handlers. Instead of

    Java Code:
    private SnowHandler snowHandler;
    do this:

    Java Code:
    private SnowHandler snowHandler = new SnowHandler ();

    and likewise for the other handlers.

  3. #3
    Mark101 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default event handlers

    so i initialize the eventhandlers and still no background change.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    You should create a JAppet like you create a JFrame. That is you create a panel and add components to the panel then you add the panel to the frame. Learn how to use a layout manager when adding the components to the panel. Now when you want to change the background you change the background of the panel, not the frame.

    You should never override the paint() method.

    Also, when you post code use the "Code" formatting tags.

Similar Threads

  1. Why wont import jm.util.*; work??
    By cakepizza in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 11:19 PM
  2. Replies: 3
    Last Post: 10-26-2009, 11:42 AM
  3. why wont it compile
    By bje98f in forum Advanced Java
    Replies: 1
    Last Post: 04-23-2009, 10:55 PM
  4. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM
  5. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 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
  •