我怎么会去有关创建的paintComponent使用自定义的图形类? [英] How would I go about creating a custom graphics class for paintComponent to use?

查看:364
本文介绍了我怎么会去有关创建的paintComponent使用自定义的图形类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找各地更好地了解我所试图做的但有一点运气。我假设的答案比我实现简单多了。

I've been searching around to understand better what I am trying to do but with little luck. I'm assuming the answer is much simpler than I realize.

我提出的一类项目的棋盘游戏,并正在使用的JPanel显示图形(明显)。在paintComponent方法,而不是仅仅建立在一个地方的一切,我想我的外部化的图形到一个类有自己的,我可以在简单的paintComponent实例。试图让一切在逻辑上分离,并尽可能干净。

I am making a board game for a class project and am using a JPanel to display the graphics (obviously). In the paintComponent, rather than just create everything in one location I'd like to externalize my graphics into a class of there own which I can simply instantiate within paintComponent. Trying to keep everything logically separated and clean as possible.

我的Graphics2D看起来想,对于它的。新增()方法实现形状的类。如果这是正确的话,说我实现的形状,是什么的paintComponent寻找它时,我的画造型?我是否需要任何特定的方法,或者只是使用构造?或者应该都是我的图形类的仅仅是使用JPanel并使用自己的paintComponent方法?这感觉有点占用大量内存,我错了。

My Graphics2D looks like it wants a class that implements Shape for its .add() method. If this is correct then, say I implement Shape, what is paintComponent looking for when it paints my shape? Do I need any specific methods or perhaps just use the constructor? Alternatively should all of my graphics class just be using JPanel and using a paintComponent of their own? That feels a bit memory intensive and wrong to me.

所有我到目前为止看到的例子都一样...

All of the examples I've seen so far are all the same...

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;

  // Create shapes here which combine to form
  // a larger object...
  g2.add(a circle...);
  g2.add(a rectangle...);
  g2.add(other shapes...);
}

和我真的只是想要做的是...

And what I really just want to do is...

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;

  // Intantiate robust graphics object from external class.
  g2.add(graphicsObject);
}

所以我最终只是添加项目才能,游戏板表层,瓷砖最重要的是,然后游戏​​代币放置在瓷砖。

So I'd ultimately be just adding items in order, a game board surface layer, tiles on top of that, and then game tokens for placement in the tiles.

的智慧和卓越的环节词是最欢迎的。先谢谢了。

Words of wisdom and excellent links are most welcome. Thanks in advance.

推荐答案

一个绘画方法仅适用于绘画。它应该画你的类的属性。在这种情况下,性能将要绘制的形状。所以你类需要一个方法来指定要绘制的形状。你不想在绘画方法来创建形状。

A painting method is for painting only. It should paint the properties of your class. In this case the properties would be the Shapes you want to paint. So you class needs a method to specify the Shapes you want to paint. You don't want to create the shapes in the painting method.

您想:


  1. 创建一个ArrayList来保存你想画的形状。

  1. create an ArrayList to hold the Shapes you want to paint.

然后在的paintComponent()方法,你只是遍历列表和油漆每个形状。

Then in the paintComponent() method you just iterate through the List and paint each shape.

基本code会是这样的:

The basic code would be something like:

// Create List containing Shapes to be painted

List<Shape> shapes = new ArrayList<Shape>();
shapes.add( circle ):
shapes.add( rectangle );

// The custom painting code might look like:

protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g.create();

    for (Shape info : shapes)
    {
        g2d.fill( info.getShape() );
    }

    g2d.dispose();
}

所以,你类也将可能有一个 addShape(...)方法来更新形状列表。

对于这种方法的工作示例检查出的 DrawOnComponent 例如HTTPS://tips4java.word$p$pss.com/2009 / 05/08 /自定义彩绘的办法/相对=nofollow>自定义涂装途径。这个例子只绘制矩形,但它表明了绘画的方法。

For a working example of this approach check out the DrawOnComponent example found in Custom Painting Approaches. This example only draws Rectangles but it demonstrates the painting approach.

这篇关于我怎么会去有关创建的paintComponent使用自定义的图形类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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