java - (JAVA)如何区分二维对象
问题描述
一个简单的问题。我尝试使用图形 2d 来制作对象。我的问题是我们如何区分两个对象之间的变量。例如下面的代码;
public void paintComponent(Graphics g) {
super.paintComponent(g);
r1 = new Rectangle(10, 10, 50, 30);
r2 = new Rectangle(450, 10, 50, 30);
g.setColor(Color.BLUE);
g.fillRect(10, 10, 50, 30);
g.setColor(Color.RED);
g.fillRect(450, 10, 50, 30);
}
public void actionPerformed(ActionEvent e) {
if (r1.intersects(r2)) {..............
}
从上面的代码中,我有 2 个矩形变量 r1 和 r2,我可以在 actionPerformed 方法中执行“if (r1.intersects(r2))”。
但是使用Graphic2d时下面的代码怎么样,我如何区分这两个三角形,以便它可以适应“if(r1.intersects(r2))”,因为只有一个变量“lukis”只存在并且它用于做2个三角形。谢谢你。
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D lukis = (Graphics2D) g;
int[] x = {100, 200, 300};
int[] y = {300, 100, 300};
lukis.setColor(Color.RED);
lukis.drawPolygon(x, y, 3);
int[] x1 = {600, 700, 800};
int[] y1 = {300, 100, 300};
lukis.setColor(Color.RED);
lukis.drawPolygon(x1, y1, 3);
}
解决方案
推荐阅读
- wordpress - 从相机 exif 信息到 wordpress 时间的解析时间
- javascript - 如何增加 react-scrollbar-custom 的大小
- reporting-services - 对表格向导报告中返回的值应用过滤器(Reportbuilder
- json - futurebuilder 和 http json 的颤振错误
- azure - 在 Azure 数据工厂中使用服务主体/应用程序进行 OAuth2 身份验证
- docker - docker compose 已更改目录权限
- c# - Azure Functions 中的依赖注入无法为类型化的 HttpClient 调用配置委托?
- postgresql - Cloud SQL - PostgreSQL - 由于缺少超级用户权限,导入失败
- database - 第三方 API 集成 - 如何构建数据库
- dafny - 在归纳数据类型中定义字典顺序