jncheckradiobase.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 281 行

JAVA
281
字号
package org.jnode.wt.components;

import org.jnode.wt.events.JNodeMouseEvent;
import org.jnode.wt.image.JNImage;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;

/**
 * @author kishore
 */

abstract public class JNCheckRadioBase extends JNAbstractButton //JNComponent
{

    private JNLabel aLabel = null;

    protected int iconX = 0;
    protected int iconY = 0;
    protected int iconWidth = 12;
    protected int iconHeight = 12;

    protected int labelLocationX = 0;
    protected int labelLocationY = 0;

    BufferedImage bi = null;
    Graphics big = null;

    protected static Image imageSelected = null;
    protected static Image imageUnselected = null;

    /**
     * KCheckBox constructor comment.
     */
    public JNCheckRadioBase(String s) {
        this(s, -1, -1);
    }

    /**
     * KCheckBox constructor comment.
     */
    public JNCheckRadioBase(String text, int w, int h) {
        super();

        /* This is not acutal size calculation for KLabel,
           This is only to calculate the required width and hight.for KLabel,
           which is used to calculate the CheckBox size */
        aLabel = new JNLabel(text);

        if ((w < 0) || (h < 0)) {
            this.setSize(calcPreferedsize(aLabel));
        } else {
            this.setSize(w, h);
        }

        this.setBackground(aLabel.getBackground());
        //-*-//
        aLabel.setEnableBorderLine(false);
//aKLabel.enableBorderLine(true);

        //-*-//
        this.setAlignment(JNLabel.LEFT);
        doImageProcessing();

    }

    protected Dimension calcPreferedsize(JNLabel akl) {
/*  Modifiy this method this is different for each component */

        /* IF w,h are lessthan 0 get the preferredsize and set them as size. */
//	if((akl.width < 0) || ( akl.height < 0)) 	{


        Dimension labelD = akl.getPreferredSize();

        int w = comp_insets.left + (iconWidth + 2) + labelD.width;//(INSETS_WIDTH * 2);
        int h = 0;

        if (labelD.height > iconHeight)
            h = labelD.height;
        else
            h = iconHeight;

        return new Dimension(w, h);
        //-*-//


    }

    void doImageProcessing() {
        bi = new BufferedImage(13, 13, BufferedImage.TYPE_INT_RGB);
        big = bi.getGraphics();

//	big.dra

    }

    public int getAlignment() {
        return aLabel.getAlignment();
    }

    public String getText() {
        return aLabel.getText();
    }

    public void handlepixels(Image img, int x, int y, int w, int h) {
        int[] pixels = new int[w * h];
        PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);

        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            System.err.println("interrupted waiting for pixels!");
            return;
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            System.err.println("image fetch aborted or errored");
            return;
        }

        Util.startColorProcessing();

        int z = 1;
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                handlesinglepixel(x + i, y + j, pixels[j * w + i], z);
//			  System.out.println("[ "+z+" ] "+ pixels[j * w + i] );
//		   	  System.out.print( Util.fs(10, pixels[j * w + i]) +",");

                z++;
            }
        }//For j
        Util.endColorProcessing();
    }

    public void handlesinglepixel(int x, int y, int pixel, int index) {
        //int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        // Deal with the pixel as necessary...

//  System.out.println("[ "+index+" ] "+alpha+"     ("+ red+","+green+","+blue+")" );



        /*String s =
//   "[ " + Util.fs(3, index)   + " ] "
//		+ Util.fs(3, alpha)   + "   ( "
                "("
                + Util.fs(3, red) + ","
                + Util.fs(3, green) + ","
                + Util.fs(3, blue) + ")";*/

//  System.out.println( s );
//	  System.out.print( s );

        System.out.print(Util.getColorIndexOf(red, green, blue) + ", ");
    }

    public void internallyPaint(Graphics g) {
//	super.internallyPaint(g);
        g.setColor(getBackground());
        g.fillRect(0, 0, getBounds().width, getBounds().height);
        g.setColor(getForeground());


        paintIcon(g);

        int lx = aLabel.getX();
        int ly = aLabel.getY();

        g.translate(lx, ly);
        aLabel.internallyPaint(g);
//	aLabel.repaint();
        g.translate(-lx, -ly);

        g.setColor(Color.black);
        g.drawRect(0, 0, getBounds().width, getBounds().height);
        g.setColor(Color.white);
        g.drawRect(1, 1, getBounds().width - 2, getBounds().height - 2);


    }

    protected void paintIcon(Graphics g) {
        g.setColor(this.getBackground());
//	g.fillRect(0,0, getBounds().width, getBounds().height);
        g.fillRect(iconX, iconY, iconWidth, iconHeight);
        g.setColor(this.getForeground());
    }

    protected void processMouseEvent(JNodeMouseEvent event) {
//	if (event.getID() == JNodeMouseEvent.MOUSE_PRESSED || event.getID() == JNodeMouseEvent.MOUSE_RELEASED)
        if (event.getID() == JNodeMouseEvent.MOUSE_RELEASED) {
            switchState();
            Graphics gfx = getGraphics().create();
            gfx.translate(getAbsLocation().x, getAbsLocation().y);
            update(gfx);
        }
        super.processMouseEvent(event);
    }

    public void recalculate() {
    }

    /*
    public void recalculate() {
        aLabel.setSize(getSize().width - (iconWidth + 2 + comp_insets.left), getSize().height);

        labelLocationX = comp_insets.left + iconWidth + 2;
        labelLocationY = 0;

        aLabel.setLocation(labelLocationX, labelLocationY);

        // calculate icon details.

        iconX = comp_insets.left;
        iconY = (getSize().height - iconHeight) / 2;
    }
    */


    public void resize() {
    }

    public void setAlignment(int alignment) {
        aLabel.setAlignment(alignment);
    }

    public void setImage(JNImage img) {
        aLabel.setImage(img);
    }

    public void setSelected(boolean b) {
        super.setSelected(b);
        aLabel.setSelected(b);
    }

    public void setSelectedImage(JNImage img) {
        aLabel.setSelectedImage(img);
    }

    public void setSize(int w, int h) {
        super.setSize(w, h);

        aLabel.setSize(w - (iconWidth + 2 + comp_insets.left), h);

        labelLocationX = comp_insets.left + iconWidth + 2;
        labelLocationY = 0;

        aLabel.setLocation(labelLocationX, labelLocationY);

        // calculate icon details.

        iconX = comp_insets.left;
        iconY = (h - iconHeight) / 2;
    }

    public void setText(String str) {
        aLabel.setText(str);
    }

    protected boolean switchState() {
        if (isSelected()) {
            //isSelected = false;
            setSelected(false);
        } else {
            //isSelected = true;
            setSelected(true);
        }

        return isSelected();
    }
}

⌨️ 快捷键说明

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