Package generator

Source Code of generator.MetaballsPanel

package generator;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JLabel;

import basic.CPoint2D;
import basic.Metaballs;

public class MetaballsPanel extends JLabel {
 
  private double xrange, yrange;
  private int width, height;
  public List<CPoint2D> points;
  private Metaballs m;
  private BufferedImage image;

  public MetaballsPanel(int width, int height, double xrange, double yrange) {
    this.xrange = xrange;
    this.yrange = yrange;
    this.width = width;
    this.height = height;
   
    points = new LinkedList<CPoint2D>();
    m = new Metaballs(width, height, xrange, yrange);
    image = m.render();
  }
 
  public void addPoint(CPoint2D point) {
    points.add(point);
    m.addControlPoint(point);
    image = m.render();
    repaint();
  }
 
  public void modifyPoint(CPoint2D before, CPoint2D after) {
    m.removeControlPoint(before);
    points.remove(before);
    addPoint(after);
  }
 
  public void clear() {
    points.clear();
    m.clear();
    image = m.render();
    repaint();
  }
 
  public BufferedImage getImage() {
    return image;
  }
 
  public void paint(Graphics g) {
    super.paint(g);
   
    //m.setWidth(getWidth());
    //m.setHeight(getHeight());
    //m.setXRange(getWidth()/100);
    //m.setYRange(getHeight()/100);
    g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
   
    int x, y;
   
    // paint circles
    g.setColor(Color.black);
    for (CPoint2D p : points) {
      x = (int)((p.x + (xrange/2)) * (width/xrange));
      y = (int)((p.y + (yrange/2)) * (height/yrange));
      System.out.println("drawing (" + x + ", " + y + ")");
     
      g.drawOval(x-5, y-5 , 10, 10);
    }
  }

}
TOP

Related Classes of generator.MetaballsPanel

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.