java homework draw house applet

时间:2012-09-29 19:46:15

标签: java graphics applet

我正在做一项家庭作业,在一个java小程序中画一个房子。房子在一个大的主矩形内有三个矩形,代表一扇门和两扇窗户。我需要窗户在点击时改变颜色,我已经达到了我已经绘制房子的位置并且还绘制了门窗,但是我无法通过单击其中的鼠标来更改它们的颜色。我在确定为什么会出现这种情况时遇到了一些麻烦。

总结一下,房子被画了;门和窗口矩形绘制并填充黑色。当点击任何窗口或门矩形时,不会发生任何错误,也不会改变颜色。

代码如下:

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;
        }
    }

}

提前感谢任何建议。

1 个答案:

答案 0 :(得分:0)

你真的让自己的生活变得非常困难。 Java Graphics API有许多专门用于解决此问题的类。

作为一般经验法则。切勿覆盖顶级容器的任何paint方法。使用适当的组件,例如JPanelJComponent

如果可能,请改写paintComponent方法。

正如HoverCraft指出的那样,请勿在{{1​​}}方法中修改UI,包括添加监听器。 paint方法会被多次调用,这意味着每次调用它时,你都会注册另一个听众......

您需要先阅读2D Graphics路径和Performing Custom Painting路径

虽然以下示例使用paint,但基本原则适用。

JFrame