All pastes #257704 Raw Edit

Unnamed

public text v1 · immutable
#257704 ·published 2006-11-25 17:53 UTC
rendered paste body
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import sun.audio.*;    
import java.io.*;
/* This is the drawing area of the program.  I also made it the center of the program
 *which handles most cross-class updates because it has all of the pointers to each
 *of the objects,making it an easy place to have all cross-class updates.*/
public class aCanvas extends JComponent implements MouseListener{
    private allAxis axis;
    private allData data;
    private allDataPoints points,line,histo,scatter;
    private infoArea infoDiv;
    private int curCat,scatterCatX,scatterCatY,filter;
    private Color backgroundColor;
    private allLines lines;
   /*When initialized it only needs the info area pointer, otherwise it just loads
    *everything with its starting(default) values.  */
    public aCanvas(infoArea i){
        setPreferredSize(new Dimension(200,200));
        curCat=0;
        scatterCatX=1;
        scatterCatY=2;
        infoDiv=i;
        filter=-1;
        backgroundColor=Color.LIGHT_GRAY;
        data=new loadRestaurantData();
        axis=new histoAxis(this,data,false);
        histo=new histogramPoints(this,data,0,axis,i);
        line=new linePoints(this,data,0,axis,infoDiv);
        points=histo;
        lines=new allLines(this);
        addMouseListener(this);
        repaint();
      }
    /*Calls the draw funtions of the axis and data points because they understand
     *how to drawm themselves, checks to see if they should actually be drawn too
     Draws the background first then allows the others to overwrite ontop of it.*/
    protected void paintComponent(Graphics g){
                Graphics2D g2d = (Graphics2D)g;

        g2d.setColor(getBackground());
        g2d.fill(new Rectangle(0,0,this.getWidth(),this.getHeight()));
        g2d.setColor(Color.black);
       g2d.scale(2.0,2.0);
        if(!axis.hiddenState())
            axis.drawAxis(g);
        if(!data.getHiddenState())
            points.drawData(g);
        g2d.scale(1,1);
    }
    /*This is currently for the combo box to update the category, updates the
     *category then repaints*/
    public void update(String cat){
       if(cat.equals("Food")){
         points.setDataPoints(0);
         curCat=0;
       }
       if(cat.equals("Price")){
         points.setDataPoints(1);
         curCat=1;
       }
       if(cat.equals("Decor")){
         points.setDataPoints(2);
         curCat=2;
       }
       if(cat.equals("Cuisine")){
           points.setDataPoints(3);
           curCat=3;
       }
        repaint();
    }
    /*Tells the axis to update its hidden state(so do the opposite of what it is)*/
    public void updateAxisHiddenState(){
        axis.updateHiddenState();
        repaint();
    }
    /*Tells the data to update its hidden state(so do the opposite of what it is),
     *the points hide or show based on this*/
     public void updateDataHiddenState(){
        data.updateHiddenState();
        repaint();
    }
     /*Tells infoDiv to update how the info is displayed:inline or popup*/
     public void updateInfoDelivery(){
         infoDiv.updateInfoDelivery();
     }
     /*Updates the color of axis*/
    public void updateAxis(Color c){
        axis.setColor(c);
        repaint();
    }
    /*Updates the color of the data points*/
    public void updatePoints(Color c){
        points.setColor(c);
        repaint();
    }
    /*updates the type of graph*/
    public void updateGraphType(String type){
        if(type.equals("Histogram")){
            axis=new histoAxis(this,data,axis.hiddenState());
            //points=new histogramPoints(this,data,0,axis,infoDiv);
            points=histo;
        }
        else if(type.equals("Line")){
            //Line and histogram graphs use the same type of axis, so no need for new class
            axis=new histoAxis(this,data,axis.hiddenState());
            //points=new linePoints(this,data,0,axis,infoDiv);
            points=line;
        }
        else if(type.equals("Scatterx2")){
            axis=new scatterAxisx2(this, data,axis.hiddenState());
           // points=new scatterPointsx2(this,data,1, axis,infoDiv);
        }
        else{
            axis=new scatterAxis(this, data,axis.hiddenState());
            points=new scatterPoints(this,data,1, axis,infoDiv);
        }
        repaint();
    }
    /*updates the x-axis of the scatter plot*/
    public void updateScatterAxisX(String cat){
        if(cat.equals("Food")){
         ((scatterAxis)axis).setXAxis(0);
         scatterCatX=0;
        }
       if(cat.equals("Price")){
         ((scatterAxis)axis).setXAxis(1);
         scatterCatX=1;
       }
       if(cat.equals("Decor")){
         ((scatterAxis)axis).setXAxis(2);
         scatterCatX=2;
       }
       if(cat.equals("Cuisine")){
         ((scatterAxis)axis).setXAxis(3);
         scatterCatX=3;
       }
        repaint();
       
    }
    /*updates the y axis of the scatter plot*/
    public void updateScatterAxisY(String cat){
        if(cat.equals("Food")){
         ((scatterAxis)axis).setYAxis(0);
         scatterCatY=0;
        }
       if(cat.equals("Price")){
         ((scatterAxis)axis).setYAxis(1);
         scatterCatY=1;
       }
       if(cat.equals("Decor")){
         ((scatterAxis)axis).setYAxis(2);
         scatterCatY=2;
       }
       if(cat.equals("Cuisine")){
         ((scatterAxis)axis).setYAxis(3);
         scatterCatY=3;
       }
        
        repaint();
       
    }
    /*sets the filter*/
    public void setFilter(String cat){
        if(cat.equals("Has Takeout"))
            filter=0;
        else if(cat.equals("Starts with A"))
            filter=1;
        else if(cat.equals("American Food"))
            filter=2;
        else
            filter=-1;
        repaint();
    }
    /*returns filter*/
    public int getFilter(){
        return filter;
    }
    /* returns the current category*/
    public int getCurCat(){
        return curCat;
    }
    /*returns x-axis current category*/
    public int getScatterCatX(){
        return scatterCatX;
    }
    /*returns y-axis current category*/
    public int getScatterCatY(){
        return scatterCatY;
    }

    public void setBackground(Color c){
        backgroundColor=c;
        repaint();
    }
    
    public Color getBackground(){
        return backgroundColor;
    }
    public void doZoomIn(){
        axis.zoomIn(this);
    }
    public void doZoomOut(){
        axis.zoomOut(this);
    }
    /*a mouse was clicked, tell the datapoints to handle it*/
    public void mousePressed (MouseEvent event) {       
          if (event.getClickCount() == 2)  
            points.deleteLine(event.getPoint());
          points.doClick(event.getPoint(),event);
    }
    public void mouseClicked (MouseEvent event) {
       
    }
    public void mouseReleased (MouseEvent event) {
        points.doClick(event.getPoint(),event);
    }
    public void mouseEntered (MouseEvent event) {}
    public void mouseExited (MouseEvent event) {}
    
    
}