作业统计小程序:java作业画屋小程序:(drawhouse)

我正在做一个家庭作业,在一个 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;
        }
    }
}

提前感谢您的任何建议。

0

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

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

在可能的情况下,重写paintComponent方法。

正如 HoverCraft 指出的那样,不要从paint方法中修改 UI,这包括添加。paint方法将被调用很多次,这意味着每次调用时,您将注册另一个...

您将希望从读取2D Graphics跟踪和Performing Custom Painting跟踪开始

虽然的示例使用JFrame,但基本原则适用。

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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(330)
Freehand:FreeHand绘图 Java
上一篇
无法定位程序输入点user32.dll:PowerBuilder2021部署user32.dll错误
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(45条)