简体   繁体   English

在paint()方法之外使用Graphics对象进行分形java

[英]Using Graphics object outside of the paint() method for a fractal java

I am trying to draw a fractal tree, and currently my design gleamed from other people's work is using graphics objects outside of the paint() method for recursion. 我正在尝试绘制一个分形树,目前我的设计从其他人的工作中闪闪发光,使用paint()方法之外的图形对象进行递归。 Will this result in anything at all? 这会导致什么吗?

您可以从BufferedImage获取Graphics对象(甚至是支持数组)并在其上绘制,然后在paintComponent()中绘制图像。

You need to pass the Graphics context as a parameter to your drawMethod(Graphics g) , then invoke than method withing the paint method. 你需要通过Graphics上下文作为参数传递给您的drawMethod(Graphics g)然后调用方法相比的withing paint方法。 drawMethod(g); , g being the Graphics context of the paint method gpaint方法的Graphics上下文

public class SomeClass {
    public void drawMethod(Graphics g) {
        g.drawString("Hello World", 50, 50);
    }
}

public class DrawPanel extends JPanel {
    SomeClass someClass = new SomeClass();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        someClass.drawMethod(g);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM