简体   繁体   English

将图像绘制到AWT上并初始化图形

[英]Drawing images onto AWT and initialising Graphics

I'll start off by telling you what Im trying to do if thats OK, as Im not certain the route Im struggling with is even the best way of achieving my ends. 首先,我将告诉您我可以尝试做的事情,因为我不确定我努力奋斗的路线甚至是实现目标的最佳途径。

I have a JFrame containing two JPanels. 我有一个包含两个JPanels的JFrame。 One contains a number of buttons (buttonPanel), the other is, initially, blank (displayPane). 一个包含许多按钮(buttonPanel),另一个最初是空白(displayPane)。 When buttons are pressed the stuff shown in displayPanel changes. 按下按钮时,displayPanel中显示的内容会更改。 The way this is working is each press of a button creates a new object that extends JPanel and then adds that to displayPane 这种工作方式是每次按下按钮都会创建一个扩展JPanel的新对象,然后将其添加到displayPane

So all the above is working fine and dandy (although I freely admit it may not be the best way of doing it) except for one particular case. 因此,除了一种特殊情况外,以上所有方法都工作正常且花哨(尽管我自由地承认这可能不是最佳方法)。

In this particular case I need to create a JLayeredPanel and then draw a clipped image on it. 在这种情况下,我需要创建一个JLayeredPanel,然后在其上绘制一个裁剪的图像。 JLayeredPanel because I want to draw some stuff on top of it, clipped because I only want to show part of the area (which exact part is passed to the constructor). JLayeredPanel是因为我想在它上面绘制一些东西,因此被裁剪了,因为我只想显示该区域的一部分(确切的部分传递给了构造函数)。

Now, the problem Im having is this. 现在,我的问题是这个。 The only way I know to draw a clipped image is through g=thingie.getGraphics(), g.setClip(Shape shape), g.drawImage(various) . 我知道绘制裁剪图像的唯一方法是通过g=thingie.getGraphics(), g.setClip(Shape shape), g.drawImage(various) However, all of that relies on being able to get graphics. 但是,所有这些都依赖于能够获得图形。 But because I am assembling the object first there is no graphics object associated with the JLayeredPane (because its not displayed) so getGraphics is returning null and g.setClip() is throwing a Null Pointer Exception. 但是,因为我首先组装了对象,所以没有与JLayeredPane关联的图形对象(因为未显示),因此getGraphics返回null,而g.setClip()抛出Null Pointer Exception。

Obviously I am doing this wrong somehow and somewhere. 显然,我以某种方式在某处做错了。 Any help would be appreciated, sorry if the question is confusing. 任何帮助,将不胜感激,如果这个问题令人困惑。 I tried to include as much detail as possible and now I am a little concerned I've muddied the issue. 我试图包括尽可能多的细节,但现在我有点担心我已经弄糊涂了。 I'll keep an eye on this and clarify if required. 我会密切注意这一点,并在需要时进行澄清。

Warning!: Wrong answer, see below the line 警告!:答案错误,请参见以下行

Why don't you just create a new Graphics object, paint on it and then use it with the update() method? 为什么不创建一个新的Graphics对象,然后在其上绘画,然后将其与update()方法一起使用?

Graphics g = new Graphics();
g.drawStuff();
thingie.update(g);

This showd be correct 这显示是正确的

As stated on the comments the previous solution was wrong but it can be done with an Double buffer, create a buffered image and draw on it, then override the paint method of the jLayeredPane pane to draw the image. 如评论中所述,先前的解决方案是错误的,但是可以使用Double缓冲区来完成,创建一个缓冲的图像并在其上绘制,然后覆盖jLayeredPane窗格的paint方法以绘制该图像。

    private void addStuff() {            
        BufferedImage bi =
            new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics bufferedGraphics = bi.getGraphics();
        //Paint stuff
        bufferedGraphics.drawLine(0, 0, 50, 50);
        javax.swing.JLayeredPane layered;
        layered = new JLayeredPane() {
            @Override
            public void paint(Graphics g) {
                g.drawImage(bi, 0, 0, null);
            }
        };        
        this.add(layered);        
        this.validate();
        this.repaint();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM