Package ketUI.panel

Source Code of ketUI.panel.TPanel

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* 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 3 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, see <http://www.gnu.org/licenses/>
*/

package ketUI.panel;

import java.awt.*;
import javax.swing.*;
import geom.Offset;
import java.awt.image.*;
import java.util.Vector;

import ket.*;
import ket.math.*;
import ket.display.*;
import geom.Position;
import ket.treeDiff.Step;
import ket.display.box.Box;
import ket.treeDiff.TreeDiff;
import ket.display.Transition;
import ket.display.box.BoxText;

/**
* A separate, experimental panel to make click-and-drag algebra easier.
*/
public class TPanel extends JPanel implements Runnable {

  Vector<Box.Pair> pairs;

  private TPanel(Box equationBox) {
    pairs = equationBox.getPairs(new Position(0.0, 0.0));
    Argument root = pairs.firstElement().argument.getVisibleRoot();
    for (int i=0; i<pairs.size(); i++) {
      Box.Pair p = pairs.get(i);
      if (p.argument!=root) continue;
      pairs.remove(i);
      break;
    }
  }

  public void run() {
    JFrame frame = new JFrame("T Frame");
    frame.setSize(640, 480);
    frame.add(this);
    frame.setVisible(true);
  }

  static final int W = 4;
  static final int R = 30;
  public void paint(Graphics g) {
    double minX=Double.MAX_VALUE;
    double minY=Double.MAX_VALUE;
    double maxX=0.0;
    double maxY=0.0;
    for (Box.Pair p : pairs) {
      minX = Math.min(minX, p.position.x);
      minY = Math.min(minY, p.position.y);
      maxX = Math.max(maxX, p.position.x);
      maxY = Math.max(maxY, p.position.y);
    }
    minX -= 10.0;
    minY -= 10.0;
    maxX += 10.0;
    maxY += 10.0;
    double scaleX = getWidth() / (maxX-minX);
    double scaleY = getHeight() / (maxY-minY);
    Vector<Position> ps = new Vector<Position>();
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    Font font = new Font("Times New Roman", Font.PLAIN, 32);
    g.setFont(font);
    for (Box.Pair p : pairs) {
      Position q = new Position((p.position.x-minX)*scaleX, (p.position.y-minY)*scaleY);
      Argument a = p.argument;
      int depth = a!=null ? a.getDepth()+2 : 1;
      Color c = ColourScheme.BLACKBOARD.getArgColour(depth);
      g.setColor(c);
      g.drawOval((int)q.x-depth, (int)q.y-depth, 2*depth+1, 2*depth+1);
      double angle = 2.0*Math.PI*Math.random();
      double rndX = 2.0*R*Math.cos(angle) - R;
      double rndY = 2.0*R*Math.sin(angle) - R;
      double tX = q.x + rndX;
      double tY = q.y + rndY;
      g.drawString(""+a, (int) tX, (int) tY);
      g.setColor(Color.GRAY);
      g.drawLine((int) q.x, (int) q.y, (int) tX, (int) tY);
    }
  }

  public static void gui(Box equationBox) {
    TPanel panel = new TPanel(equationBox);
    SwingUtilities.invokeLater(panel);
  }
}
TOP

Related Classes of ketUI.panel.TPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.