图形中有多个独立的图层 [英] Multiple independent layers in Graphics

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

问题描述

是否可以使用 Graphics 来创建多个独立的图层?实际上,我需要两层(如Photoshop,Gimp,Paint.NET等),我可以在其上绘制对象,并且我希望顶层的所有对象都出现在底层的所有对象之上,而与其中的顺序无关对象被绘制。不是这种情况,我可以先绘制一个图层的所有对象,然后绘制其他对象的所有对象。



示例:


  1. 在底层上绘制红色圆圈
  2. 在顶层上绘制绿色矩形
  3. 绘制红色在底层填充矩形

  4. 在顶层上绘制绿色圆圈

预期结果:空白的绿色对象应该被渲染到两个填充的红色对象上面。



我使用 JLayeredPane Canvas ,而且我能得到的最接近的是 JComponent Canvas >,但不幸的是, Canvas 背景不能透明,所以在 JComponent 中绘制的所有内容都被 Canvas background。



你能告诉我如何实现这个(如果可能的话)?

解决方案

有几个wa是的,做到这一点。你可以保存一个BufferedImage列表作为你的图像层,并相应地渲染它们:

 类AdvancePaint extends JPanel(){
ArrayList< BufferedImage>层;
//其他构造函数,变量,初始化未显示

class AdvancePaint(){
layers = new ArrayList< BufferedImage>();
}

public void addLayer(BufferedImage layer){
layers.add(layer);
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
for(BufferredImage buf:layers)//渲染所有图层
g.drawImage(buf,0,0,width,height,null);




$ b $ p
$ b

上面的代码将从列表中呈现BufferedImage根据他们被添加到列表中的顺序。如果您不想在每次重绘时绘制所有图层,则始终可以添加另一个 draw(Graphics g)方法,您可以选择要绘制到另一个BufferedImage上的图层。然后只在 paintComponent(g)上绘制这个BufferedImage。



由于图层存储为 Collection ,您可以随时通过排序或更改列表中的位置来更改其渲染顺序。


$ b 要添加图层:

  BufferedImage layer = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); 
advancePaint.addLayer(layer);

使用 BufferedImage.TYPE_INT_ARGB ,后台将会透明,因此其他图层将能够闪耀。


Is it possible to have multiple independent drawing layers using Graphics? I actually need two layers (like in Photoshop, Gimp, Paint.NET, ...) on which I can draw objects and I want all objects of the top layer appear above all objects of the bottom layer, independent of the order in which objects are drawn. It is not the case that I can first draw all objects of one layer and then draw all objects of the other.

Example:

  1. draw red filled circle on bottom layer
  2. draw green rectangle on top layer
  3. draw red filled rectangle on bottom layer
  4. draw green circle on top layer

Desired result: the two empty green objects should be rendered above the two filled red objects.

I fiddled around using JLayeredPane and Canvas and the closest I could get was a Canvas on top of a JComponent but unfortunately the Canvas background cannot be transparent, so everything that is drawn in the JComponent is covered by the Canvas background.

Can you tell me how to achieve this (if it's possible after all)?

解决方案

There are a few ways to do it. You can keep a list of BufferedImages as your layers of images and render them accordingly:

class AdvancePaint extends JPanel(){
    ArrayList<BufferedImage> layers;
    //Other constructors, variables, initializations not shown

    class AdvancePaint(){
        layers = new ArrayList<BufferedImage>();
    }

    public void addLayer(BufferedImage layer){
        layers.add(layer);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(BufferredImage buf : layers)  //render all layers
            g.drawImage(buf, 0, 0, width, height, null);
    }
}

The above codes will render the BufferedImage from the list in accordance to the order they are added to the list. If you do not want to paint all layers on every repaint, you can always add another draw(Graphics g) method which you can pick the layers to be drawn onto another BufferedImage. Then only draw this BufferedImage on paintComponent(g).

Since the layers are stored as a Collection, you can always change their rendering order by sorting them or changing their position in the list.

You can always tweak on the minor details from my example to customize what you need.


To add a Layer:

BufferedImage layer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
advancePaint.addLayer(layer);

Using BufferedImage.TYPE_INT_ARGB, the background will be transparent, hence other layers will be able to "shine through".

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

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