How to pass more than one parameter in a paint () method in Java?

1

I was developing a project where I need to override the paint () method, but the paint () method only receives a Graphics object as a parameter. What I need to do is create a paint method that takes two parameters and uses Image objects belonging to the Other object to draw on the screen, for example '

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class Desenho extends JPanel{

       public void paint(Graphics g, Outro o){
           Graphics2D g2d = (Graphics2D)g;
           g2d.drawImage(o.getImagem(), o.getX(), o.getY(), null);
       }
}

After this I would need to call this method and pass the parameters.

Outro o2 = new Outro();
Graphics gh = new Graphics();     // Não posso criar um Graphics assim pois Graphics é classe    abstrata.
paint(gh, o2);

The problem is that I can not create a Graphics object outside the method and thus I can not create the method with more than one argument.

If anyone knows how to do this or has a different idea that reaches the same result and can help me, thank you in advance.

    
asked by anonymous 07.12.2016 / 18:53

1 answer

0

One way around this is to make Outro into a class property.

And when instantiating the class passes it in the constructor!

Here's an example:

public class Desenho  extends JFrame{

    private Outro o;

    public Desenho(Outro o) {
        this.o = o;
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method s
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(o.getImagem(), o.getX(), o.getY(), null);
        super.paint(g);
    }
}

I do not know if this form meets the scope of your application, but it's a solution!

    
07.12.2016 / 19:47