import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

public class selectGraphGridZoom extends JPanel implements MouseListener, 
MouseMotionListener{

        private static final long serialVersionUID = 1L;
        private double maxDepth = Math.random() * 200;
        private double maxKnots = 4;


        // 1 inch = 72
        // Setup the spacing for the cells
        float ySpacing = 36;                                                    
                        // Use this number to fit to screen (72 is equal to 1 
inch)
        float height = 0.0f;                                                    
                        // Start at 72 and make 20 feet 1 inch
        float length = (72 * (float)maxKnots) + 72;                             
        // Start at 72 and make 1 knot 1 inch
        float startX = 72f;
        float startY = 72f;
        boolean isMouseDraggingBox = false;                                     
                // Test to see if mouse is dragging
        int posX, posY;                                                         
        // Start position x of the rectangle
        int mX, mY;                                                             
                                        // Most recent position of the mouse


        public selectGraphGridZoom() {
                super();
                initialize();
        }


        private void initialize() {
                this.setSize(800, 800);
                this.setLayout(new GridBagLayout());

                posX = 72;
                posY = 36;

                addMouseListener( this );
            addMouseMotionListener( this );
        }

        public void paint(Graphics g) {

                setAxisHeight();
                Graphics2D g2d = (Graphics2D)g;

                Line2D lineY = new Line2D.Double( startX, startY, 72, height + 
72);
                Line2D lineTopX = new Line2D.Double( startX, startY, length, 
72);
                Line2D lineBottomX = new Line2D.Double( 72, height + 72, 
length, height + 72);



                // Create dotted lines for the grid system
                Stroke strokeGrid = new BasicStroke(1,
                                                                                
BasicStroke.CAP_BUTT,
                                                                                
BasicStroke.JOIN_BEVEL,
                                                                                
0,
                                                                                
new float[] { 1 , 1 },
                                                                                
0);

                Stroke strokeRect = new BasicStroke(1,
                                                                                
BasicStroke.CAP_BUTT,
                                                                                
BasicStroke.JOIN_BEVEL,
                                                                                
0,
                                                                                
new float[] { 2 , 2 },
                                                                                
0);

                g2d.draw(lineY);
                g2d.draw(lineTopX);
                g2d.draw(lineBottomX);
                g2d.setColor(Color.blue);
                graphData(g2d);
                g2d.setColor(Color.black);
                g2d.setStroke(strokeGrid);
                g2d.draw(createGrid());

                labelAxis(g2d);

                // Draw the rectangle on the graph
                g2d.setColor(Color.magenta);
                g2d.setStroke(strokeRect);
                g2d.draw(createRectangle());

        }

        // Create a grid system for the graph
        private Shape createGrid() {
                GeneralPath path = new GeneralPath();                           
                                                        //Create a General path 
to make the grid

                // Make the YAxis grid
                for( float x = ySpacing; x <= height; x+= ySpacing){


                        path.moveTo(startX, x + startY);                        
                                                                // Move to the 
start position
                        path.lineTo(length, x + startY);                        
                                                                // Draw the 
line with the start point being the moveTo
                }
                // Make the XAxis grid
                for ( int x = 72; x < length; x += 72) {
                        path.moveTo(startX + x, startY);                        
                                                                // Move to the 
start position
                        path.lineTo(x + startX, height + 72);                   
                                                        // Draw the line with 
the start point being the moveTo
                }
                return path;
        }

        // Label the axis with there numbers
                private void labelAxis(Graphics2D g2d) {
                        int xLabel = 0;                                         
                                                                                
// The label for the x Axis
                        int yLabel = 0;                                         
                                                                                
// The label for the y Axis
                        Font font = new Font("Serif", Font.BOLD, 12);           
                                                // Font properties
                        int shiftText = 4;                                      
                                                                                
// Used to align the text with the line

                        g2d.setFont(font);                                      
                                                                                
// Set the font for the text
                        g2d.drawString("Current (Knots)", length/2, 36);        
                                                // Display the label for the y 
Axis

                        //Y Axis Labels
                        for( int x = shiftText; x <= height + shiftText; x += 
ySpacing){
                                g2d.drawString(Integer.toString(yLabel), startX 
- 24, startY + x);              // Write the text to the screen
                                yLabel += 10;                                   
                                                                                
// Increment the text
                        }
                        // x Axis Labels
                        for ( int x = -shiftText; x < length - shiftText; x += 
72) {
                                g2d.drawString(Integer.toString(xLabel), startX 
+ x, (startY - shiftText) - 15);        // Write the text to the screen
                                xLabel += 1;                                    
                                                                                
// Increment the text
                        }

                        g2d.translate(36, (height/2) + 72 + 36);                
                                                        // Move to the position 
to display the label
                        g2d.rotate(Math.toRadians(270));                        
                                                                // Rotate the 
label
                        g2d.drawString("Depth (ft)" ,0, 0);                     
                                                                // Display the 
label

                        // Return back to origin
                        g2d.rotate(Math.toRadians(-270));
                        g2d.translate(-36, -((height/2) + 72));
                }
                // Graph the data from the the array created of points
                private void graphData(Graphics2D g2d) {
                        Point2D[] mPoints = setDataPointArray();                
                                                        // Get the array of 
points
                        for (int x = 0; x < mPoints.length-1; x++  ) {
                                Line2D line = new Line2D.Double(mPoints[x], 
mPoints[x+1]);                              // Graph each point to its last 
point
                                g2d.draw(line);                                 
                                                                                
// Draw the line on the screen
                        }

                }
                // Creates an array of Points to graph
                private Point2D[] setDataPointArray() {
                        double[] histData = getHistoricalData(getCellNum());    
                                                                // Get the data 
array
                        Point2D[] mPoints = new Point2D[histData.length];       
                                                // Create an array of points
                        double yCoord = startY - 18;                            
                                                                // Get start 
position ( y - 18 because want to start at 0)
                        for(int x = 0; x < histData.length; x++) {              
                                                        // Cycles through the 
array
                                mPoints[x] = new Point2D.Double(histData[x] * 
72, yCoord += 18 );               // Gives an x coord and y coord
                        }                                                       
                                                                                
                // X is mulitiplied by 72 because that is 1 cell
                                                                                
                                                                                
                // Y is added to 18 because that is .5 of 1 cell
                        return mPoints;                                         
                                                                                
// Return the array of points
                }
                // Create an array of random data to graph
                public double[] getHistoricalData(int getCellNum) {
                        double[] velocity = new double[getCellNum()];           
                                                // Create an array to store data
                        for( int x = 0; x < getCellNum(); x++){                 
                                                        // Determine number 
items by finding the depth
                                velocity[x] = (Math.random()) + 1;              
                                                                // Put random 
numbers in ranging from around 1
                        }
                        return velocity;                                        
                                                                                
// Return the array of random data

                }

                // Find the number of cells needed to create array
                public int getCellNum() {
                        if (maxDepth % 5 != 0)                                  
                                                                        // 
Determine if the number is divisable by 5
                                return (int)(maxDepth/5.0) + 1;                 
                                                                // If not add 1 
more to the cell number
                        else
                                return (int)(maxDepth/5.0);                     
                                                                        // 1 
cell is equal to 5 feet in the array
                }
                // Find the maximum number needed to fit everything in graph
                public void setAxisHeight() {
                        float currentHeight = 0.0f;                             
                                                                        // 
Stores current height needed
                        int remainder =  10 - ((int)maxDepth % 10);             
                                                        // Find how much to 
make a divisable of 10
                        int depth = (int)maxDepth + remainder;                  
                                                        // Make the number a 
divisable of 10
                        currentHeight = (((depth)/10) * ySpacing);              
                                                        // Convert the number 
into the spacing for graph

                        this.height = currentHeight;                            
                                                                // Set the 
height to found number
                }

                // Create a Rectangle to select on graph
                public Shape createRectangle() {
                        int x = 72 * 2;                                         
                                                                                
// This will hightlight 2 knots
                        int y = 72;                                             
                                                                                
        // This will highlight 20 feet
                        if(maxDepth < 10) {
                                x = 72;
                                y = 36;
                        }
                        return new Rectangle2D.Double(posX, posY, x, y);

                }

                public void mouseClicked(MouseEvent e) {
                        // Not needed for drag selection
                }

                public void mouseEntered(MouseEvent e) {
                        // Not needed for drag selection
                }

                public void mouseExited(MouseEvent e) {
                        // Not needed for drag selection
                }

                public void mousePressed(MouseEvent e) {
                        mX = e.getX();                                          
                                                                                
// Found where the mouse is X
                        mY = e.getY();                                          
                                                                                
// Found where the mouse is Y
                        System.out.println("x = " + mX + " y = " + mY);

                        if(posX < mX && mX < posX + 72 + 72 && posY < mY && mY 
< posY + 36 + 72) {                      // Click within rectangle
                                isMouseDraggingBox = true;                      
                                                                                
        // Set the value to true
                                System.out.println("true");
                        }
                        e.consume();                                            
                                                                                
// Process the event
                }

                public void mouseReleased(MouseEvent e) {
                        isMouseDraggingBox = false;
                        e.consume();
                        //repaint();
                        //displayZoomView();
                }

                public void mouseDragged(MouseEvent e) {
                        if (isMouseDraggingBox) {
                                // Get new mouse position
                                int newMX = e.getX();
                                int newMY = e.getY();

                                // Move the rectangle to the new mouse position
                                posX += newMX - mX;
                                posY += newMY - mY;

                                // Update the new mouse position
                                mX = newMX;
                                mY = newMY;

                                repaint();
                                e.consume();
                        }

                }

                public void mouseMoved(MouseEvent e) {
                        // Not needed for drag selection
                        //System.out.println("new posx = " + posX + " new posY 
= " + (posY+36));
                }


                public void update(Graphics g) {
                        Graphics2D g2d = (Graphics2D)g;
                        Stroke strokeRect = new BasicStroke(1,                  
                                        // Width of stroke
                                                                                
                BasicStroke.CAP_BUTT,                   // End cap style
                                                                                
                BasicStroke.JOIN_BEVEL,                 // Join style
                                                                                
                0,                                                              
// Miter limit
                                                                                
                new float[] { 2 , 2 },                  // Dash pattern
                                                                                
                0);                                                             
// Dash phase

                        // Draw the rectangle on the graph
                        g2d.setColor(Color.magenta);
                        g2d.setStroke(strokeRect);
                        g2d.draw(createRectangle());
                }

                private void displayZoomView() {
                        // displays the smaller view
                        getZoomViewLabels();
                }

                // Create a smaller Grid System for the zoomed view
                private void getZoomViewLabels() {
                        double xLabelStart = 0.0;
                        xLabelStart = ((posX - 72)/72);
                        System.out.println("xLabel zoom = " + xLabelStart + " 
posx = " + posX);

                }

}
[Message sent by forum member 'ricorx7' (ricorx7)]

http://forums.java.net/jive/thread.jspa?messageID=208203

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to