org.jcurl.core.swing.SumDisplayBase.java Source code

Java tutorial

Introduction

Here is the source code for org.jcurl.core.swing.SumDisplayBase.java

Source

/*
 * jcurl java curling software framework http://www.jcurl.org
 * Copyright (C) 2005-2009 M. Rohrmoser
 * 
 * This program 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.
 * 
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jcurl.core.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.commons.logging.Log;
import org.jcurl.core.api.RockSet;
import org.jcurl.core.api.RockSetUtils;
import org.jcurl.core.api.RockType.Pos;
import org.jcurl.core.log.JCLoggerFactory;
import org.jcurl.core.ui.IceShapes;

/**
 * 
 * @author <a href="mailto:m@jcurl.org">M. Rohrmoser </a>
 * @version $Id:SumDisplayBase.java 378 2007-01-24 01:18:35Z mrohrmoser $
 */
abstract class SumDisplayBase extends JComponent implements ChangeListener {

    private static final IceShapes.RockColors colors = new IceShapes.RockColors();

    private static final Map<Key, Object> hints = new HashMap<Key, Object>();

    private static final Log log = JCLoggerFactory.getLogger(SumDisplayBase.class);
    static {
        // hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION,
        // RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // hints.put(RenderingHints.KEY_COLOR_RENDERING,
        // RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        // hints.put(RenderingHints.KEY_DITHERING,
        // RenderingHints.VALUE_DITHER_ENABLE);
        // hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
        // RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        // hints.put(RenderingHints.KEY_INTERPOLATION,
        // RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        // hints.put(RenderingHints.KEY_RENDERING,
        // RenderingHints.VALUE_RENDER_QUALITY);
        // hints.put(RenderingHints.KEY_STROKE_CONTROL,
        // RenderingHints.VALUE_STROKE_NORMALIZE);
        // hints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
        // RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }

    public Paint backGround = new Color(0xF0F0FF);

    private RockSet<Pos> model;

    private int recentMask = -1;

    public SumDisplayBase() {
        this(null);
    }

    public SumDisplayBase(final RockSet<Pos> model) {
        this.setPos(model);
    }

    protected abstract int computeMask(final RockSet<Pos> rocks);

    @Override
    public Dimension getMaximumSize() {
        return super.getMaximumSize();
    }

    @Override
    public Dimension getMinimumSize() {
        final int d = 10;
        return new Dimension(d, d);
    }

    @Override
    public Dimension getPreferredSize() {
        return getMinimumSize();
    }

    @Override
    protected void paintComponent(final Graphics g) {
        super.paintComponent(g);
        final Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHints(hints);
        // background
        g2.setPaint(backGround);
        g2.fillRect(0, 0, getWidth(), getHeight());
        for (int i = RockSet.ROCKS_PER_COLOR - 1; i >= 0; i--) {
            if (RockSet.isSet(recentMask, i, true))
                paintRock(g2, i, true);
            if (RockSet.isSet(recentMask, i, false))
                paintRock(g2, i, false);
        }
    }

    protected void paintRock(final Graphics2D g2, final int idx8, final boolean isDark) {
        final float r = 0.35F * getWidth();
        // vertical display:
        final float cx = 0.5F * getWidth();
        float cy = getHeight() / RockSet.ROCKS_PER_SET;
        if (isDark)
            cy *= idx8 + 0.5F;
        else
            cy *= RockSet.ROCKS_PER_SET - (idx8 + 0.5F);
        g2.setPaint(isDark ? colors.dark : colors.light);
        g2.fillArc((int) (cx - r), (int) (cy - r), (int) (2 * r), (int) (2 * r), 0, 360);
        g2.setColor(Color.BLACK);
        g2.drawArc((int) (cx - r), (int) (cy - r), (int) (2 * r), (int) (2 * r), 0, 360);
    }

    public void setPos(final RockSet<Pos> rocks) {
        // if (model != null && model != rocks)
        // model.removeChangeListener(this);
        // rocks.addChangeListener(this);
        model = rocks;
        showRocks(computeMask(model));
    }

    public void setPos(final RockSet<Pos> rocks, final int discontinuous) {
        this.setPos(rocks);
    }

    public void showRocks(final int mask) {
        if (mask == recentMask)
            return;
        recentMask = mask;
        if (log.isDebugEnabled())
            log.debug("mask " + recentMask);
        this.repaint();
    }

    public void stateChanged(final ChangeEvent evt) {
        final Object tmp = evt.getSource();
        if (tmp == null || RockSetUtils.class.isAssignableFrom(tmp.getClass()))
            this.setPos((RockSet<Pos>) tmp);
        else
            log.info(tmp);
    }
}