如何从不同类的子例程中引用Graphics2D对象? [英] How do I reference a Graphics2D object from subroutine in a different class?

查看:87
本文介绍了如何从不同类的子例程中引用Graphics2D对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在外部课程中构建几个绘图,并根据需要调用它们。我尝试过使用一组形状,效果很好,但我不能使用文字。我尝试使用缓冲图像,但这似乎太耗电了。



我认为我需要做的是在另一个类中创建一个子程序,我调用子程序将图形添加到我的Graphics2D对象。我不确定我到底是怎么做到的。我知道在VB.Net中我会使用ByRef,这可能会有效。



我知道以下代码在这么多级别上是错的,我想知道怎么做我在Java中实现了相同的原则。 ByRef VARIABLE as OBJECT 是一个VB.NET的东西,我在这里用它来说明我要完成的事情。



Hi,

I would like to have several drawings constructed in an external class and call them to be drawn as needed. I have tried using an array of shapes, which works very well but I can't use text. I tried using buffered image, however this seems to be too power hungry.

What I think I need to do is create a subroutine in another class where I call the subroutine to add graphics to my Graphics2D object. I'm not sure how exactly I achieve this. I know in VB.Net I would use ByRef and that would probably work.

I know the following code is wrong on so many levels, I want to know how do I achieve the same principle in Java. ByRef VARIABLE as OBJECT is a VB.NET thing, I'm using it here to illustrate what I'm to accomplish.

public class GraphicFunctions {

   public void DrawingA(ByRef gfx2d as Graphics2D){
      gfx2d.DrawLine(1,2,3,4);
      gfx2d.DrawLine(5,6,7,8);
   }
   
   public void DrawingB(ByRef gfx2d as Graphics2D){
      gfx2d.DrawLine(4,3,2,1);
      gfx2d.DrawLine(9,10,11,12);
   }

}

Public class GenerateDrawing {
   private void doDrawing(Graphics g){

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.blue);
   }
}





提前致谢



Thanks in advance

推荐答案

这是我用Java编写的第二周编码,我不知道这是如何工作的,也不知道为什么会这样,但是它的工作原理如何。根据几个站点,Java不支持ByRef,而是使用ByVal。根据我刚刚完成的事情,似乎恰恰相反。



从好处来看,这似乎比使用缓冲图像的资源使用了1/4 ,文字,颜色和一切都有效。



This is my Second Week coding in Java, I'm not sure how or why this works, but it works some how. According to several sites, Java does not support ByRef, but works with ByVal instead. According to what I have just done it seems to be the exact opposite.

On the upside this seems to use 1/4 of the resources than drawing a buffered image does, text, colors and everything works.

public class GraphicFunctions {
 
   public void DrawingA(Graphics2D g2d){
      gfx2d.DrawLine(1,2,3,4);
      gfx2d.DrawLine(5,6,7,8);
   }
   
   public void DrawingB(Graphics2D g2d){
      gfx2d.DrawLine(4,3,2,1);
      gfx2d.DrawLine(9,10,11,12);
   }
 
}
 
Public class GenerateDrawing {

   GraphicFunctions Gf = new GraphicFunctions();

   private void doDrawing(Graphics g){
 
        Graphics2D g2d = (Graphics2D) g;
 
        g2d.setColor(Color.blue);
        
        Gf.DrawingA(g2d);
        Gf.DrawingB(g2d);
   }
}


这篇关于如何从不同类的子例程中引用Graphics2D对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆