How would I go about creating a custom graphics class for paintComponent to use?

▼魔方 西西 提交于 2019-12-08 11:35:30

问题


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.

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.

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.


回答1:


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.

You want to:

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

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

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

So you class would also probably have an addShape(...) method to update the shapes List.

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.



来源:https://stackoverflow.com/questions/33716632/how-would-i-go-about-creating-a-custom-graphics-class-for-paintcomponent-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!