Question

I'm trying to pass a graphics element up the chain. I need to have the ability to draw a number of different die and return the correct one for each instance. I can get this to compile but I don't know if i'm doing this correctly. I want to pass the graphics component to a panel to be displayed.

My shortened paint class

import java.awt.*;
import javax.swing.*;


class DiePaint extends JPanel
{
Graphics g;

public Graphics dieSwitch(int inInt)
{
    return die1();
}
private Graphics die1()
{
    //reset drawing
    repaint();

    //draw a die with 1 pip
    g.setColor(Color.BLACK);
    g.drawRect(0,0,50,50);
    g.drawOval(24,24,2,2);
    g.fillOval(24,24,2,2);

    //return graphic
    return g;
}

}

A method in my other class i'm trying to use to call it.

private void setDie()
{
    //set die labels
    die1P.paint(drawDie.dieSwitch(game.getDie(0)));
    }
Was it helpful?

Solution

"I want to pass the graphics component to a panel to be displayed."

No, you don't.

You need to see how to Perform Custom Painting. You're going to need a paintComponent method in your panel

@Override
protected void paintComponent(Graphic s) {
    super.paintComponent(g);
    // draw here
}

You don't explicitly call paint like you are doing here die1P.paint(drawDie.dieSwitch

If you want to be able to set what is being painted, you can have a Die object that you use to draw. Something like

class Die {
    public void draw(Graphics g) {
        // draw die here
    }
}

Then in your panel class have a setter for the Die object, that will repaint the new Die. You will probably want to have a way to differentiate each die. Pass some arguments to a constructor to do that. Then paint the one die.

public class DiePanel extends JPanel {
    private Die die;
    public void setDie(Die die) {
        this.die = die;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (die != null) {
            die.draw(g);
        }
    }
}

You may also, instead, want to make Die an interface, so you can implement difference Die objects, like DieOne, DieTwo, etc. Something like

public interface Die {
    void Draw(Grapchis g);
}

public class DieOne {
    @Override
    public void draw(Graphics g) {
        // draw die one
    }
}

When you call setDie(), you can pass a specific Die, like

DieOne dieOne = new DieOne();
...
diePanel.setDie(dieOne);

OTHER TIPS

Yes you can pass the drawn component by using BufferedImage....first you draw the graphic on this BufferedImage then pass this image and then draw this image to the Panel...Here is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;


class DiePaint extends JPanel
{
int width=500; int height=500;  //you can change as requred
BufferedImage buffg=new BufferedImage(width,height , BufferedImage.TYPE_INT_ARGB);
Graphics g;

public BufferedImage dieSwitch(int inInt)   //change return type to BufferedImage
{
    return die1();
}
private BufferedImage die1()     // change return type to BufferedImage
{
    g=buffg.getGraphics();   //get Graphics from buffg..
    //reset drawing
    repaint();

    //draw a die with 1 pip   
    g.setColor(Color.BLACK);   //draw all the component on buffg using its Graphics g
    g.drawRect(0,0,50,50);
    g.drawOval(24,24,2,2);
    g.fillOval(24,24,2,2);

    //return graphic
    return buffg;
}
}

now once you get this "buffg" now you can draw it on any panel.....

JPanel p=new JPanel(){
    @Override
    public void paint(Graphics g){
         g.drawImage(drawDie.dieSwitch(game.getDie(0)));   //get & draw Image
    }

};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top