mypiccomponent.java

来自「Java 简易小相册.帮你理解Drag and Drop 的功能.」· Java 代码 · 共 78 行

JAVA
78
字号
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JComponent;
/*
 * Created on 1/07/2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Xuesong Le
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MyPicComponent extends JComponent
{
    Image
    	img = null;
    int
    	WIDTH = 171,
    	HEIGHT = 256;
    
    private static final long serialVersionUID = 1L;

    public MyPicComponent(Image img)
    {
        this.img = img;
    }
    
    public void setImage(Image img)
    {
        this.img = img;
        repaint();
    }
    
    public Image getImage()
    {
        return img;
    }
    
    protected void paintComponent(Graphics g)
    {
        // Fill A white Rectange
        g.setColor(Color.white);
        g.fillRect(0,0,img == null ? WIDTH : img.getWidth(this),
                		img == null ? HEIGHT : img.getHeight(this));
        
        // Draw An Image
        g.drawImage(img,0,0,this);
        
        // Draw A Red Rectangle if focused
        // Draw A Black Rectangle if not focused
        if(this.isFocusOwner())
        {
            g.setColor(Color.red);
        }
        else
        {
            g.setColor(Color.black);
        }
        
        g.drawRect(0,0,img == null ? WIDTH : img.getWidth(this),
                		img == null ? HEIGHT : img.getHeight(this));
        
        g.dispose();
        
    }
    
    
    public static void main(String[] args)
    {
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?