All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.OSXPainterTest Maven / Gradle / Ivy

Go to download

A Mavenisation of the Quaqua Mac OSX Swing Look and Feel (Java library) Quaqua Look and Feel (C) 2003-2017, Werner Randelshofer. Mavenisation by Matt Gumbley, DevZendo.org - for problems with Mavenisation, see Matt; for issues with Quaqua, see the Quaqua home page. For full license details, see http://randelshofer.ch/quaqua/license.html

The newest version!
/*
 * @(#)OSXPainterTest.java  1.0  2011-07-26
 * 
 * Copyright (c) 2011-2013 Werner Randelshofer, Switzerland.
 * All rights reserved.
 * 
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package test;

import ch.randelshofer.quaqua.osx.OSXAquaPainter;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.DataBufferInt;
import java.awt.image.Kernel;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import static java.lang.Math.*;

/**
 * OSXPainterTest.
 *
 * @author Werner Randelshofer
 * @version 1.0 2011-07-26 Created.
 */
public class OSXPainterTest extends javax.swing.JPanel {

    private static class Canvas extends JPanel {

        private BufferedImage image;
        private OSXAquaPainter painter;

        public Canvas() {
            System.out.println("OSXPainterTest nativeCodeAvailable=" + OSXAquaPainter.isNativeCodeAvailable());
            painter = new OSXAquaPainter();
            painter.setWidget(OSXAquaPainter.Widget.buttonCheckBox);
            painter.setValueByKey(OSXAquaPainter.Key.value,1.0);
            painter.setValueByKey(OSXAquaPainter.Key.focused,1.0);
        }

        static float[] gaussian(float radius, float s,float sum) {
            int r = (int) Math.ceil(radius);
            float[] gaussian = new float[r * 2 + 1];

            // compute the gaussian
            float h = 1f; // height of the peak 
            float c = r; // position of the centre of the peak 
            //float s = radius/1.5f; // width of the 'bell'
            float invs2sq = 1f/(2f * s * s);
            for (int i = 0; i < gaussian.length; i++) {
                float x = i;
                gaussian[i] = (float) (h * exp(-pow(x - c, 2) * invs2sq));
            }

            normalizeKernel(gaussian,sum);
System.out.println("g="+Arrays.toString(gaussian));
            return gaussian;
        }
        static float[] pyramid(float radius, float sum) {
            int r = (int) Math.ceil(radius);
            float[] gaussian = new float[r * 2 + 1];

            // compute the pyramid
            float c = r; // position of the centre of the peak 
            
            for (int i = 0; i < gaussian.length; i++) {
                float x = i;
                gaussian[i] = (float) c-abs(x-c);
            }

            normalizeKernel(gaussian,sum);
System.out.println("p="+Arrays.toString(gaussian));
            return gaussian;
        }

        /** Normalizes the kernel so that all its elements add up to the given
         * sum. 
         * 
         * @param kernel
         * @param sum 
         */
        static void normalizeKernel(float[] kernel, float sum) {
            float total = 0;
            for (int i = 0; i < kernel.length; i++) {
                total += kernel[i];
            }
            if (abs(total) > 1e-20) {
                total = sum / total;
                for (int i = 0; i < kernel.length; i++) {
                    kernel[i] *= total;
                }
            }

        }

        @Override
        public void paintComponent(Graphics grr) {
            Graphics2D gr = (Graphics2D) grr;
            int w = getWidth(), h = getHeight();
            if (image == null || image.getWidth() != w || image.getHeight() != h) {
                image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            }

            int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
            //painter.paint(data, image.getWidth(), image.getHeight(), 0, 0, 40, 40);
            painter.paint(image, 6, 6, 32, 20);
            gr.drawImage(image, 0, 0, this);
            gr.drawImage(image, 40, 0, this);
            gr.drawImage(image, 80, 0, this);
            gr.drawImage(image, 120, 0, this);

            //
            float intensity = 1.8f;
            int blur = 7;
            final float blurry = intensity / (blur * blur);
            final float[] blurKernel = new float[blur * blur];
            for (int i = 0; i < blurKernel.length; i++) {
                blurKernel[i] = blurry;
            }
            ConvolveOp blurOp = new ConvolveOp(new Kernel(blur, blur, blurKernel));

            // 
            ConvolveOp sobelTL = new ConvolveOp(new Kernel(3, 3, (new float[]{2, 1, 0, 1, 0, -1, 0, -1, -2})));
            ConvolveOp sobelBR = new ConvolveOp(new Kernel(3, 3, (new float[]{-2, -1, 0, -1, 0, 1, 0, 1, 2})));
/*
            ConvolveOp edgeLeftOp = new ConvolveOp(new Kernel(2, 1, new float[]{1, -1}));
            ConvolveOp edgeRightOp = new ConvolveOp(new Kernel(2, 1, new float[]{-1, 1}));
            ConvolveOp edgeTopOp = new ConvolveOp(new Kernel(1, 2, new float[]{1, -1}));
            ConvolveOp edgeBottomOp = new ConvolveOp(new Kernel(1, 2, new float[]{-1, 1}));
            */
            ConvolveOp edgeLeftOp = new ConvolveOp(new Kernel(3, 1, new float[]{1, 0,-1}));
            ConvolveOp edgeRightOp = new ConvolveOp(new Kernel(3, 1, new float[]{-1, 0,1}));
            ConvolveOp edgeTopOp = new ConvolveOp(new Kernel(1, 3, new float[]{1, 0,-1}));
            ConvolveOp edgeBottomOp = new ConvolveOp(new Kernel(1, 3, new float[]{-1,0, 1}));
            /*
            float[] edgy=new float[]{0.5f,1, 0,-1,-0.5f};
            float[] medgy=new float[edgy.length];
            for (int i=0;i//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// //GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy