java中编写getElementAt方法实现下面功能 takes a position in the window and returns the graphical object at that location, if any

时间: 2024-03-22 14:39:08 浏览: 16
您可以使用Java的Swing库中的JComponent类中的getElementAt方法来实现该功能。以下是可能的实现方法: ```java public GraphicalObject getElementAt(int x, int y) { for (int i = getComponentCount() - 1; i >= 0; i--) { Component comp = getComponent(i); if (comp.contains(x, y)) { if (comp instanceof GraphicalObject) { return (GraphicalObject) comp; } else { return null; } } } return null; } ``` 该方法首先遍历JComponent中的所有子组件,从上到下,以便找到最上面的对象。然后,它使用Component类中的contains方法来检查该组件是否包含指定的坐标。如果是,则检查该组件是否是GraphicalObject的实例。如果是,则返回该组件,否则返回null。如果没有找到任何组件,则返回null。
相关问题

下面这段话要我做什么If you look in Chapter 9 (page 299) at the methods that are defined at the GraphicsProgram level, you will discover that there is a method public GObject getElementAt(double x, double y) that takes a position in the window and returns the graphical object at that location, if any. If there are no graphical objects that cover that position, getElementAt returns the special constant null. If there is more than one, getElementAt always chooses the one closest to the top of the stack, which is the one that appears to be in front on the display.

这段话是在提示你,如果你需要获取屏幕上特定位置的图形对象,可以使用 GraphicsProgram 类中定义的 getElementAt(double x, double y) 方法。该方法接受一个在窗口中的位置并返回该位置上存在的图形对象。如果没有图形对象覆盖该位置,则返回特殊常量 null。如果存在多个对象,则 getElementAt 方法总是选择最靠近堆栈顶部的对象,即显示在屏幕最前面的对象。你可以在第9章第299页上找到该方法的定义和更多详细信息。

写一个返回对象是Graphics的getElementAt方法

假设你是在编写一个图形界面的程序,需要实现一个方法`getElementAt(int x, int y)`,该方法可以返回在(x, y)点上的图形元素,如果该点上没有图形元素,则返回null。可以按照以下步骤实现该方法: 1. 获取当前窗口的所有组件,可以使用Container类的`getComponents()`方法。 2. 遍历所有组件,判断哪些组件是图形组件,这里假设图形组件是指实现了GraphicalObject接口的组件。 3. 对于每个图形组件,判断该组件是否包含在(x, y)点上,可以使用组件的`contains()`方法。 4. 如果找到了包含在(x, y)点上的图形组件,就返回该组件的Graphics对象,可以使用组件的`getGraphics()`方法。 下面是一个简单的实现示例: ```java public GraphicalObject getElementAt(int x, int y) { // 获取当前窗口的所有组件 Component[] components = getContentPane().getComponents(); // 遍历所有组件 for (Component component : components) { // 判断哪些组件是图形组件 if (component instanceof GraphicalObject) { GraphicalObject graphicalObject = (GraphicalObject) component; // 判断该组件是否包含在(x, y)点上 if (graphicalObject.contains(x, y)) { // 如果找到了包含在(x, y)点上的图形组件,就返回该组件的Graphics对象 return graphicalObject.getGraphics(); } } } // 如果没有找到包含在(x, y)点上的图形组件,则返回null return null; } ``` 在上面的示例中,我们首先获取当前窗口的所有组件,并遍历所有组件,判断哪些组件是图形组件。对于每个图形组件,我们使用`contains()`方法判断其是否包含在(x, y)点上,如果包含,则返回该组件的Graphics对象。如果没有找到包含在(x, y)点上的图形组件,则返回null。

相关推荐

Also create a ControllerCreate class that extends Controller.The create method takes as arguments the name of a new library user, a number of books (as a string), and an integer representing the role of user to create (where the integer 0 means a lender and the integer 1 means a borrower). The create method of the controller then transforms the book number from a string to an integer (using the Integer.parseInt static method), creates an object from the correct class (based on the role specified by the user input: lender or borrower) and calls the addUser method of the library to add the new user object to the library. • If no exception occurs then the create method of the controller returns the empty string. • If the constructor of the Borrower class throws a NotALenderException then the create method of the controller must catch this exception and return as result the error message from the exception object. • If the parseInt method of the Integer class throws a NumberFormatException (because the user typed something which is not an integer) then the create method of the controller must catch this exception and return as result the error message from the exception object. Modify the run method of the GUI class to add a ViewCreate view that uses a ControllerCreate controller and the same model as before (not a new model!) Do not delete the previous views. Note: if at the end of Question 7 you had manually added to your library (model object) some users for testing, then you must now remove those users from the run method of the anonymous class inside the GUI class. You do not need these test users anymore because you have now a graphical user interface to create new users! Run your GUI and check that you can correctly use the new view to create different users for your library, with different types of roles. • Check that, when you create a new user, the simple view is automatically correctly updated to show the new total number of books borrowed by all users. • Also use the “get book” view to check that the users are correctly created with the correct names and correct number of books. • Also check that trying to create a borrower with a negative number of books correctly shows an error message. Also check that trying to create a user with a number of books which is not an integer correctly shows an error message (do not worry about the content of the error message). After you created a new user, you can also check whether it is a lender or a borrower using the “more book” view to increase the number of books of the user by a big negative number: • if the new user you created is a lender, then increasing the number of books by a big negative value will work and the number of books borrowed by the user will just become a larger value (you can then check that using the “get book” view); • if the new user you created is a borrower, then increasing the number of books by a big negative value will fail with an error message and the number of books borrowed by the user will not change (you can then check that using the “get book” view). 完成符合以上要求的java代码

最新推荐

recommend-type

CentOS 7.6 64位操作系统安装“GNOME”桌面系统 Linux服务器 (附软件下载)

CentOS 7.6 64位操作系统安装“GNOME”桌面系统 Linux服务器 这两天我刚入门服务器,听我哥介绍“GNOME”桌面系统可视化Linux服务器非常好用,但是去...yum groupinstall GNOME Desktop Graphical Administration Tools
recommend-type

SCI投稿Title page, highlights, Cover letter模板,适合IEEE,elsevier等各大期刊

SCI投稿中用到的Title page, highlights, Cover letter模板,适合IEEE,elsevier等各大期刊。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

get() { return this.photoState },

这是一个 JavaScript 中的方法定义,它定义了一个名为 `get` 的方法。这个方法没有参数,它返回了 `this.photoState`。在这个方法中,`this` 是指当前对象,而 `photoState` 是该对象的一个属性。通常情况下,`get` 方法用于获取对象的属性值,并且可以在获取属性值之前进行一些逻辑操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB柱状图在数据分析中的作用:从可视化到洞察

![MATLAB柱状图在数据分析中的作用:从可视化到洞察](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB柱状图概述** 柱状图是一种广泛用于数据可视化的图表类型,它使用垂直条形来表示数据中不同类别或组别的值。在MATLAB中,柱状图通过`bar`函数创建,该函数接受数据向量或矩阵作为输入,并生成相应的高度条形。 柱状图的优点在于其简单性和易于理解性。它们可以快速有效地传达数据分布和组别之间的比较。此外,MATLAB提供了广泛的定制选项,允许用户调整条形颜色、
recommend-type

id=1 and 1=3 union select 1,group_concat(uismkhbuow_name) from xtfrfdxcsk.uismkhbuow ;哪错了

这是一个 SQL 注入攻击的语句,其中 `1=3` 是永远不成立的条件,所以整个语句都是错误的。这个语句的目的是通过注入一段查询语句,来获取 `xtfrfdxcsk.uismkhbuow` 表中的数据。但是这样的操作是非法的,不仅会破坏数据库的完整性和安全性,还可能触犯法律。建议不要尝试进行 SQL 注入攻击。