Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

Java
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
Posted
Comments
[no name] 26-Apr-15 9:36am    
Instead of just down voting my Question which I believe to be a valid question, that I have explained to the best of my ability. Instead kindly inform me as to what is wrong with my question in the first place before just down voting it.

1 solution

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.

Java
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);
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900