⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java2dtools.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2005 by Werner Klieber (werner@klieber.info)
 * http://caliph-emir.sourceforge.net
 */
package at.wklieber.tools;

import at.wklieber.gui.IColorRectangle;
import at.wklieber.gui.IComponent;
import at.wklieber.gui.dominantcolor.DominantColorFinder;
import at.wklieber.gui.dominantcolor.DominantColorPlugin;
import at.wklieber.gui.dominantcolor.RGBColorPercentagePair;
import at.wklieber.gui.dominantcolor.RGBColorPercentagePairList;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;


public class Java2dTools {
    static Category cat = Logger.getLogger(Java2dTools.class.getName());
    private static Java2dTools java2dTools = null;

    public static final String LOOK_CURRENT_OS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    public static final String LOOK_WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    public static final String LOOK_MOTIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    public static final String LOOK_MAC = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
    public static final String LOOK_JAVA = "javax.swing.plaf.metal.MetalLookAndFeel";
    //public static final String LOOK_KUNSTSTOFF = "com.incors.plaf.kunststoff.KunststoffLookAndFeel";

    public static final int COMPONENT_MIN_SIZE = 40;


    public static final int TYPE_SERIALIZE_RAW = 0; // serialize the complete image array data uncompressed
    public static final int TYPE_SERIALIZE_JPEG = 1; // serialize the complete image array data JPEG encoded


    public static Java2dTools getReference() {
        if (java2dTools == null) {
            java2dTools = new Java2dTools();
        }

        return java2dTools;
    }


    public static void setLookAndFeel(String lookAndFeel, JFrame frame1) {
        try {
            // the java look - on all plattforms
            //"javax.swing.plaf.metal.MetalLookAndFeel"
            //UIManager.getCrossPlatformLookAndFeelClassName();

            UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();

            for (int i = 0; i < info.length; i++) {
                cat.debug("LOOK: " + info[i].getName() + ", \"" + info[i].getClassName() + "\"");
            }

            LookAndFeel[] infoAux = UIManager.getAuxiliaryLookAndFeels();

            if (infoAux == null) {
                cat.debug("LOOK AUX: no additional look and feels installed");
            } else {
                for (int i = 0; i < infoAux.length; i++) {
                    cat.debug("LOOK AUX: " + infoAux[i].getName() + ", \"" + infoAux[i].getDescription());
                }
            }

            // the look and feel of th currend os
            if (lookAndFeel.equals(LOOK_WINDOWS)) {
                UIManager.setLookAndFeel(LOOK_WINDOWS);
            } else if (lookAndFeel.equals(LOOK_MOTIF)) {
                UIManager.setLookAndFeel(LOOK_MOTIF);
            } else if (lookAndFeel.equals(LOOK_MAC)) {
                UIManager.setLookAndFeel(LOOK_MAC);
            } else if (lookAndFeel.equals(LOOK_JAVA)) {
                UIManager.setLookAndFeel(LOOK_JAVA);
            } /*else if (lookAndFeel.equals(LOOK_KUNSTSTOFF)) {
                UIManager.setLookAndFeel(new com.incors.plaf.kunststoff.KunststoffLookAndFeel());
            }*/ else
                UIManager.getSystemLookAndFeelClassName();

            if (frame1 != null) {
                Rectangle rect = frame1.getBounds();
                SwingUtilities.updateComponentTreeUI(frame1);
                frame1.pack();
                frame1.setBounds(rect);
                frame1.validate();
            }
        } catch (Exception e) {
            cat.error(e);
        }
    }


    private Java2dTools() {
    } // end constructor


    public void drawCenteredText(Graphics g1, String text1, int middleX1, int middleY1) {
        int textWidth = (g1.getFontMetrics()).stringWidth(text1);
        //int d = (g1.getFontMetrics()).getDescent();
        int textHeight = (g1.getFontMetrics()).getHeight();


        int x = (middleX1 - (textWidth / 2));
        int y = (middleY1 + (textHeight / 2));

        g1.drawString(text1, x, y);
    }

    /**
     * use the paint method to generate an svg stream
     */
    public void java2Svg(JPanel panel1, String filename1) {
        cat.error("not implemented");
        /*try {
           System.out.println("writing svg. Pleas wait");
           // Get a DOMImplementation
           DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
           // Create an instance of org.w3c.dom.Document
           Document document = domImpl.createDocument(null, "svg", null);
           // Create an instance of the SVG Generator
           SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
           // Ask the test to render into the SVG Graphics2D implementation
           //TestSVGGen test = new TestSVGGen();
           panel1.paint(svgGenerator);
           // Finally, stream out SVG to the standard output using UTF-8
           // character to byte encoding

           FileOutputStream fout = new FileOutputStream(filename1);
           Writer out = new OutputStreamWriter(fout, "UTF-8");

           //Writer out = new OutputStreamWriter(System.out, "UTF-8");
           boolean useCSS = true; // we want to use CSS style attribute
           svgGenerator.stream(out, useCSS);
           System.out.println("File written: \"" + filename1 + "\"");
        } catch (Exception e) {
           e.printStackTrace();
        }*/
    }

    public void viewSvg(String filename1) {
        cat.error("not implemented");
        /*try {
           String[] args = new String[1];
           args[0] = filename1;

           org.apache.batik.apps.svgbrowser.Main browser;
           browser = new org.apache.batik.apps.svgbrowser.Main(args);


        } catch (Exception e) {
           e.printStackTrace();
        }*/
    }

    /**
     * Retrun a Color that has enough contrast with the drawColor so it can be used for
     * a readable text. Default color is black. If the drawcolor is dark, white is returned
     */
    public Color getContrastColor(Color drawColor1) {
        Color returnValue = Color.BLACK;
        if (drawColor1 == null) {
            return returnValue;
        }

        //System.out.println("C: " + drawColor1.toString());
        try {
            Color black = Color.BLACK;
            //drawColor1.
            if (drawColor1.getRGB() == black.getRGB()) {
                returnValue = Color.GRAY;

            }

            if ((drawColor1.getRed() < 10) && (drawColor1.getRed() < 10) && (drawColor1.getRed() < 10)) {
                returnValue = Color.GRAY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }

    /**
     * fit a frame (e. g. a image) to a window by leaving the aspect ratio unchanged
     *
     * @param window1    the size of the window that defines the outer border
     * @param component1 the size of a component that has to be fit in
     * @return the new size of the component1. The component lies in the middle of the outer window
     */
    /*
    public Rectangle fitToWindow(Rectangle window1, Rectangle component1) {
        Rectangle returnValue = new Rectangle((int) window1.getX(), (int) window1.getY(), (int) component1.getWidth(), (int) component1.getHeight());

        // ratio from width to heigth
        float ratioComponent = (float) (component1.width / (float) component1.height);
        float ratioWindows = (float) (window1.width / (float) window1.height);

        //cat.debug("-----------------------------------------------");
        //cat.debug("Input component: " + returnValue.toString() + "Ratio: " + ratioComponent);
        //cat.debug("Input win : " + window1.toString() + "Ratio: " + ratioWindows);
        try {
            // ratio from width to heigth

            boolean hasShrinked = false;
            // shrink component width if window is smaller
            if (returnValue.width > window1.width) {
                int newWidth = (int) (window1.width);
                int newHeigh = (int) (newWidth / ratioComponent);

                returnValue.setBounds((int) returnValue.getX(), (int) returnValue.getY(), newWidth, newHeigh);
                hasShrinked = true;
                //cat.debug("CORRECT width: ratio: " + (newWidth / (float) newHeigh) + ", new w/h: " + newWidth + ", " + newHeigh);
            }

            // shrink frame heigh if window is too small
            // the wide of the image fits already to the windows bounds
            if (returnValue.height > window1.height) {
                int newHeigh = (int) window1.height;
                int newWidth = (int) (newHeigh * ratioComponent);


                returnValue.setBounds((int) returnValue.getX(), (int) returnValue.getY(), (int) newWidth, (int) newHeigh);
                hasShrinked = true;
                //cat.debug("CORRECT Heigh: ratio: " + (newWidth / (float) newHeigh) + ", new w/h: " + newWidth + ", " + newHeigh);
            }

            if (!hasShrinked) {
                // strech frame width if window is too big
                float rateWithBigger = (window1.width / (float) (returnValue.width));
                //float rateWithBigger = 1000;
                float rateHeighBigger = (window1.height / (float) (returnValue.height));
                //float rateHeighBigger = 1000;

                float rateBigger = 1;
                float ratioFrameW = 1;
                float ratioFrameH = 1;
                if (rateWithBigger < rateHeighBigger) {
                    rateBigger = rateWithBigger;
                    //ratioFrameH = 1/ratioComponent;
                } else {
                    rateBigger = rateHeighBigger;
                    //ratioFrameW = ratioComponent;
                }

                int newWidth = (int) (returnValue.width * rateBigger * ratioFrameW);
                int newHeigh = (int) (returnValue.height * rateBigger * ratioFrameH);
                returnValue.setBounds((int) returnValue.getX(), (int) returnValue.getY(), (int) newWidth, (int) newHeigh);
                //cat.debug("CORRECT Streched: rW: " + ratioFrameW + ", rH: " + ratioFrameH + ", bigger rate: " + rateBigger);
                //cat.debug("-->New Width: " + newWidth + ", new Heigh: " + newHeigh);
            }  // end if strech

            // finally center the frame
            //int middleX = (int) window1.getWidth() / 2;
            //int middleY = (int) window1.getHeight() / 2;

            int offsetX = (int) ((window1.getWidth() - returnValue.getWidth()) / 2);
            int offsetY = (int) ((window1.getHeight() - returnValue.getHeight()) / 2);

            int with = returnValue.width;
            int height = returnValue.height;
            returnValue.setBounds((int) returnValue.getX() + offsetX, (int) returnValue.getY() + offsetY, with, height);
            //cat.debug("New Center: " + returnValue.toString());
            //returnValue.setBounds(0, 0, with, height);

        } catch (Exception e) {
            cat.error(e);
        }


        return returnValue;
    }
    */

    public Rectangle2D fitToWindow(Rectangle2D window1, Rectangle2D component1) {
        Rectangle2D returnValue = (Rectangle2D) window1.clone();
        returnValue.setRect(window1.getX(), window1.getY(), component1.getWidth(), component1.getHeight());

        // ratio from width to heigth
        double ratioComponent = (double) (component1.getWidth() / (double) component1.getHeight());
        double ratioWindows = (double) (window1.getWidth() / (double) window1.getHeight());

⌨️ 快捷键说明

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