传递绘制的图形

编程入门 行业动态 更新时间:2024-10-25 20:28:28
本文介绍了传递绘制的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

传递绘制的图形

本文发布于:2023-10-12 01:31:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!