为什么java.awt.image.getWidth()运行得如此之慢

时间:2017-11-13 19:38:07

标签: java image awt

我目前正在打一场纸牌游戏。主类有两个不同的JPanel内部类。最多同时出现10个Field JPanel和18个Hand JPanels实例,每个实例都有一个mousePistener,其中包含MousePressed中的覆盖代码。但是,每次我点击我的卡片执行操作时,程序在响应之前有大约2-3秒的延迟。我使用YourKit来分析运行时间,它告诉我sun.awt.image.ToolkitImage.getWidth(ImageObserver)的响应时间最长(大约3秒)。我现在真的很困惑为什么getWidth(null)是如此耗时。附件是我的paintComponent方法和YourKit的结果。 YourKit Result

@Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.WHITE);

        Image cardImage = CARD.getCurrentImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH);

        int x = (getWidth() - cardImage.getWidth(null)) / 2;
        int y = (getHeight() - cardImage.getHeight(null)) / 2;

        g2d.setClip(new RoundRectangle2D.Double(
                0, 0, getWidth(), getHeight(), getWidth() / 4, getHeight() / 4));
        g2d.drawImage(cardImage, x, y, null);

        switch (CARD.getType()){
            case Card.FOLLOWER:
                Follower follower = (Follower) CARD;
                g2d.drawImage(ATK, 15,  getHeight() - 60, null);
                g2d.drawImage(DEF, getWidth() - 55, getHeight() - 60, null);

                AttributedString attack = new AttributedString(Integer.toString(follower.getAttack()));
                AttributedString health = new AttributedString(Integer.toString(follower.getHealth()));
                attack.addAttribute(TextAttribute.SIZE, 15);
                health.addAttribute(TextAttribute.SIZE, 15);
                attack.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
                health.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);

                g2d.drawString(attack.getIterator(), 35, getHeight() - 27);
                g2d.drawString(health.getIterator(), getWidth() - 35, getHeight() - 27);

                if (follower.hasWard()){
                    g2d.drawImage(WARD, (getWidth() - WARD.getWidth(null)) / 2,
                            (getHeight() - WARD.getHeight(null)) / 2, this);
                }

                if (follower.hasAmbush()){
                    g2d.drawImage(AMBUSH, (getWidth() - AMBUSH.getWidth(null)) / 2,
                            (getHeight() - AMBUSH.getHeight(null)) / 4, this);
                }

                if (follower.hasBane()) {
                    g2d.drawImage(BANE, 10, 10, this);
                }

                if (follower.hasDrain()){
                    g2d.drawImage(DRAIN, 50, 10, this);
                }

                switch (follower.getAtkStatus()){
                    case Follower.STORM_STAT:
                        g2d.setColor(Color.GREEN);
                        g2d.fillOval(getWidth() - 40, 8, 30,30);
                        g2d.setColor(Color.WHITE);
                        break;

                    case Follower.RUSH_STAT:
                        g2d.setColor(Color.YELLOW);
                        g2d.fillOval(getWidth() - 40, 8, 30, 30);
                        g2d.setColor(Color.WHITE);
                        break;

                    default:
                        g2d.setColor(Color.darkGray);
                        g2d.fillOval(getWidth() - 40, 8, 30, 30);
                        g2d.setColor(Color.WHITE);
                        break;
                }

                break;

            case Card.AMULET:
                if (((Amulet) CARD).isCountDown()){
                    g2d.drawString(Integer.toString(((Amulet) CARD).getCountDown()), getWidth() - 50, getHeight() - 50);
                }
                break;
        }
    }

0 个答案:

没有答案