Question: Write a program that draws a house. When the user clicks on the door,
ID: 3641493 • Letter: Q
Question
Question: Write a program that draws a house. When the user clicks on the door, or windows they should close. see page 927 problem 2***NOTE: If the door/window is closed and you click again it should OPEN. ************
The code attached is the house drawing i have done with the mouse events already specified. My problem is finding out how to specify the range for each window and door so that if the mouse is pressed or clicked in that range it closes or opens.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class HouseDemo extends JApplet
{
private JPanel panel;
private int currentX;
private int currentY;
public HouseDemo()
{
getContentPane().setBackground(Color.white);
panel = new JPanel();
add(panel);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
//House Frame
g.drawRect(100,250,350,250);
//House Roof
g.drawLine(100,250,280,125); //Left Side
g.drawLine(280, 125, 450, 250); //Right Side
//House Door
g.drawRect(240, 400, 75, 100); //doorframe
g.drawOval(295, 450, 10, 10); //doorknob
//House Window Left Side
g.drawRect(140, 300, 75, 75);
g.drawLine(140, 340, 215, 340); //Horizontal Line
g.drawLine(177,300,177,375); //Vertical Line
//House Window Right Side
g.drawRect(345, 300, 75, 75);
g.drawLine(345, 337, 420, 337); //Horizontal Line
g.drawLine(382, 300, 382, 375); //Vertical Line
}
private class MyMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
currentX = e.getX();
currentY = e.getY();
}
public void mousePressed(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
//NO USE
}
public void mouseExited(MouseEvent e)
{
//NO USE
}
public void mouseReleased(MouseEvent e)
{
//NO USE
}
}
}
Explanation / Answer
Use the co-ordinates for Window and Door in your program and add perimeter with +100. If getX or getY falls in that range you can trigger mouse event. I hope that helped. If not let me know.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.