1

我刚开始重新编码,我想我忘记了如何加倍缓冲。这是我现在拥有的代码,我不确定我缺少什么。当我启动它时,只有一个白色的屏幕,没有椭圆形。

渲染的错误是什么?

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;

public class Graphs extends JFrame {

private Image dbImage;
private Graphics dbg;

public static void main(String[] args) {
    new Graphs();
}

public Graphs() {
    setSize(1000, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setTitle("Graphs");
    setLocationRelativeTo(null);
    setVisible(true);
}

public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    dbg.drawImage(dbImage, 0, 0, this);
}

public void paintComponent(Graphics g) {
    g.drawOval(200, 200, 200, 200);
    repaint();
}
}

更新:编译错误@Override

The method  paintComponent(Graphics) of type Graphs must override or implement a supertype method.

1 quick fix available:
-> Remove '@Override' annotation
4

2 回答 2

5

您没有看到椭圆的原因是您正在将图像绘制到它自己的Graphics对象上。代替:

dbg.drawImage(dbImage, 0, 0, this);

g.drawImage(dbImage, 0, 0, this);

最好不要paint在顶级容器中覆盖,而是paintComponent在子类中覆盖JComponent。也记得打电话

super.paintComponent(g);
于 2013-01-24T01:56:05.387 回答
3
  1. 您应该避免覆盖paint顶级
    组件(如JFrame)的方法,对您来说主要问题是它们不是双缓冲的,就像从JComponent
  2. 不打电话super.paint是非常非常糟糕的。你基本上阻止了框架绘制它的任何子组件......
  3. 您不应该在该paint方法中加载图像,它会减慢任何未来的重绘
  4. 你永远不应该repaint从任何paintXxx方法调用,它会导致创建一个无限循环的绘画,迅速吞噬你的 CPU 周期
  5. JFrame没有paintComponent方法。
  6. 优选地,通过该方法执行定制绘画JComponent(例如)。JPanelpaintComponent

更新示例

在此处输入图像描述

public class BadPaint10 {

    public static void main(String[] args) {
        new BadPaint10();
    }

    public BadPaint10() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private BufferedImage background;

        public PaintPane() {
            try {
                background = ImageIO.read(new File("C:/Users/shane/Dropbox/pictures/436px-Work_with_exotic_creatures.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g.drawImage(background, x, y, this);

                x = (getWidth() - 200) / 2;
                y = (getHeight() - 200) / 2;
                g.setColor(Color.RED);
                g.drawOval(x, y, 200, 200);

            }

        }
    }
}

如前所述,优先级很重要。元素绘制的顺序将影响结果。

你可能会发现

有用。

于 2013-01-24T01:55:58.747 回答