Paint,Repaint和PaintComponent

时间:2014-06-15 06:30:30

标签: java swing paint

我正在关注使用RescaleOp的Oracle网站上的教程。我试图在没有applet的情况下完成教程并将所有内容转换为swing。代码如下。

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/*
 * How to load an Image from an external file.
 */
public class ARGB extends JPanel {

    BufferedImage img;
    float[] scales = { 1f, 1f, 1f, 0.5f };
    float[] offsets = new float[4];
    RescaleOp rop;

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.setColor(Color.black);
        g2d.setFont(new Font("Dialog", Font.BOLD, 24));
        g2d.drawString("Java 2D is great!", 10, 80);
        g2d.drawImage(img, rop, 0, 0);
        System.out.println("hi");

    }

    public ARGB() {
        try {
            img = ImageIO.read(new File("src/strawberry.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
            // TODO: handle exception
        }
    }

    public Dimension getPreferredSize() {
        if (img == null) {
            return new Dimension(100, 100);
        } else {
            return new Dimension(img.getWidth(), img.getHeight());
        }
    }

    public void setOpacity(float opacity) {
        scales[3] = opacity;
        rop = new RescaleOp(scales, offsets, null);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final ARGB argb = new ARGB();
        JFrame frame = new JFrame("See Through Image");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.add("Center", new LoadImage());
        JSlider opacitySlider = new JSlider(0, 100);
        frame.add("South", opacitySlider);
        opacitySlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                argb.setOpacity(slider.getValue() / 100f);
                argb.repaint();
            }
        });
        Dimension size = argb.getPreferredSize();
        Dimension sliderSize = opacitySlider.getPreferredSize();
        frame.setSize(size.width, size.height + sliderSize.height);
        frame.pack();
        frame.setVisible(true);
    }

}

当我运行它时,我没有在第39行看到我的调试语句。我真的不明白为什么。 谢谢!

1 个答案:

答案 0 :(得分:1)

您尚未将JPanel对象ie.e实例的ARGB类添加到框架中。您已添加LoadImage对象。在哪儿?? debug语句在ARGB vlass中。您必须添加ARGB对象。然后只会调用它的paint方法