Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:47:11+00:00 2026-06-12T02:47:11+00:00

I’m working on a homework assignment to draw a house in a java applet.

  • 0

I’m working on a homework assignment to draw a house in a java applet. The house has three rectangles within a large main rectangle representing a door and two windows. I need the windows to change color when clicked on, I’ve reached the point where I have drawn the house and have the doors and windows drawn as well but I am not able to change the color of them based on clicking the mouse in them. I’m having some trouble determining why this is the case.

To summarize, the house is drawn; door and window rectangles are drawn and filled in black. When clicking on any of the window or door rectangles nothing occurs, no errors and no change in color.

Code follows:

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

public class DrawHouse extends JApplet implements MouseListener
{
    private int mX; //variable to hold x position of the mouse cursor when clicked
    private int mY; //variable to hold y position of the mouse cursor when clicked
    private int rect1x;
    private int rect1y;
    private int rect1w;
    private int rect1h;
    private int rect2x;
    private int rect2y;
    private int rect2w;
    private int rect2h;
    private int rect3x;
    private int rect3y;
    private int rect3w;
    private int rect3h;
    boolean rect1Clicked;
    boolean rect2Clicked;
    boolean rect3Clicked;

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

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

        Polygon pg = new Polygon();

        pg.addPoint(280, 200);
        pg.addPoint(470, 100);
        pg.addPoint(670, 200);

        g.drawPolygon(pg);

        g.setColor(Color.BLACK);
        g.drawRect(300, 200, 350, 300);
        g.fillRect(350, 300, 50, 100);
        g.fillRect(550, 300, 50, 100);
        g.fillRect(440, 300, 75, 200);

        addMouseListener(this);

        if(rect1Clicked || rect2Clicked || rect3Clicked)
        {
            g.setColor(Color.GRAY);
            g.clearRect(rect1x, rect1y, rect1w, rect1h);
            g.fillRect(rect1x, rect1y, rect1w, rect1h);
            g.setColor(Color.GRAY);
            g.clearRect(rect2x, rect2y, rect2w, rect2h);
            g.fillRect(rect2x, rect2y, rect2w, rect2h);
            g.setColor(Color.GRAY);
            g.clearRect(rect3x, rect3y, rect3w, rect3h);
            g.fillRect(rect3x, rect3y, rect3w, rect3h);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) 
    {
        rect1x = 350;
        rect1y = 300;
        rect1w = 350;
        rect1h = 300;
        rect2x = 550;
        rect2y = 300;
        rect2w = 50;
        rect2h = 100;
        rect3x = 440;
        rect3y = 300;
        rect3w = 75;
        rect3h = 200;
        mX = e.getX();
        mY = e.getY();

        if(mX > rect1x && mX < rect1x + rect1w && mY > rect1y && mY < rect1y + rect1h)
        {
            rect1Clicked = true;
        }
        else
        { 
            rect1Clicked = false;
        }
        if(mX > rect2x && mX < rect2x + rect2w && mY > rect2y && mY < rect2y+rect2h)
        {
            rect2Clicked = true;
        }
        else
        {
            rect2Clicked = false;
        }
        if(mX > rect3x && mX < rect3x + rect3w && mY > rect3y && mY < rect3y + rect3h)
        {
            rect3Clicked = true;
        }
        else
        {
            rect3Clicked = false;
        }
    }

}

Thanks in advance for any suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:47:13+00:00Added an answer on June 12, 2026 at 2:47 am

    You’re really making life very difficult for yourself. The Java Graphics API has a number of classes specifically suited for solving this problem.

    As a general rule of thumb. NEVER override any of the paint methods of top level containers. Use an appropriate component, such as JPanel or JComponent.

    Where possible, override the paintComponent method instead.

    As HoverCraft has pointed out, DON’T modify the UI from within the paint methods, this includes adding listeners. The paint method will get called lots of times, meaning each time it’s called, you will register yet ANOTHER listener…

    You will want to start by having a read through the 2D Graphics trail and the Performing Custom Painting trail

    While the example below uses a JFrame, the basic principles apply.

    public static void main(String[] args) {
    
        EventQueue.invokeLater(new Runnable() {
    
            @Override
            public void run() {
    
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
    
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.setSize(200, 200);
                frame.add(new HousePane());
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
    
            }
        });
    
    }
    
    public static class HousePane extends JPanel {
    
        private List<Rectangle2D> parts = new ArrayList<Rectangle2D>(25);
        private List<Rectangle2D> selected = new ArrayList<Rectangle2D>(25);
    
        public HousePane() {
    
            parts.add(new Rectangle2D.Float(10, 10, 50, 50));
            parts.add(new Rectangle2D.Float(60, 10, 50, 50));
            parts.add(new Rectangle2D.Float(10, 60, 50, 50));
            parts.add(new Rectangle2D.Float(60, 60, 50, 50));
    
            addMouseListener(new MouseAdapter() {
    
                @Override
                public void mouseClicked(MouseEvent e) {
                    selected.clear();
                    for (Rectangle2D rect : parts) {
                        if (rect.contains(e.getPoint())) {
                            selected.add(rect);
                        }
                    }
    
                    // You could require the user to click the shape again
                    // to deselect by doing something like...
                    //for (Rectangle2D rect : parts) {
                    //    if (rect.contains(e.getPoint())) {
                    //        if (selected.contains(rect)) {
                    //            selected.remove(rect);
                    //        } else {
                    //            selected.add(rect);
                    //        }
                    //    }
                    //}
                    repaint();
                }
    
            });
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.BLUE);
            for (Rectangle2D rect : selected) {
    
                g2d.fill(rect);
    
            }
            g2d.setColor(Color.BLACK);
            for (Rectangle2D rect : parts) {
    
                g2d.draw(rect);
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an array which has BIG numbers and small numbers in it. I
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.