简体   繁体   English

传递绘制的图形

[英]Passing a drawn graphic

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)));
    }

"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您将需要在面板中使用paintComponent方法

@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你没有像你在这里做的那样明确调用paint 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.如果您希望能够设置正在绘制的内容,您可以拥有一个用于绘制的Die对象。 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 .然后在您的面板类中为Die对象setter一个setter ,它将重新绘制新的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相反,您可能还想让Die成为一个接口,这样您就可以实现不同的Die对象,例如DieOneDieTwo等。

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当您调用setDie() ,您可以传递特定的Die ,例如

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

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:是的,您可以使用 BufferedImage 传递绘制的组件....首先在此 BufferedImage 上绘制图形,然后传递此图像,然后将此图像绘制到面板...这是代码:

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.....现在一旦你得到这个“buffg”,你就可以在任何面板上绘制它......

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

};

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

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