如何创建使用将类中的方法绘制到另一个类的JPanel中

时间:2023-02-09 16:01:30

I have 2 classes, one class is my GUI frameviewer. The other is a class that I am trying to use with my project. The class LabeledBar provides a draw method. I will have an ArrayList of the LabeledBars in my FrameViewer class. I want to iterate through that list and create a new Panel holding these bars. I can't quite figure out how to draw these bars onto that frame.

我有2个类,一个类是我的GUI frameviewer。另一个是我试图用于我的项目的类。 LabeledBar类提供了draw方法。我将在FrameViewer类中有一个LabeledBars的ArrayList。我想遍历该列表并创建一个包含这些栏的新面板。我无法弄清楚如何在这个框架上绘制这些条形图。

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);
    }
}

This is my a method from my other class in attempt to create a new JFrame that includes the labeledBars in them.

这是我的另一个类的方法,试图创建一个新的JFrame,其中包含labeledBars。

 private void paintBars()
{
    Graphics2D g = (Graphics2D)labeledBarsFrame.getGraphics();

    for (LabeledBar element: bars)
    {
        element.draw(g);
    }
    //labeledBarsFrame.add(g);
}

1 个解决方案

#1


3  

I want to iterate through that list and create a new Panel holding these bars. I can't quite figure out how to draw these bars onto that frame.

我想遍历该列表并创建一个包含这些栏的新面板。我无法弄清楚如何在这个框架上绘制这些条形图。

Check out Custom Painting Approaches.

查看自定义绘画方法。

The DrawOnComponent examples should get you started in the right direction. It shows how to paint custom Objects found in an ArrayList.

DrawOnComponent示例应该让您从正确的方向开始。它显示了如何绘制在ArrayList中找到的自定义对象。

Basically you need to create a JPanel and override the paintComponent(...) to iterate through your ArrayList and invoke the draw(...) method on each of your Objects. The panel is then added to the frame.

基本上你需要创建一个JPanel并覆盖paintComponent(...)来迭代你的ArrayList并在你的每个对象上调用draw(...)方法。然后将面板添加到框架中。

#1


3  

I want to iterate through that list and create a new Panel holding these bars. I can't quite figure out how to draw these bars onto that frame.

我想遍历该列表并创建一个包含这些栏的新面板。我无法弄清楚如何在这个框架上绘制这些条形图。

Check out Custom Painting Approaches.

查看自定义绘画方法。

The DrawOnComponent examples should get you started in the right direction. It shows how to paint custom Objects found in an ArrayList.

DrawOnComponent示例应该让您从正确的方向开始。它显示了如何绘制在ArrayList中找到的自定义对象。

Basically you need to create a JPanel and override the paintComponent(...) to iterate through your ArrayList and invoke the draw(...) method on each of your Objects. The panel is then added to the frame.

基本上你需要创建一个JPanel并覆盖paintComponent(...)来迭代你的ArrayList并在你的每个对象上调用draw(...)方法。然后将面板添加到框架中。