ABC

Ads

 
 

Share on Google+Share on Google+

Shadab Ahmad Khan
ABC
1 Answer(s)      5 years and 2 months ago
Posted in : HTML

package threeDAssignment;

import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.Box; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel;

@SuppressWarnings("serial") public class LayerOptionDialog extends JDialog implements ActionListener {

public LayerOptionDialog(JFrame owner, String title) {
    super(owner,title);

    setSize(350, 150);

    setLocation(new Point(0, owner.getHeight()-155));

    setResizable(false);

    grid=((DisplayUI)owner).getGrid();

    canvas=((DisplayUI)owner).getCanvas();

    createDialog();

    addListeners();
}

private void addListeners() {
    cmbDirection.addActionListener(this);
    cmbLayer.addActionListener(this);

    btnBack.addActionListener(this);
    btnForward.addActionListener(this);
    btnFastBack.addActionListener(this);
    btnFastForward.addActionListener(this);
    btnStop.addActionListener(this);

    btnReset.addActionListener(this);
    btnClose.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    Object source=e.getSource();

    if(source.equals(cmbDirection))
    {
        if(cmbDirection.getSelectedIndex()>0){
            DefaultComboBoxModel cmbLayerModel=(DefaultComboBoxModel)cmbLayer.getModel();
            cmbLayerModel.removeAllElements();

            if(cmbDirection.getSelectedIndex()==1){
                int NX=grid.getNX();
                for(int i=1; i<=NX; i++){
                    cmbLayerModel.addElement(i);
                }
            }else if(cmbDirection.getSelectedIndex()==2){
                int NY=grid.getNY();
                for(int i=1; i<=NY; i++){
                    cmbLayerModel.addElement(i);
                }
            }
            else if(cmbDirection.getSelectedIndex()==3){
                int NZ=grid.getNZ();
                for(int i=1; i<=NZ; i++){
                    cmbLayerModel.addElement(i);
                }
            }

            cmbLayer.repaint();
        }else{
            DefaultComboBoxModel cmbLayerModel=(DefaultComboBoxModel)cmbLayer.getModel();
            cmbLayerModel.removeAllElements();
            cmbLayer.repaint();
        }
    }
    else if(source.equals(cmbLayer)){
        showGridLayer(0);

    }

    else if(source.equals(btnForward)){
        showGridLayer(1);
    }
    else if(source.equals(btnBack)){
        showGridLayer(-1);
    }
    else if (source.equals(btnFastForward)) {
        isStop=false;
        if (cmbDirection.getSelectedIndex() > 0) {
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        while (Integer.parseInt(cmbLayer.getSelectedItem().toString()) < ((cmbDirection.getSelectedIndex()==1)?grid.getNX():((cmbDirection.getSelectedIndex()==2)?grid.getNY():grid.getNZ()))) {
                            if(isStop)
                                return;
                            showGridLayer(1);
                            if(Integer.parseInt(cmbLayer.getSelectedItem().toString()) == ((cmbDirection.getSelectedIndex()==1)?grid.getNX():((cmbDirection.getSelectedIndex()==2)?grid.getNY():grid.getNZ()))){
                                return;
                            }
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
        }
    }
    else if (source.equals(btnFastBack)) {
        isStop=false;
        if (cmbDirection.getSelectedIndex() > 0) {
            Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        while (Integer.parseInt(cmbLayer.getSelectedItem().toString()) > 0) {
                            if(isStop)
                                return;
                            showGridLayer(-1);
                            if(Integer.parseInt(cmbLayer.getSelectedItem().toString())==1)
                                return;
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
        }
    }
    else if(source.equals(btnReset)){
        cmbDirection.setSelectedIndex(0);
        DefaultComboBoxModel cmbLayerModel=(DefaultComboBoxModel)cmbLayer.getModel();
        cmbLayerModel.removeAllElements();
        cmbLayer.repaint();
        canvas.showLayer(0);
    }
    else if(source.equals(btnClose)){
        this.dispose();
    }
    else if(source.equals(btnStop)){

// t.interrupt(); isStop=true; } }

private void showGridLayer(int i) {
    if(cmbDirection.getSelectedIndex()>0){
        DefaultComboBoxModel cmbLayerModel=(DefaultComboBoxModel)cmbLayer.getModel();
        if(cmbLayerModel.getSize()>0){
            int layer=Integer.parseInt(cmbLayer.getSelectedItem().toString());
            if(cmbDirection.getSelectedIndex()==1){

                if((layer+i)<=grid.getNX() && (layer+i)>0){
                    canvas.showLayer(layer+i);
                    cmbLayer.setSelectedItem(layer+i);
                }

            }else if(cmbDirection.getSelectedIndex()==2){

                if((layer+i)<=grid.getNY() && (layer+i)>0){
                    canvas.showLayer(grid.getNX()+layer+i);
                    cmbLayer.setSelectedItem(layer+i);
                }
            }else if(cmbDirection.getSelectedIndex()==3){

                if((layer+i)<=grid.getNZ() && (layer+i)>0){
                    canvas.showLayer(grid.getNX()+grid.getNY()+layer+i);
                    cmbLayer.setSelectedItem(layer+i);
                }
            }
        }
    }else{
        canvas.showLayer(0);
    }
}

private void createDialog() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc=new GridBagConstraints();

    Insets ins=new Insets(10, 2, 10, 2);

    gbc.anchor=GridBagConstraints.FIRST_LINE_START;
    gbc.fill=GridBagConstraints.BOTH;
    gbc.insets=ins;

    gbc.gridx=0;
    gbc.gridy=0;
    add(new JLabel("Direction :"),gbc);

    cmbDirection=new JComboBox(new Object[]{"Select","X-Direction","Y-Directiion","Z-Direction"});
    gbc.gridx=1;
    gbc.gridwidth=2;
    add(cmbDirection,gbc);

    gbc.gridx=2;
    gbc.gridwidth=1;
    add(Box.createHorizontalStrut(10),gbc);

    gbc.gridx=3;
    add(new JLabel("   Layer :"),gbc);

    cmbLayer=new JComboBox();
    gbc.gridx=4;
    add(cmbLayer,gbc);

    btnFastBack=new JButton("<<<");
    gbc.gridx=0;
    gbc.gridy=1;
    add(btnFastBack,gbc);

    btnBack=new JButton("<<");
    gbc.gridx=1;
    add(btnBack,gbc);

    btnStop=new JButton("...");
    gbc.gridx=2;
    add(btnStop,gbc);

    btnForward=new JButton(">>");
    gbc.gridx=3;
    add(btnForward,gbc);

    btnFastForward=new JButton(">>>");
    gbc.gridx=4;
    add(btnFastForward,gbc);

    gbc.gridx=0;
    gbc.gridy=2;
    gbc.gridwidth=2;
    add(Box.createHorizontalStrut(50),gbc);

    btnReset=new JButton("Reset");
    gbc.gridx=1;
    gbc.gridwidth=1;
    add(btnReset,gbc);

    gbc.gridx=2;
    add(Box.createHorizontalStrut(50),gbc);

    btnClose=new JButton("Close");
    gbc.gridx=3;
    add(btnClose,gbc);
}

private Grid grid;
private ThreeDCanvas canvas;

private JComboBox cmbLayer;
private JComboBox cmbDirection;

private JButton btnBack;
private JButton btnForward;
private JButton btnFastBack;
private JButton btnFastForward;
private JButton btnStop;
private JButton btnReset;
private JButton btnClose;

private boolean isStop=false;

}

Ads
View Answers

August 17, 2012 at 11:27 PM


package twoDAssignment;

import java.awt.BasicStroke; import java.awt.Color; import java.awt.FontMetrics; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.RenderingHints; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.text.DecimalFormat; import java.util.ArrayList;

import javax.swing.BoxLayout; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.SwingUtilities;

/** * this class contains the method that plot the wells in selected drawing * region and performs operations like zoom/unzoom/pan on the drawings. * * */ @SuppressWarnings("serial") public class Canvas extends JLabel implements MouseMotionListener, MouseListener {

private BufferedImage image = null;
private Color[] colors = { new Color(125, 125, 125, 100), new Color(0, 0, 255, 100),
        new Color(255, 0, 0, 100), new Color(0, 255, 0, 100) };

private int pannedX=0;
private int pannedY=0;
private int draggedX=0;
private int draggedY=0;

public Canvas() {

    wellModel = new WellModel();

    plottedWellList = new ArrayList<Rectangle2D>();

    setDoubleBuffered(true);

    popTooltip = new JPopupMenu();

    addListners();

    setOpaque(true);

    setBackground(Color.WHITE);
}

/**
 * sets the wellList that is to plot. and minimum/maximum value of X,Y-location
 * of the well.
 * @param wellList : ArrayList of Well
 */
public void setWellList(ArrayList<Well> wellList) {

    wellModel.setWellList(wellList);
    setXMinMax();
    setYMinMax();

    strXTicks=Double.toString(wellModel.getxMin());
    strYTicks=Double.toString(wellModel.getyMin());
}

/**
 * method used to add the listeners.
 */
public void addListners() {
    addMouseMotionListener(this);
    addMouseListener(this);
}


@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (wellModel.getWellList() == null || !(wellModel.getWellList().size() > 0)) {
        return;
    }
    width = getWidth();
    height = getHeight();

    Graphics2D g2 = (Graphics2D) g;

    if (propertyIndex >= 0) {
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(1.0f));

        plotAreaWidth = (width * 4) / 5; //drawing area width
        plotAreaHeight = (height * 4) / 5; //drawing area height

        setTicksNo(g2);

        int extra = plotAreaWidth % (wellModel.getxTicksNo() - 1);
        plotAreaWidth = plotAreaWidth - extra;

        extra = plotAreaHeight % (wellModel.getyTicksNo() - 1);
        plotAreaHeight = plotAreaHeight - extra;

        //initialize image if it is null and initialize draw the plot area rectangle.
        if (image == null) {
            plotArea = new Rectangle2D.Float(width / 10, height / 10, plotAreaWidth, plotAreaHeight);
            image = getPlotImage(plotAreaWidth, plotAreaHeight);

        }
        g2.setClip(plotArea);
        g2.drawImage(image, width / 10, height / 10 , null);
        g2.setClip(null);
        g2.draw(plotArea);

        g2.setColor(new Color(colors[propertyIndex%4].getRed(),
                colors[propertyIndex%4].getGreen(), colors[propertyIndex%4]
                                                        .getBlue(), 10));
        g2.fill(plotArea);

        drawAxisPolygon(g2);

        drawAxis(g2);
    }

}

/**
 * method used to sets the no. of possible major ticks on the axis in both x, and y direction.
 * @param g2 : Graphics2D
 */
private void setTicksNo(Graphics2D g2) {
    FontMetrics fm=g2.getFontMetrics();

    int xTicksWidth=fm.stringWidth(strXTicks)+20;
    int yTicksWidth=fm.stringWidth(strYTicks);

    int xTicksNo=plotAreaWidth/xTicksWidth;
    int yTicksNo=plotAreaHeight/yTicksWidth;

    wellModel.setxTicksNo(xTicksNo);
    wellModel.setyTicksNo(yTicksNo);
}

/**
 * method used to draw the outer polygons.
 * @param g2 : Graphics2D
 */
private void drawAxisPolygon(Graphics2D g2) {
    Graphics2D g3=(Graphics2D)g2.create();
    g3.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g3.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    //left polygon
    int[] xpointsLeft={0,width/10,width/10,0,0};
    int[] ypointsLeft={0,height/10,height/10+plotAreaHeight,getHeight(),0};
    int npointsLeft=5;

    GradientPaint paint=new GradientPaint(0,0, Color.WHITE, getWidth(), 0,new Color(colors[propertyIndex%4].getRed(),colors[propertyIndex%4].getGreen(),colors[propertyIndex%4].getBlue(),0));
    g3.setPaint(paint);

    Polygon leftPoly=new Polygon(xpointsLeft, ypointsLeft, npointsLeft);
    g3.fillPolygon(leftPoly);


    //bottom polygon
    int[] xpointsBottom={0,width/10,width/10+plotAreaWidth,getWidth(),0};
    int[] ypointsBottom={getHeight(),height/10+plotAreaHeight+1,height/10+plotAreaHeight,getHeight(),getHeight()};
    int npointsBottom=5;

    paint=new GradientPaint(0,getHeight(), Color.WHITE, getWidth(), getHeight(),new Color(colors[propertyIndex%4].getRed(),colors[propertyIndex%4].getGreen(),colors[propertyIndex%4].getBlue(), 0));

    Polygon bottomPoly=new Polygon(xpointsBottom, ypointsBottom, npointsBottom);
    g3.setPaint(paint);
    g3.fill(bottomPoly);


    //top polygon
    int[] xpointsTop={0,getWidth(),plotAreaWidth+width/10,width/10,0};
    int[] ypointsTop={0,0,height/10,height/10,0};
    int npointsTop=5;
    paint=new GradientPaint(0, 0, Color.WHITE, getWidth(), 0, new Color(colors[propertyIndex%4].getRed(),colors[propertyIndex%4].getGreen(),colors[propertyIndex%4].getBlue(),0));

    Polygon topPoly=new Polygon(xpointsTop, ypointsTop, npointsTop);
    g3.setPaint(paint);
    g3.fill(topPoly);


    //right polygon
    int[] xpointsRight={getWidth(),plotAreaWidth+width/10,plotAreaWidth+width/10+1,getWidth(),getWidth()};
    int[] ypointsRight={0,height/10,plotAreaHeight+height/10,getHeight(),0};
    int npointsRight=5;
    paint=new GradientPaint(0, getHeight(), Color.WHITE, getWidth(), getHeight(), new Color(colors[propertyIndex%4].getRed(),colors[propertyIndex%4].getGreen(),colors[propertyIndex%4].getBlue(),0));

    Polygon rightPoly=new Polygon(xpointsRight, ypointsRight, npointsRight);
    g3.setPaint(paint);
    g3.fill(rightPoly);
    g3.dispose();
}

/**
 * method used to get plot image.
 * @param imgWidth : image width.
 * @param imgHeight : image height.
 * @return BufferedImage.
 */
private BufferedImage getPlotImage(int imgWidth, int imgHeight) {
    BufferedImage bfImage = new BufferedImage(imgWidth, imgHeight,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bfImage.createGraphics();

    plottedWellList.clear();

    ArrayList<Well> wellList = wellModel.getWellList();
    for (Well well : wellList) {
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        Rectangle2D rectWell = getWellRectangle(well, g2);
        plottedWellList.add(rectWell);
        g2.fill(rectWell);
        g2.drawString(well.getName(), (int) rectWell.getCenterX() + 3,
                (int) rectWell.getCenterY() + 3);

        if (propertyIndex > 0) {
            double maxProp = getMaxProp();

            Ellipse2D propBubble = getPropBubble(well, maxProp, g2);

            g2.setColor(colors[propertyIndex%4].darker().darker());

            g2.draw(propBubble);
            g2.setColor(colors[propertyIndex%4]);
            g2.fill(propBubble);

        }

    }

    return bfImage;
}

/**
 * method used to get the maximum property value.
 * @return double : maximum property value.
 */
private double getMaxProp() {
    ArrayList<Well> wellList = wellModel.getWellList();
    double maxProp = wellList.get(0).getAlProperties().get(
            propertyIndex - 1);
    for (Well well : wellList) {
        if (well.getAlProperties().get(propertyIndex - 1) > maxProp)
            maxProp = well.getAlProperties().get(propertyIndex - 1);
    }
    return maxProp;
}

/**
 * method used to draw the axes.
 * @param g2 : Graphics2D
 */
private void drawAxis(Graphics2D g2) {
    g2.setColor(Color.BLACK);

    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    df.setGroupingUsed(false);

    double majorXTicksWidth = Double.parseDouble(df.format(plotAreaWidth / (wellModel.getxTicksNo() - 1)));
    double minorXTicksWidth = Double.parseDouble(df.format(majorXTicksWidth / (WellConstants.MINOR_TICKS + 1)));

    double majorYTicksWidth = Double.parseDouble(df.format(plotAreaHeight / (wellModel.getyTicksNo() - 1)));
    double minorYTicksWidth = Double.parseDouble(df.format(majorYTicksWidth / (WellConstants.MINOR_TICKS + 1)));

    float xStart = width / 10;
    float yStart = (height / 10) + plotAreaHeight;

    Line2D majorTicksLine;
    Line2D minorTicksLine;

    String ticksValue;
    FontMetrics fontMetrics = g2.getFontMetrics();

    double xInterval = Double.parseDouble(df.format((wellModel.getxMax() - wellModel.getxMin()) / (wellModel.getxTicksNo() - 1)));
    double yInterval = Double.parseDouble(df.format((wellModel.getyMax() - wellModel.getyMin()) / (wellModel.getyTicksNo() - 1)));

    //x-axis
    for (int i = 0; i < wellModel.getxTicksNo()-1; i++)
    {
        majorTicksLine = new Line2D.Double(xStart + (majorXTicksWidth * i), yStart, xStart + (majorXTicksWidth * i), yStart + WellConstants.MAJOR_TICKS_HEIGHT);
        g2.draw(majorTicksLine);
        ticksValue = df.format(wellModel.getxMin() + (xInterval * i));
        g2.drawString(ticksValue,(float) (xStart + (majorXTicksWidth * i)), (float) (yStart + 2 * WellConstants.MAJOR_TICKS_HEIGHT));

        for (int j = 0; j < 4; j++) 
        {
            minorTicksLine = new Line2D.Double(xStart + (majorXTicksWidth * i) + (minorXTicksWidth * (j + 1)), yStart, xStart + (majorXTicksWidth * i) + (minorXTicksWidth * (j + 1)), yStart + WellConstants.MINOR_TICKS_HEIGHT);
            g2.draw(minorTicksLine);
        }
    }

    //y-axis
    for (int i = 0; i < wellModel.getyTicksNo()-1; i++)
    {
        majorTicksLine = new Line2D.Double(xStart, yStart - (majorYTicksWidth * i), xStart - WellConstants.MAJOR_TICKS_HEIGHT, yStart - (majorYTicksWidth * i));
        g2.draw(majorTicksLine);
        ticksValue = df.format(wellModel.getyMin() + (yInterval * i));
        g2.drawString(ticksValue, (float) (xStart - WellConstants.MAJOR_TICKS_HEIGHT - fontMetrics.stringWidth(ticksValue)),(float) (yStart - (majorYTicksWidth * i)));

        for (int j = 0; j < 4; j++) 
        {
            minorTicksLine = new Line2D.Double(xStart,(yStart - (majorYTicksWidth * i)) - (minorYTicksWidth * (j + 1)), xStart - WellConstants.MINOR_TICKS_HEIGHT,(yStart - (majorYTicksWidth * i)) - (minorYTicksWidth * (j + 1)));
            g2.draw(minorTicksLine);
        }
    }

    //last ticks of x-axis
    majorTicksLine = new Line2D.Double(xStart + plotAreaWidth, yStart, xStart + plotAreaWidth, yStart + WellConstants.MAJOR_TICKS_HEIGHT);
    g2.draw(majorTicksLine);
    ticksValue = df.format(wellModel.getxMax());
    g2.drawString(ticksValue, (float) (xStart + plotAreaWidth), (float) (yStart + 2 * WellConstants.MAJOR_TICKS_HEIGHT));

    //last ticks of y-axis.
    majorTicksLine = new Line2D.Double(xStart, yStart - plotAreaHeight, xStart - WellConstants.MAJOR_TICKS, yStart - plotAreaHeight);
    g2.draw(majorTicksLine);
    ticksValue = df.format(wellModel.getyMax());
    g2.drawString(ticksValue, (float) (xStart - WellConstants.MAJOR_TICKS_HEIGHT - fontMetrics.stringWidth(ticksValue)), (float) (yStart - plotAreaHeight));
}

/**
 * method used to get the property bubble.
 * @param well : whose property bubble is requied to draw.
 * @param maxProp : maximum property value.
 * @param g2 : Graphics2D.
 * @return Ellipse2D.
 */
private Ellipse2D getPropBubble(Well well, double maxProp, Graphics2D g2) {

    float[] scaledValue = getScaledValue(well.getxLoc(), well.getyLoc());
    double bubbleRadius = (well.getAlProperties().get(propertyIndex - 1) * WellConstants.MAX_BUBBLE_RADIUS) / maxProp;
    Ellipse2D propBubble = new Ellipse2D.Double(scaledValue[0] - (bubbleRadius / 2), scaledValue[1] - (bubbleRadius / 2), bubbleRadius, bubbleRadius);

    return propBubble;
}

/**
 * method used to get well rectangle.
 * @param well
 * @param g2
 * @return Rectangle2D.
 */
private Rectangle2D getWellRectangle(Well well, Graphics2D g2) {
    g2.setColor(Color.BLACK);

    float[] scaledValue = getScaledValue(well.getxLoc(), well.getyLoc());
    Rectangle2D wellRectangle = new Rectangle2D.Float(scaledValue[0] - 2, scaledValue[1] - 2, 4, 4);

    return wellRectangle;
}

/**
 * this method is used to convert real value to the pixel value.
 * @param xLoc : real x
 * @param yLoc : real y
 * @return float array containing scaled values of real x and y.
 */
private float[] getScaledValue(double xLoc, double yLoc) {
    float[] scaledValue = new float[2];

    double xdiff = wellModel.getxMax() - wellModel.getxMin();
    double ydiff = wellModel.getyMax() - wellModel.getyMin();

    scaledValue[0] = (float) ((xLoc - wellModel.getxMin()) * (plotAreaWidth / xdiff));
    scaledValue[1] = (float) ((yLoc - wellModel.getyMin()) * (plotAreaHeight / ydiff));

    return scaledValue;
}

/**
 * this method is used to convert pixel value to the real value.
 * @param xLoc : scaled x
 * @param yLoc : scaled y
 * @return double array containing real values of scaled x and y.
 */
private double[] getRealValue(float xScaled, float yScaled) {
    double[] realValue = new double[2];

    double xdiff = wellModel.getxMax() - wellModel.getxMin();
    double ydiff = wellModel.getyMax() - wellModel.getyMin();

    realValue[0] = (double) (xdiff / plotAreaWidth) * (xScaled - width / 10) + wellModel.getxMin();
    realValue[1] = (double) (ydiff / plotAreaHeight)* (yScaled - height / 10) + wellModel.getyMin();

    return realValue;
}

/**
 * this method sets the minimum and maximum value of x in model.
 */
private void setXMinMax() {
    double xMin = 0.0;
    double xMax = 0.0;

    ArrayList<Well> wellList = wellModel.getWellList();
    xMin = wellList.get(0).getxLoc();
    xMax = wellList.get(0).getxLoc();

    for (Well well : wellList) {
        if (well.getxLoc() < xMin)
            xMin = well.getxLoc();
        if (well.getxLoc() > xMax)
            xMax = well.getxLoc();
    }

    wellModel.setxMin(xMin);
    wellModel.setxMax(xMax);
}

/**
 * this method sets the minimum and maximum value of y in model.
 */
private void setYMinMax() {
    double yMin = 0.0;
    double yMax = 0.0;

    ArrayList<Well> wellList = wellModel.getWellList();
    yMin = wellList.get(0).getyLoc();
    yMax = wellList.get(0).getyLoc();

    for (Well well : wellList) {
        if (well.getyLoc() < yMin)
            yMin = well.getyLoc();
        if (well.getyLoc() > yMax)
            yMax = well.getyLoc();
    }

    wellModel.setyMin(yMin);
    wellModel.setyMax(yMax);
}

/**
 * this method sets the index of selected property.
 */
public void setIndex(int index) {
    image=null;
    this.propertyIndex = index;
}

@Override
public void mouseDragged(MouseEvent e) {

    endX = e.getX();
    endY = e.getY();

    if (plotArea == null)
        return;

    if (SwingUtilities.isLeftMouseButton(e)) {
        if (!plotArea.contains(initX, initY))
            return;
        if (!plotArea.contains(endX, endY))
            return;


        //selection rectangle
        Rectangle2D.Double rectSelectionArea = new Rectangle2D.Double();
        rectSelectionArea.setFrameFromDiagonal(initX, initY, endX, endY);
        paintImmediately(0, 0, this.getWidth(), this
                .getHeight());
        Graphics2D g3 = (Graphics2D) this.getGraphics().create();
        g3.setPaint(new Color(colors[propertyIndex%4].getRed(), colors[propertyIndex%4].getGreen(), colors[propertyIndex%4].getBlue(), 15));

        g3.fill(rectSelectionArea);
        g3.setStroke(new BasicStroke(0.70f));
        g3.setColor(new Color(colors[propertyIndex%4].getRed(), colors[propertyIndex%4].getGreen(), colors[propertyIndex%4].getBlue(), 70));
        g3.draw(rectSelectionArea);
        g3.dispose();
    }
    if (SwingUtilities.isRightMouseButton(e)) {

        double[] startDrag = getRealValue(initX, initY);
        double[] endDrag = getRealValue(endX, endY);
        draggedX=(int)(endDrag[0]-startDrag[0]);    //total drag in x-direction
        draggedY=(int)(endDrag[1]-startDrag[1]);    //total drag in y-direction
        pan();
        pannedX = draggedX;     //last panned amount in x-direction
        pannedY = draggedY;     //last panned amount in y-direction
    }

}

@Override
public void mouseMoved(MouseEvent e) {
    int xLoc = e.getX();
    int yLoc = e.getY();

    int wellIndex = -1;

    if (plottedWellList != null && plotArea != null) {
        if (!plotArea.contains(xLoc, yLoc)) {
            return;
        }

        for (Rectangle2D wellRect : plottedWellList) {
            if (wellRect.contains(xLoc - width / 10, yLoc - height / 10)) {
                wellIndex = plottedWellList.indexOf(wellRect);
                break;
            }
        }
        if (wellIndex >= 0 && wellIndex < wellModel.getWellList().size()) {
            Well well = wellModel.getWellList().get(wellIndex);
            popTooltip.removeAll();
            JPanel pnl = new JPanel();
            BoxLayout box = new BoxLayout(pnl, BoxLayout.Y_AXIS);

            pnl.setLayout(box);

            pnl.add(new JLabel("Well Name : " + well.getName()));
            pnl.add(new JLabel("X Location : " + well.getxLoc()));
            pnl.add(new JLabel("Y Location : " + well.getyLoc()));
            if(propertyIndex>0){
                pnl.add(new JLabel("Property"+propertyIndex+" : "+well.getAlProperties().get(propertyIndex-1)));
            }

            popTooltip.add(pnl);

            popTooltip.show(e.getComponent(), xLoc, yLoc);

        } else {
            popTooltip.setVisible(false);
        }
    }
}

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
    initX = e.getX();
    initY = e.getY();
}

@Override
public void mouseReleased(MouseEvent e) {
    if (plotArea == null)
        return;

    if (SwingUtilities.isLeftMouseButton(e)) {
        zoom();
    }

    pannedX=0;
    pannedY=0;
}

/**
 * method used to pan the plot image.
 */
private void pan() {
    if (!plotArea.contains(initX, initY)) {
        return;
    }
    if (!plotArea.contains(endX, endY)) {
        return;
    }
    if ((initX == endX) || (initY == endY)) {
        return;
    }

    wellModel.setPannedXMin(wellModel.getxMin()-draggedX + pannedX);
    wellModel.setPannedXMax(wellModel.getxMax()-draggedX + pannedX);
    wellModel.setPannedYMin(wellModel.getyMin()-draggedY + pannedY);
    wellModel.setPannedYMax(wellModel.getyMax()-draggedY + pannedY);
    wellModel.setPanned(true);
    image=null;
    paintImmediately(0, 0, this.getWidth(), this.getHeight());
}

/**
 * method used to zoom the plot image.
 */
private void zoom() {

    if (!plotArea.contains(initX, initY)) {
        return;
    }
    if (!plotArea.contains(endX, endY)) {
        return;
    }
    if ((initX == endX) || (initY == endY)) {
        return;
    }
    if ((initX > endX) || (initY > endY)) {
        unZoom();
        return;
    }

    double[] startDrag = getRealValue(initX, initY);
    double[] endDrag = getRealValue(endX, endY);

    wellModel.setZoomedXMin(startDrag[0]);
    wellModel.setZoomedYMin(startDrag[1]);
    wellModel.setZoomedXMax(endDrag[0]);
    wellModel.setZoomedYMax(endDrag[1]);

    wellModel.setZoomed(true);
    image=null;
    paintImmediately(0, 0, getWidth(), getHeight());

}

/**
 * method used to unzoom the plot image.
 */
private void unZoom() {

    wellModel.setZoomed(false);
    wellModel.setPanned(false);
    pannedX=0;
    pannedY=0;
    image=null;
    paintImmediately(0, 0, getWidth(), getHeight());
}

private WellModel wellModel;

private ArrayList<Rectangle2D> plottedWellList;
private JPopupMenu popTooltip;
private int propertyIndex = -1;
private int width;
private int height;

private Rectangle2D plotArea;

private int plotAreaWidth;
private int plotAreaHeight;
private String strXTicks;
private String strYTicks;
private float initX, initY, endX, endY;

}


Ads









Related Tutorials/Questions & Answers:
ABC
ABC
Advertisements
Version of com.rklaehn>abc-collection_2.11 dependency
List of Version of com.rklaehn>abc-collection_2.11 dependency
Version of com.rklaehn>abc-collection_2.12 dependency
List of Version of com.rklaehn>abc-collection_2.12 dependency
Version of com.rklaehn>abc-collection_sjs0.6_2.11 dependency
List of Version of com.rklaehn>abc-collection_sjs0.6_2.11 dependency
Version of com.rklaehn>abc-collection_sjs0.6_2.12 dependency
List of Version of com.rklaehn>abc-collection_sjs0.6_2.12 dependency
Version of com.rklaehn>abc-extras_2.11 dependency
List of Version of com.rklaehn>abc-extras_2.11 dependency
Version of com.rklaehn>abc-extras_2.12 dependency
List of Version of com.rklaehn>abc-extras_2.12 dependency
Version of com.rklaehn>abc-extras_sjs0.6_2.11 dependency
List of Version of com.rklaehn>abc-extras_sjs0.6_2.11 dependency
Version of com.rklaehn>abc-extras_sjs0.6_2.12 dependency
List of Version of com.rklaehn>abc-extras_sjs0.6_2.12 dependency
Version of com.rklaehn>abc-laws_2.11 dependency
List of Version of com.rklaehn>abc-laws_2.11 dependency
Version of com.rklaehn>abc-laws_2.12 dependency
List of Version of com.rklaehn>abc-laws_2.12 dependency
Version of com.rklaehn>abc-laws_sjs0.6_2.11 dependency
List of Version of com.rklaehn>abc-laws_sjs0.6_2.11 dependency
Version of com.rklaehn>abc-laws_sjs0.6_2.12 dependency
List of Version of com.rklaehn>abc-laws_sjs0.6_2.12 dependency
Maven Dependency abc-collection_2.11 >> 0.2.1
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_2.11 version0.2.1 in your project
Maven Dependency abc-collection_2.11 >> 0.2.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_2.11 version0.2.0 in your project
Maven Dependency abc-collection_sjs0.6_2.11 >> 0.2.1
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_sjs0.6_2.11 version0.2.1 in your project
Maven Dependency abc-collection_sjs0.6_2.11 >> 0.3.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_sjs0.6_2.11 version0.3.0 in your project
Maven Dependency abc-extras_2.11 >> 0.2.1
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-extras_2.11 version0.2.1 in your project
Maven Dependency abc-extras_2.11 >> 0.3.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-extras_2.11 version0.3.0 in your project
Maven Dependency abc-extras_sjs0.6_2.11 >> 0.3.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-extras_sjs0.6_2.11 version0.3.0 in your project
Maven Dependency abc-extras_sjs0.6_2.11 >> 0.2.1
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-extras_sjs0.6_2.11 version0.2.1 in your project
Maven Dependency abc-laws_2.11 >> 0.2.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-laws_2.11 version0.2.0 in your project
Maven Dependency abc-laws_2.11 >> 0.2.1
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-laws_2.11 version0.2.1 in your project
Maven Dependency abc-laws_sjs0.6_2.11 >> 0.2.1
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-laws_sjs0.6_2.11 version0.2.1 in your project
Maven Dependency abc-laws_sjs0.6_2.11 >> 0.3.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-laws_sjs0.6_2.11 version0.3.0 in your project
Maven Dependency abc-laws_sjs0.6_2.12 >> 0.4.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-laws_sjs0.6_2.12 version0.4.0 in your project
Maven Dependency abc-collection_2.11 >> 0.3.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_2.11 version0.3.0 in your project
Maven Dependency abc-collection_2.12 >> 0.4.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_2.12 version0.4.0 in your project
Maven Dependency abc-collection_sjs0.6_2.11 >> 0.2.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_sjs0.6_2.11 version0.2.0 in your project
Maven Dependency abc-collection_sjs0.6_2.12 >> 0.4.0
You should include the dependency code given in this page to add Maven Dependency of com.rklaehn >> abc-collection_sjs0.6_2.12 version0.4.0 in your project

Ads

 
Advertisement null

Ads