Java 如何从一个类到另一个类中的JPanel创建use Draw方法
我有两个类,一个类是我的GUI frameviewer。另一个是我试图在我的项目中使用的类。LabeledBar类提供了一个draw方法。在FrameViewer类中,我将有一个LabeledBars的ArrayList。我想遍历该列表并创建一个包含这些条的新面板。我不太明白怎么把这些条画到那个框架上Java 如何从一个类到另一个类中的JPanel创建use Draw方法,java,swing,jframe,graphics2d,Java,Swing,Jframe,Graphics2d,我有两个类,一个类是我的GUI frameviewer。另一个是我试图在我的项目中使用的类。LabeledBar类提供了一个draw方法。在FrameViewer类中,我将有一个LabeledBars的ArrayList。我想遍历该列表并创建一个包含这些条的新面板。我不太明白怎么把这些条画到那个框架上 import java.awt.Color; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
/** LabeledBar is a rectangle with an interior label.
*
*
*/
public class LabeledBar
{
private int xLeft;
private int yTop;
private int width;
private int height;
private String label;
private Color color;
/** Construct this object from the specified dimensions.
* @param x x coordinate of the upper-left corner of this bar.
* @param y y coordinate of the upper-left corner of this bar.
* @param aWidth width of the bar in pixels.
* @param label the text to be displayed inside the bar.
* @param color desired color of the lines of the bar.
*/
public LabeledBar(int x, int y, int aWidth, String label, Color color)
{
xLeft = x;
yTop = y;
width = aWidth;
height = 20;
this.label = label;
this.color = color;
}
/** Draw this bar on the supplied graphics context.
* @param g2 the context on which to draw this bar.
*/
public void draw(Graphics2D g2)
{
Rectangle leftRectangle = new Rectangle(
xLeft, yTop,
width, height);
g2.setColor(color);
g2.draw(leftRectangle);
g2.drawString(label, xLeft+height/4, yTop+height*3/4);
}
}
这是我的另一个类中的一个方法,试图创建一个新的JFrame,其中包含labeledBars
private void paintBars()
{
Graphics2D g = (Graphics2D)labeledBarsFrame.getGraphics();
for (LabeledBar element: bars)
{
element.draw(g);
}
//labeledBarsFrame.add(g);
}
我想遍历该列表并创建一个包含这些条的新面板。我不太明白怎么把这些条画到那个框架上
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
/** LabeledBar is a rectangle with an interior label.
*
*
*/
public class LabeledBar
{
private int xLeft;
private int yTop;
private int width;
private int height;
private String label;
private Color color;
/** Construct this object from the specified dimensions.
* @param x x coordinate of the upper-left corner of this bar.
* @param y y coordinate of the upper-left corner of this bar.
* @param aWidth width of the bar in pixels.
* @param label the text to be displayed inside the bar.
* @param color desired color of the lines of the bar.
*/
public LabeledBar(int x, int y, int aWidth, String label, Color color)
{
xLeft = x;
yTop = y;
width = aWidth;
height = 20;
this.label = label;
this.color = color;
}
/** Draw this bar on the supplied graphics context.
* @param g2 the context on which to draw this bar.
*/
public void draw(Graphics2D g2)
{
Rectangle leftRectangle = new Rectangle(
xLeft, yTop,
width, height);
g2.setColor(color);
g2.draw(leftRectangle);
g2.drawString(label, xLeft+height/4, yTop+height*3/4);
}
}
退房
DrawOnComponent
示例应该让您从正确的方向开始。它显示了如何绘制在ArrayList中找到的自定义对象
基本上,您需要创建一个
JPanel
并覆盖paintComponent(…)
来迭代ArrayList
并在每个对象上调用draw(…)
方法。然后将面板添加到框架。查找M-V-C或模型视图控制器,并以这种方式构造程序。切勿使用通过调用组件上的getGraphics()
获得的图形对象进行绘制。而是在JPanel的paintComponent方法中绘制。阅读Swing Graphics教程,因为猜测这些东西是学习如何做的一个糟糕的方法。您可以在此处找到指向Swing教程和其他Swing资源的链接:问题解决后,您不会删除问题的文本。你“接受”帮助解决问题的答案,让人们知道问题已经解决。问题文本回滚。斯威格,请不要诋毁你的问题。本网站不是个人帮助网站,而是问答网站,在这里发布常见问题和答案,供所有人查看并从中获益。通过诋毁你的问题,你使它对未来的访问者毫无帮助。