JPanel 用特定颜色绘图
我发现这个类可以用不同的颜色绘制圆圈。每个圆圈的颜色是根据特定的颜色顺序确定的,该颜色顺序在结束时会迭代(已使用一次所有颜色)。我想修改它,使我能够单独确定每个圆圈的颜色(在 g.setColor 上)。换句话说,我希望能够将颜色部署为参数,并从另一个类中的另一个方法调用该方法。
public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
rect = circles.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
}
如果您发现我的问题很愚蠢,我想让您知道,令我担心的是该方法是从 JPanel 继承的,并且我不确定如何有效地覆盖它。
I have found this class that draws circles with different colors. The color of each circle is determined according to a specific order of colors which iterates as it comes to the end (having used all colors by one time). I want to modify this on a way that grants me the potential to determine individually the color (on g.setColor) for each circle. In other words, I want to be able to deploy the color as a parameter and to invoke the method from another method in another class.
public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
rect = circles.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
}
If you find my question stupid I would like to let you know that what worries me is the fact that the method is inherited from JPanel and I am not sure how to override it effectively.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
DrawingPane
似乎有一个名为circles
(原文如此)的矩形
列表。我不知道Rectangle
是您的类之一还是标准的java.awt.Rectangle
。如果它是您的类之一,则只需向该类添加一个
color
属性,并在迭代期间从中获取该属性即可。如果是标准的java.awt.Rectangle,则引入一个Circle类,包含一个矩形和一个颜色,并使用一个Circle列表而不是
矩形
列表。Your
DrawingPane
seems to have a list ofRectangle
namedcircles
(sic). I don't know ifRectangle
is one of your classes or the standardjava.awt.Rectangle
.If it's one of your class, then simply add a
color
attribute to this class, and get this attribute from it during your iteration.If it's the standard
java.awt.Rectangle
, then introduce aCircle
class, containing a Rectangle and a color, and use a list ofCircle
rather than a list ofRectangle
.然后您需要将颜色和形状存储为自定义类的属性。
自定义绘画方法显示了如何执行此操作的示例。我将使用
DrawOnComponent
示例作为起点。您的代码将变得更加简单,因为您不需要处理拖动。您需要做的就是创建一个 addCircle(...) 方法,它将圆的大小/位置/颜色作为参数。Then you need to store the Color and shape as properties of a custom class.
Custom Painting Approaches shows an example of how to do this. I would use the
DrawOnComponent
example as a starting point. Your code will be much simpler since you don't need to handle dragging. All you need to do is create an addCircle(...) method which will take the size/location/color of the circle as parameters.您在找这个吗?
您可以在单独的 .Java 文件中自由声明
MyCircle
和DrawingPane
类。我确信这会回答“我希望能够将颜色部署为参数并从另一个类中的另一个方法调用该方法。”
Are you looking for this ?
You are free to declare the classes
MyCircle
andDrawingPane
in separate .Java files.I am sure that this will give answer to " I want to be able to deploy the color as a parameter and to invoke the method from another method in another class."
我不确定你的意思,但如果你试图从外部类设置圆圈颜色,那么使用 setter 和(如果需要)getter 使数组成为该类的属性:
I'm not sure what you mean, but if you're trying to set the circle colors from an outside class, then make the array a property of the class with a setter and (if needed) a getter: