Java帮助:重叠的矩形会在矩形重叠的地方绘制新的颜色 [英] Java help: overlapping rectangles that paint a new color where the rectangles overlap

查看:73
本文介绍了Java帮助:重叠的矩形会在矩形重叠的地方绘制新的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在处理Java作业,我必须创建一个Rectangle类,该类在绘制重叠矩形的程序中使用,并且在矩形重叠的情况下,将使用新颜色绘制一个新矩形.我已经包含了一个到硬件描述的链接,因为我认为,比起我来解释它,最简单的方法就是让您查看一下..

So I'm working on a Java assignment where I had to create a Rectangle class that was utilized in a program that draws overlapping rectangles and where the rectangles overlap, a new rectangle is drawn, with a new color. I have included a link to the HW description as I figured it be easiest to just let you look over that than me trying to explain it..http://myslu.stlawu.edu/~ltorrey/courses/spring13/cs219/hw/hw6.html

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

I am currently stuck on creating the getOverlap method, which draws the new overlapped rectangle. I had to create a draw method that obviously draws the rectangles and am confused on whether the getOverlap method defines new parameters for the variables that were previously defined and then reroute into the draw method? Any help would be greatly appreciated.

推荐答案

只需稍微巧妙地使用Area,您就可以获取Graphics2D API来为您完成此任务.

With a little clever use of Area you can get the Graphics2D API to do it for you.

基本上,我创建一个Area,它是对两个矩形进行异或"运算的结果.然后,我从Area中减去此值,这是将两个矩形相加的结果

Basically, I create an Area that is the result of an exclusiveOR operation on the two rectangles. I then subtract this from an Area that is the result of adding the two rectangles together

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

}

这篇关于Java帮助:重叠的矩形会在矩形重叠的地方绘制新的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆