传递绘制的图形 [英] Passing a drawn graphic

查看:47
本文介绍了传递绘制的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图形元素向上传递.我需要有能力绘制多个不同的模具,并为每个实例返回正确的模具.我可以将其编译,但是我不知道我是否正确地进行了编译.我想将图形组件传递到要显示的面板.

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.

我缩短的绘画课

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."

不,你不要.

您需要了解如何执行自定义绘画.您将需要在面板中使用 paintComponent 方法

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
}

您不会像在此处那样显式调用 paint die1P.paint(drawDie.dieSwitch

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

如果希望能够设置要绘制的内容,则可以使用一个 Die 对象进行绘制.像

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

然后在您的面板类中为 Die 对象提供一个 setter ,该对象将重新绘制新的 Die .您可能需要一种方法来区分每个骰子.将一些参数传递给构造函数以执行此操作.然后绘制一个模具.

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

您可能还想将 Die 用作接口,以便可以实现不同的 Die 对象,例如 DieOne DieTwo 等.

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

调用 setDie()时,可以传递特定的 Die ,例如

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

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

这篇关于传递绘制的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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