如何使用单独的方法调用油漆组件方法并绘制矩形?
我正在使用Swing创建一个GUI,每次运行方法都会绘制一个新的矩形。 我想使用会增加的变量并继续在系列中制作新的矩形。 我的目标是给出一个链接列表的视觉表示,并强调链接列表的头部指向下一个项目的概念,该项目指向下一个,依此类推。
我是Java的基本知识的初学者,而我才刚刚开始摆动并创建用户界面。
void insert(int i, int j) {
//some code which would create a new rectangle and add to my GUI.
}
// this is my panel class
public class MyPanel extends JPanel{
MyPanel() {
this.setPreferredSize(new Dimension(500, 500));
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setPaint(Color.blue);
g2D.setStroke(new BasicStroke(5));
g2D.drawRect(i, j, 100, 50);
}
}
I am using Swing to create a GUI which would draw a new rectangle every time a method runs.
I want to use variables that would get incremented and continue making new rectangles in a series.
My goal is to give a visual representation of a linked list and to emphasize on the concept that a linked list has a head which points to the next item which points to the next and so on.
I am a beginner with basic knowledge of Java, and I'm just getting started with Swing and creating User interfaces.
void insert(int i, int j) {
//some code which would create a new rectangle and add to my GUI.
}
// this is my panel class
public class MyPanel extends JPanel{
MyPanel() {
this.setPreferredSize(new Dimension(500, 500));
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setPaint(Color.blue);
g2D.setStroke(new BasicStroke(5));
g2D.drawRect(i, j, 100, 50);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做什么:
insert(int,int)
方法insert(int,int)
方法在Java 14中介绍了(*)记录(*)记录,它们可用于以简短的方式创建数据类。
他们会自动生成getters,
equals()
,hashcode()
和toString()
- method。对于此示例,可以将记录重写为:(
我们不需要
equals()
,hashcode()
and code> tostring())What you can do:
insert(int,int)
methodinsert(int,int)
methodpaintComponents()
-method can loop over the saved values and display them(*) records were introduced in Java 14 and they can be used to create data classes in a short way.
They automatically generate Getters, the
equals()
,hashCode()
andtoString()
-method.For this example, the record could be be rewritten to:
(we don't need
equals()
,hashCode()
andtoString()
)