1

所以我正在处理一个 Java 任务,我必须创建一个 Rectangle 类,该类在一个绘制重叠矩形的程序中使用,并且在矩形重叠的地方,一个新的矩形被绘制出来,具有新的颜色。我已经包含了硬件描述的链接,因为我认为让你看一下比我试图解释它最简单.. http://myslu.stlawu.edu/~ltorrey/courses/spring13/cs219/hw /hw6.html

我目前坚持创建 getOverlap 方法,该方法绘制新的重叠矩形。我必须创建一个明显绘制矩形的 draw 方法,并且对 getOverlap 方法是否为先前定义的变量定义新参数然后重新路由到 draw 方法感到困惑?任何帮助将不胜感激。

4

3 回答 3

5

通过一点点巧妙的使用,Area您可以获得Graphics2DAPI 来为您做这件事。

基本上,我创建了Area一个对两个矩形进行异或运算的结果。Area然后我从将两个矩形加在一起的结果中减去这个

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OverlappingRectangles {

    public static void main(String[] args) {
        new OverlappingRectangles();
    }

    public OverlappingRectangles() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            Rectangle r1 = new Rectangle(0, 0, 150, 150);
            Rectangle r2 = new Rectangle(50, 50, 150, 150);

            g2d.setColor(Color.RED);
            g2d.fill(r1);
            g2d.setColor(Color.BLUE);
            g2d.fill(r2);

            Area a1 = new Area(r1);
            a1.exclusiveOr(new Area(r2));
            Area a2 = new Area(r2);
            a2.add(new Area(r1));
            a2.subtract(a1);

            g2d.setColor(Color.GREEN);
            g2d.fill(a2);

            g2d.dispose();
        }
    }

}
于 2013-03-20T03:36:50.107 回答
3

使用 MadProgrammer 的代码,我发现了一些更简单的方法来做到这一点:

使用区域:

Area yellow = new Area(r1);
yellow.intersect( new Area(r2) );
g2d.setColor(Color.YELLOW);
g2d.fill(yellow);

或使用另一个矩形:

Rectangle green = r1.intersection(r2);
g2d.setColor(Color.GREEN);
g2d.fill(green);
于 2013-03-20T04:30:43.287 回答
2

如果java.awt.Rectangle允许,以下是您如何在实现中使用它的概述:

private static class Rectangle {
    private int x1, y1, x2, y2, r, g, b;
    private java.awt.Rectangle r1 = new java.awt.Rectangle();
    private java.awt.Rectangle r2 = new java.awt.Rectangle();
    private java.awt.Rectangle r3;

    public Rectangle(int x1, int y1, int x2, int y2, int r, int g, int b) {
        ...
    }

    private void draw(Graphics page) {
        ...
    }

    private boolean overlapsWith(Rectangle r) {
        r1.setBounds(...);
        r2.setBounds(...);
        return r1.intersects(r2);
    }

    private Rectangle getOverlap(Rectangle r) {
        r1.setBounds(...);
        r2.setBounds(...);
        r3 = r1.intersection(r2);
        return new Rectangle(...);
    }
于 2013-03-20T04:52:39.310 回答