Results 1 to 16 of 16
  1. #1
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Zooming in graph using awt/swing

    how to implement zooming with using awt classes and functions....i need the suggestion..........I have planed to use AffineTransform ...but it just zoom my image...i have graph...........i want that... when i zoom ...only the contents inside gets zoomed and accordingly x-y axis should get granualar.....Here i am bothered about granuality of x-y axis when zoom in happens so that graph ploted gives me more accurate values on x-y axis....
    here is the sample of code in which i want to implement the zooming feature.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.geom.AffineTransform;
    import java.util.*;
    import java.text.*;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class AffineTransformTest {
    
    	private static TransformingCanvas canvas;
    	public static int SIZE_X			= 480;
    	public static int	SIZE_Y			= 250;
    	public static int	SIZE_Y1			= 240;
    	public static int xOffset = 50;
    	public	static int yOffset = 40;
    
    	public	static int Y1_MAX_VALUE = 100;
    
    	public static int Y2_MAX_VALUE = 3;
    
    	public	static int MAX_NUM_INTERVELL_PER_DAY = 96;
    	public static int 	duration=1;
    	public static int sizeY = SIZE_Y;
    
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		canvas = new TransformingCanvas();
    		TranslateHandler translater = new TranslateHandler();
    		canvas.addMouseListener(translater);
    		canvas.addMouseMotionListener(translater);
    		canvas.addMouseWheelListener(new ScaleHandler());
    		frame.setLayout(new BorderLayout());
    		frame.getContentPane().add(canvas, BorderLayout.CENTER);
    		frame.setSize(500, 500);
    		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		frame.setVisible(true);
    	}
    
    	private static class TransformingCanvas extends JComponent {
    		private double translateX;
    		private double translateY;
    		private double scale;
    
    		TransformingCanvas() {
    			translateX = 0;
    			translateY = 0;
    			scale = 1;
    			setOpaque(true);
    			setDoubleBuffered(true);
    		}
    
    		@Override public void paint(Graphics g) {
    
    				Color 	plotColor1,plotColor2,plotColor3,plotColor4,plotColor5,plotColor6,plotColor7,plotColor8,plotColor9,plotColor10;
    
    			plotColor1 		= Color.orange;
    				plotColor2 		= Color.black;
    				plotColor3 		= Color.blue;
    				plotColor4 		= Color.magenta;
    				plotColor5 		= Color.cyan;
    				plotColor6 		= Color.pink;
    				plotColor7 		= Color.gray;
    				plotColor8		= Color.red;
    				plotColor9		= Color.green;
    	plotColor10		= Color.lightGray;
    
    			AffineTransform tx = new AffineTransform();
    			tx.translate(translateX, translateY);
    			tx.scale(scale, scale);
    			Graphics2D ourGraphics = (Graphics2D) g;
    			ourGraphics.setColor(Color.WHITE);
    			ourGraphics.fillRect(0, 0, getWidth(), getHeight());
    			ourGraphics.setTransform(tx);
    			ourGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    					RenderingHints.VALUE_ANTIALIAS_ON);
    			ourGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    			ourGraphics.setColor(Color.BLACK);
    			ourGraphics.drawRect(50, 50, 50, 50);
    			ourGraphics.fillOval(100, 100, 100, 100);
    			ourGraphics.drawString("Test Affine Transform", 50, 30);
    
    			g.setColor(Color.black);
    				g.drawString("ES" , xOffset, 10);
    				g.drawString("SES", xOffset+30,10);
    				g.drawString("BBE", xOffset+60,10);
    				g.drawString("UAS",xOffset+90,10);
    				g.drawString("ESR", xOffset+120,10);
    				g.drawString("SESR", xOffset+160,10);
    				g.drawString("BBER" , xOffset+200,10);
    				g.drawString("SUSPECT",xOffset+240,10);
    				g.drawString("TIME-ELAPSED",xOffset+310,10);
    
    				g.setColor(plotColor1);
    				g.fillRect(xOffset+5, 15, 12, 8);
    
    				g.setColor(plotColor2);
    				g.fillRect(xOffset+35, 15, 12, 8);
    
    				g.setColor(plotColor3);
    				g.fillRect(xOffset+65, 15, 12, 8);
    
    				g.setColor(plotColor4);
    				g.fillRect(xOffset+95, 15, 12, 8);
    
    				g.setColor(plotColor5);
    				g.fillRect(xOffset+125, 15, 12, 8);
    
    				g.setColor(plotColor6);
    				g.fillRect(xOffset+170, 15, 12, 8);
    
    				g.setColor(plotColor7);
    				g.fillRect(xOffset+210, 15, 12, 8);
    
    				g.setColor(plotColor8);
    				g.fillRect(xOffset+260, 15, 12, 8);
    
    				g.setColor(plotColor9);
    				g.fillRect(xOffset+340, 15, 12, 8);
    				/*<Bugzilla><1903><07-Dec-2009><RR><END>*/
    	g.setColor(Color.black);
    
    			/*Dram Graph boarder */
    				g.drawRect(xOffset,yOffset,SIZE_X,sizeY);
    				g.drawRect(xOffset-1,yOffset-1,SIZE_X+2,sizeY+2);
    
    			   /*Plot X axis*/
    				int max_x_marks = 8;
    				int temp = 1,cnt_graph = 0;
    				int float_temp,float_temp2;
    
    				/*max 8 plots on x axis*/
    				for(int x=max_x_marks; x>0; x--)
    				{
    
    					float_temp = (int)((MAX_NUM_INTERVELL_PER_DAY*duration)/max_x_marks)*((max_x_marks+1)-x);
    					float_temp2 = SIZE_X-(60*cnt_graph);
    
    					g.drawString(String.valueOf(float_temp),(float_temp2-20) ,SIZE_Y+yOffset+35);
    					cnt_graph++;
    				}
    
    				/*Plot Y1 AXIS*/
    				temp = Y1_MAX_VALUE;
    				for(int x = 0; x <= SIZE_Y; x+= 25)
    				{
    					g.drawString(String.valueOf(temp), 25, x + yOffset+10);
    					temp -= (Y1_MAX_VALUE - 0)/10;
    				}
    				temp = 1000;
    				 /*Plot Y2 AXIS*/
    				int index_log = 1;
    				for(int x = 0; x <= SIZE_Y1; x+= 80)
    				{
    					if(x== 240)
    					index_log--;
    					if(temp>=1)
    					{
    						g.drawString(String.valueOf(temp), 550, x+yOffset+8-index_log);
    						g.drawLine(530,x+yOffset+5-index_log, 540, x+yOffset+5-index_log);
    					}
    					temp = temp/10;
    				}
    
    				Font thisFont = new Font("Times New Roman", Font.BOLD, 14);
    				g.setFont(thisFont);
    				g.drawString("Y2", 550, 160);
    	g.drawString("Y1",5, 160);
    			// super.paint(g);
    		}
    	}
    
    	private static class TranslateHandler implements MouseListener,
    			MouseMotionListener {
    		private int lastOffsetX;
    		private int lastOffsetY;
    
    		public void mousePressed(MouseEvent e) {
    			// capture starting point
    			lastOffsetX = e.getX();
    			lastOffsetY = e.getY();
    		}
    
    		public void mouseDragged(MouseEvent e) {
    
    		// new x and y are defined by current mouse location subtracted
    			// by previously processed mouse location
    			int newX = e.getX() - lastOffsetX;
    			int newY = e.getY() - lastOffsetY;
    
    			// increment last offset to last processed by drag event.
    			lastOffsetX += newX;
    			lastOffsetY += newY;
    
    			// update the canvas locations
    			canvas.translateX += newX;
    			canvas.translateY += newY;
    
    			// schedule a repaint.
    			canvas.repaint();
    		}
    
    		public void mouseClicked(MouseEvent e) {}
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
    		public void mouseMoved(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    	}
    
    	private static class ScaleHandler implements MouseWheelListener {
    		public void mouseWheelMoved(MouseWheelEvent e) {
    			if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
    
    				// make it a reasonable amount of zoom
    				// .1 gives a nice slow transition
    				canvas.scale += (.1 * e.getWheelRotation());
    				// don't cross negative threshold.
    				// also, setting scale to 0 has bad effects
    				canvas.scale = Math.max(0.00001, canvas.scale);
    				canvas.repaint();
    			}
    		}
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    How about using a separate BufferedImage for drawing the graph and then draw it where you want it to show in the component.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    sorry i didnt got your point

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    It looks like you need to be able to make two drawings in the same space. One would be the scales and box around the outside that would not change. The other on the inside that changes. If the inside part were an image it would be isolated from the box and its contents could be drawn using the scaling etc and the finished image could be drawn inside the box.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Zooming in graph using awt/swing

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    Quote Originally Posted by Norm View Post
    It looks like you need to be able to make two drawings in the same space. One would be the scales and box around the outside that would not change. The other on the inside that changes. If the inside part were an image it would be isolated from the box and its contents could be drawn using the scaling etc and the finished image could be drawn inside the box.


    Bingo.................absolutely correct..... urrently i am facing problem to how to separate that inner box and outer n need to scale the x-y axis according to zoom....

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    need to scale the x-y axis according to zoom....
    No ideas besides trial-and-error. Have an inner image that is easy to see x-y values with and do some experiments.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    Quote Originally Posted by Norm View Post
    No ideas besides trial-and-error. Have an inner image that is easy to see x-y values with and do some experiments.
    Wat is the trial you gonna suggest............

  9. #9
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    @Norm As u have got my idea...then... Is my appraoch correct ? Or shall i start from scratch with somethgelse...........

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    I haven't written an app like this one and do not know what will work. Hence I suggested trial and error.
    Make a image with many recognizable x-y items and draw x & y scales for it. Then zoom the image and change where the x &y scale marks are drawn to match the scaling. Test it, change it, test it, change it ....
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    If you were asked to create a app which has staic image and it should have zoom functionality with x-y axis granuality....wat u would have done?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    I'd do what I suggested in post #10 and post#7
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    @Norm is there way i can use AffineTransform to zoom in below piece of code....

    Java Code:
      import java.awt.*;
        import java.awt.image.*;
        import java.awt.event.*;
        import java.applet.*;
        import java.net.*;
        import java.io.*;
        import java.util.*;
        import java.text.*;
    
        /******************************************************************************
        *
        * Class - Graph
        *
        * 		This is the entry point.
        *	This class extends Applet class and implements Runnable.
        *
        *****************************************************************************/
    
        public class GraphPerf extends Applet implements Runnable,ItemListener,MouseMotionListener
    
        {
        //Global variables
    	int MAX_DATA_X  ;
    	int    SIZE_X= 480;
    	int    SIZE_Y= 250;
    	int    SIZE_Y1= 240;
    	int MIN_ERROR = -1;
    	int MAX_LOG10_ERROR_COUNT = 1001;
    	int MAX_ERROR_COUNT = 101;
    	int SIZE_Y_EXTENDED_BOTTOM = 20;
    	int MAX_DISP_PARMS = 16;
    	int MAX_NUM_INTERVELL_PER_DAY = 96;
    	int MAX_DISP_PARMS_STD = 7;
    	int	refreshTime;
    	String 	serverAddress,hostAddr;
    	Color 	plotColor1,plotColor8;
    
    	float graphData[][];
    	float graph_data_rf_east,graph_data_rf_west;
    
    	int xOffset = 50;
    	int yOffset = 40;
    
    	int Y1_MAX_VALUE = 100;
    	int Y2_MAX_VALUE = 3;
    
    	float RANGE2;
    
    	Thread 	graphThread;
    	Image 	Buffer;
    	Graphics plot;
    
    	Choice 	timeChoice, modeChoice, seloptChoice;
    	int 	duration=1, viewMode=1, line_type = 0,viewOpt = 1;
    
    	Label valueLabel1, valueLabel2,valueLabel3,valueLabel4,valueLabel5;
    	boolean GraphBottomExtendFlag=true;
    
    
        /******************************************************************************
        *
        * method - init
        *
        * 		This is the method called first after applet loaded in to browser.
        *	This function reads configurable parameters from HTML tag and updates
        *	global variables.
        *****************************************************************************/
    
        public void init()
        {
    
    	MAX_DATA_X = 672;//Integer.parseInt(getParameter("max_data_x"));
    	//refreshTime		= 30;//Integer.parseInt(getParameter("refresh_sec"));
    	/*URL url = getDocumentBase();
    	String host = url.getHost();
    	try
    	{
    		InetAddress addr =  InetAddress.getByName(host);
    		hostAddr=addr.getHostAddress();
    	}catch (UnknownHostException ex) {
    		ex.printStackTrace();
    	}
         */
    	//serverAddress 	= new String ( getParameter("access_str")+ hostAddr + getParameter("data_server"));
    	graphData 		= new float[MAX_DISP_PARMS][MAX_DATA_X+1];
    	/*initialize the array with -1 not with 0(0 also valid for graph data)  */
    	int i =0,j = 0;
    	for( j=0; j<MAX_DISP_PARMS; j++)
    	{
    		for( i=0; i<MAX_DATA_X+1; i++)
    		{
    			graphData[j][i] = -1;
    		}
    	}
    	graph_data_rf_east = -1;
    	graph_data_rf_west = -1;
    
    	plotColor1 		= Color.orange;
    	plotColor8		= Color.red;
    
    	setBackground(Color.white);
    	setLayout(null);
    
    	timeChoice   = new Choice();
    	timeChoice.add("1");
    	timeChoice.add("2");
    	timeChoice.add("3");
    	timeChoice.add("4");
    	timeChoice.add("5");
    	timeChoice.add("6");
    	timeChoice.add("7");
    	add(timeChoice);
    
    	timeChoice.setBounds(190,340,40,23);
    	timeChoice.addItemListener(this);
    
    	Label timeLabel1 = new Label("View graph for last");
    	Label timeLabel2 = new Label("day(s)");
    	add(timeLabel1);
    	timeLabel1.setBounds(xOffset+30,340,160,23);
    
    	add(timeLabel2);
    	timeLabel2.setBounds(240,340,50,23);
    
    	valueLabel1 = new Label();
    	add(valueLabel1);
    	valueLabel1.setBounds(300,340,50,23);
    	valueLabel2 = new Label();
    	add(valueLabel2);
    	valueLabel2.setBounds(370,340,70,23);
    	valueLabel3 = new Label();
    	add(valueLabel3);
    	valueLabel3.setBounds(440,340,70,23);
    	valueLabel4 = new Label();
    	add(valueLabel4);
    	valueLabel4.setBounds(500,340,70,23);
    	valueLabel5 = new Label();
    	add(valueLabel5);
    	valueLabel5.setBounds(370,370,80,25);
    	modeChoice = new Choice();
    	modeChoice.add("East");
    	modeChoice.add("West");
    	/* Display this only for Protected and East-West Mode */
    	if(2/*Integer.parseInt(getParameter("mode"))*/ == 2)
    	{
    		add(modeChoice);
    	}
    	else
    	{
    		viewOpt = 1;
    	}
    	modeChoice.setBounds(xOffset+SIZE_X-55, 0, 60, 25);
    	modeChoice.addItemListener(this);
    	addMouseMotionListener(this);
        }
    
        public void start()
        {
    	graphThread = new Thread(this);
    	graphThread.start();
        }
    
        public void stop()
        {
    	graphThread = null;
        }
    
        /******************************************************************************
        *
        * 	This method will be called after starting the thread. This is a
        *	infinite loop which will call query method for every 30 sec to read data
        *	from CGI. Then it plots graph by calling plotGraph method
        *	the thread.
        *****************************************************************************/
    
        public void run()
        {
    	/*while (false)
    	{
    		try
    		{//getData(serverAddress);
    			int sizeY = SIZE_Y;
    			if(GraphBottomExtendFlag)
    			{
    				sizeY += SIZE_Y_EXTENDED_BOTTOM;
    			}
    			repaint(xOffset+1,yOffset+1,SIZE_X-1,sizeY-1);
    			//graphThread.sleep(refreshTime*1000);
    		}catch (Exception e) { System.out.println(e); }
    	}*/
        }
    
        /******************************************************************************
        *
        * method - paint
        *
        * 		This method displays the graph plotted by plotGraph method
        *	in the screen. Then it draws axis for the graph
        *
        *****************************************************************************/
    
        public void paint(Graphics g1)
        {
    	int sizeY = SIZE_Y;
    	/*If  Graph Bottom is to be Etended
    	 *soo that zero is displayed properly
    	 */
    	if(GraphBottomExtendFlag)
    	{
    		sizeY += SIZE_Y_EXTENDED_BOTTOM;
    	}
    
    	if( duration <= 5 )
    	{
    		Buffer = createImage(SIZE_X, sizeY);
    		plot = Buffer.getGraphics();
    		plotGraph(plot);
    		g1.drawImage (Buffer,xOffset,yOffset,this);
    	}
    	else
    	{
    		Buffer = createImage(MAX_DATA_X*duration/7,sizeY);
    		plot = Buffer.getGraphics();
    		plotGraph(plot);
    		g1.drawImage (Buffer,xOffset,yOffset,SIZE_X,sizeY,this);
    	}
    
    	g1.setColor(Color.black);
    
    	g1.drawRect(70,150,270,80);
    	/*Dram Graph boarder */
    	g1.drawRect(xOffset,yOffset,SIZE_X,sizeY);
    	g1.drawRect(xOffset-1,yOffset-1,SIZE_X+2,sizeY+2);
    
              /*Plot X axis*/
    	int max_x_marks = 8;
    	int temp = 1,cnt_graph = 0;
    	int float_temp,float_temp2;
    
    	/*max 8 plots on x axis*/
    	for(int x=max_x_marks; x>0; x--)
    	{
    
    		float_temp = (int)((MAX_NUM_INTERVELL_PER_DAY*duration)/max_x_marks)*((max_x_marks+1)-x);
    		float_temp2 = SIZE_X-(60*cnt_graph);
    
    		g1.drawString(String.valueOf(float_temp),(float_temp2-20) ,SIZE_Y+yOffset+35);
    		cnt_graph++;
    	}
    
    	/*Plot Y1 AXIS*/
    	temp = Y1_MAX_VALUE;
    	for(int x = 0; x <= SIZE_Y; x+= 25)
    	{
    		g1.drawString(String.valueOf(temp), 25, x + yOffset+10);
    		temp -= (Y1_MAX_VALUE - 0)/10;
    	}
    	temp = 1000;
    	 /*Plot Y2 AXIS*/
    	int index_log = 1;
    	for(int x = 0; x <= SIZE_Y1; x+= 80)
    	{
    		if(x== 240)
    		index_log--;
    		if(temp>=1)
    		{
    			g1.drawString(String.valueOf(temp), 550, x+yOffset+8-index_log);
    			g1.drawLine(530,x+yOffset+5-index_log, 540, x+yOffset+5-index_log);
    		}
    		temp = temp/10;
    	}
    	Font thisFont = new Font("Times New Roman", Font.BOLD, 14);
    	g1.setFont(thisFont);
    	g1.drawString("Y2", 550, 160);
    	g1.drawString("Y1",5, 160);
    
        }
        /******************************************************************************
         *
         * method - plotGraph
         *
         * 		Depending on the mode, "East", "West" or "Combined", it plots
         *	single or two graphs in the same applet.
         *
         *	Inputs :
         *			g - Graphics object
         *****************************************************************************/
    
        public void plotGraph(Graphics g)
        {
    	g.setColor(new Color(255,255,220));
    
    	/*If  Error-Sec-Count Graph
    	 *Then extend the lower portion
    	 *soo that zero is displayed properly
    	 */
    	if(GraphBottomExtendFlag)
    	{
    		g.fillRect(0,0,MAX_DATA_X,SIZE_Y + SIZE_Y_EXTENDED_BOTTOM);
    	}
    	else
    	{
    		g.fillRect(0,0,MAX_DATA_X,SIZE_Y);
    	}
    
    
    	switch(viewMode)
    	{
    		case 1 :
    			plot1(g);
    			plot_timeelapsed_east(g);
    			break;
    
    		case 2 :
    			plot8(g);
    			plot_timeelapsed_west(g);
    			break;
    	}
        }
        /******************************************************************************
         *
         * method - plot1
         *
         * 		This method uses graphData[0][] global variable and plots series of lines
         *	in the applet
         *
         *	Inputs :
         *			g - Graphics object
         *****************************************************************************/
    
         void plot1(Graphics g)
         {
    	int end = MAX_DATA_X;
    	int localPlotBuffer[];
    
    	localPlotBuffer = new int[2];
    
    	g.setColor(plotColor1);
    	if(duration > 5)
    	{
    		for(int x=(duration*MAX_NUM_INTERVELL_PER_DAY); x > 0 ; x--)
    		{
    			/*if data is valid data then plot else ignore*/
    			if((graphData[0][end]>MIN_ERROR)&&(graphData[0][end-1]<MAX_LOG10_ERROR_COUNT)&&(graphData[0][end-1]>MIN_ERROR)&&(graphData[0][end]<MAX_LOG10_ERROR_COUNT))
    			{
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[0][end] == 0)
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[0][end]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[0][end])/(float)Y2_MAX_VALUE));
    				}
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[0][end-1] == 0)
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[0][end-1]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[0][end-1])/(float)Y2_MAX_VALUE));
    				}
    				g.drawLine(x-1,(localPlotBuffer[0]+5), x-2,(localPlotBuffer[1]+5));
    				g.drawLine(x,(localPlotBuffer[0]+5), x-1,(localPlotBuffer[1]+5));
    			}
    			end--;
    		}
    	}
    	else
    	{
    		float temp = SIZE_X;
    		for(int x=(duration*MAX_NUM_INTERVELL_PER_DAY) ; x > 0 ; x--)
    		{
    			float LocalTemp1 = temp;
    			float LocalTemp2 = (int)(temp-(double)5/(double)duration);
    			/*Normalise the pixcel positions */
    			/*Normalise the pixcel positions */
    			if(duration == 1)
    			{
    				if(LocalTemp1>(SIZE_X-3))
    				LocalTemp1 = SIZE_X;
    				if(LocalTemp2>(SIZE_X-3))
    				LocalTemp2 = SIZE_X;
    			}
    			/*Normalise the pixcel positions */
    			else if(duration == 2)
    			{
    				if(LocalTemp1>(SIZE_X-2))
    				LocalTemp1 = SIZE_X;
    				if(LocalTemp2>(SIZE_X-2))
    				LocalTemp2 = SIZE_X;
    			}
    			/*Normalise the pixcel positions */
    			else if(duration == 3)
    			{
    				if(LocalTemp1>(SIZE_X-1))
    				LocalTemp1 = SIZE_X;
    				if(LocalTemp2>(SIZE_X-1))
    				LocalTemp2 = SIZE_X;
    			}
    			/*if data is valid data then plot else ignore*/
    			if((graphData[0][end]>MIN_ERROR)&&(graphData[0][end-1]<MAX_LOG10_ERROR_COUNT)&&(graphData[0][end-1]>MIN_ERROR)&&(graphData[0][end]<MAX_LOG10_ERROR_COUNT))
    			{
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[0][end] == 0)
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[0][end]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[0][end])/(float)Y2_MAX_VALUE));
    				}
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[0][end-1] == 0)
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[0][end-1]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[0][end-1])/(float)Y2_MAX_VALUE));
    				}
    				g.drawLine((int)LocalTemp1,(localPlotBuffer[0]+5), (int)LocalTemp2,(localPlotBuffer[1]+5));
    			}
    			temp-=(double)5/(double)duration;
    			end--;
    		}
    	}
        }
    
        /******************************************************************************
        *
        * method - plot8
        *
        * 		This method uses graphData[7][] global variable and plots series of lines
        *	in the applet
        *
        *	Inputs :
        *			g - Graphics object
        *****************************************************************************/
    
        void plot8(Graphics g)
        {
    	int end = MAX_DATA_X;
    	int localPlotBuffer[];
    	localPlotBuffer = new int[2];
    
    	g.setColor(plotColor1);
    
    	if(duration > 5)
    	{
    		for(int x=(duration*MAX_NUM_INTERVELL_PER_DAY); x > 0 ;x-- )
    		{
    			/*if data is valid data then plot else ignore*/
    			if((graphData[8][end]>MIN_ERROR)&&(graphData[8][end-1]<MAX_LOG10_ERROR_COUNT)&&(graphData[8][end-1]>MIN_ERROR)&&(graphData[8][end]<MAX_LOG10_ERROR_COUNT))
    			{
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[8][end] == 0)
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[8][end]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[8][end])/(float)Y2_MAX_VALUE));
    				}
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[8][end-1]== 0)
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[8][end-1]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[8][end-1])/(float)Y2_MAX_VALUE));
    				}
    				g.drawLine(x-1,(localPlotBuffer[0]+5), x-2,(localPlotBuffer[1]+5));
    				g.drawLine(x,(localPlotBuffer[0]+5), x-1,(localPlotBuffer[1]+5));
    			}
    			end--;
    		}
    	}
    	else
    	{
    		float temp = SIZE_X;
    		for(int x=(duration*MAX_NUM_INTERVELL_PER_DAY) ; x > 0 ; x--)
    		{
    			float LocalTemp1 = temp;
    			float LocalTemp2 = (int)(temp-(double)5/(double)duration);
    			/*Normalise the pixcel positions */
    			if(duration == 1)
    			{
    				if(LocalTemp1>(SIZE_X-3))
    				LocalTemp1 = SIZE_X;
    				if(LocalTemp2>(SIZE_X-3))
    				LocalTemp2 = SIZE_X;
    			}
    			/*Normalise the pixcel positions */
    			else if(duration == 2)
    			{
    				if(LocalTemp1>(SIZE_X-2))
    				LocalTemp1 = SIZE_X;
    				if(LocalTemp2>(SIZE_X-2))
    				LocalTemp2 = SIZE_X;
    			}
    			/*Normalise the pixcel positions */
    			else if(duration == 3)
    			{
    				if(LocalTemp1>(SIZE_X-1))
    				LocalTemp1 = SIZE_X;
    				if(LocalTemp2>(SIZE_X-1))
    				LocalTemp2 = SIZE_X;
    			}
    			/*if data is valid data then plot else ignore*/
    			if((graphData[8][end]>MIN_ERROR)&&(graphData[8][end-1]<MAX_LOG10_ERROR_COUNT)&&(graphData[8][end-1]>MIN_ERROR)&&(graphData[8][end]<MAX_LOG10_ERROR_COUNT))
    			{
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[8][end] == 0)
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[8][end]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[0] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[8][end])/(float)Y2_MAX_VALUE));
    				}
    				/*if data present is 0, log10(0) is not define so then plot this graph in normal scale */
    				if(graphData[8][end-1]== 0)
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y )*(float)(1 - (float)graphData[8][end-1]/(float)Y1_MAX_VALUE)) ;
    				}
    				else
    				{
    					localPlotBuffer[1] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graphData[8][end-1])/(float)Y2_MAX_VALUE));
    				}
    				g.drawLine((int)LocalTemp1,(localPlotBuffer[0]+5), (int)LocalTemp2,(localPlotBuffer[1]+5));
    			}
    			temp-=(double)5/(double)duration;
    			end--;
    		}
    	}
        }
    
        /******************************************************************************
         *
         * method - plot_timeelapsed_east
         *
         * 		This method uses graph_data_rf_east global variable and plots series of lines
         *	in the applet
        *****************************************************************************/
    
        void plot_timeelapsed_east(Graphics g)
        {
    	int end = MAX_DATA_X;
    	int localPlotBuffer[];
    	int x= 0;
    
    	localPlotBuffer = new int[2];
    	x= (duration*MAX_NUM_INTERVELL_PER_DAY);
    	g.setColor(plotColor1);
    	/*if data is valid data then plot else ignore*/
    	if((graph_data_rf_east>0)&&(graph_data_rf_east<MAX_LOG10_ERROR_COUNT))
    	{
    		localPlotBuffer[0] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graph_data_rf_east)/(float)Y2_MAX_VALUE));
    		if(duration>5)
    		g.drawLine(x-1,SIZE_Y+5, x-2,(localPlotBuffer[0]+5));
    		else
    		g.drawLine(SIZE_X,SIZE_Y+5, 474+duration,(localPlotBuffer[0]+5));
    	}
          }/*End for plot_timeelapsed_east() */
    
         /******************************************************************************
         *
         * method - plot_timeelapsed_west
         *
         * 		This method uses graph_data_rf_east global variable and plots series of lines
         *	in the applet
         *****************************************************************************/
    
        void plot_timeelapsed_west(Graphics g)
        {
    	int end = MAX_DATA_X;
    	int localPlotBuffer[];
    	int x= 0;
    
    	localPlotBuffer = new int[2];
    	x= (duration*MAX_NUM_INTERVELL_PER_DAY);
    
    	g.setColor(plotColor1);
    	/*if data is valid data then plot else ignore*/
    	if((graph_data_rf_east>MIN_ERROR)&&(graph_data_rf_east<MAX_LOG10_ERROR_COUNT))
    	{
    		localPlotBuffer[0] = (int)((float)(SIZE_Y1 )*(float)(1 - (float)Math.log10(graph_data_rf_west)/(float)Y2_MAX_VALUE));
    		if(duration>5)
    		g.drawLine(x-1,SIZE_Y+5, x-2,(localPlotBuffer[0]+5));
    		else
    		g.drawLine(SIZE_X,SIZE_Y+5, 474+duration,(localPlotBuffer[0]+5));
    	}
        }
    
        /******************************************************************************
        *
        * method - getData
        *
        * 		This method sends query to CGI to collect data. Then it converts the
        *	data for applet area then updates global variable.
        *
        *	Inputs :
        *			serverAddress - server CGI path
        *****************************************************************************/
    
        public void getData(String serverAddress)
        {
    
    	URL addr;
    	BufferedReader in;
    	String inputLine;
    	int count = 0;
    	int i=0,j = 0;
    	try
    	{
    		addr = new URL(serverAddress);
    		URLConnection connection = addr.openConnection();
    		in = new BufferedReader(new InputStreamReader(addr.openStream()));
    		/*Read data for first link */
    		for( j=0; j<MAX_DISP_PARMS_STD; j++)
    		{
    			for( i=0; i<MAX_DATA_X+1; i++)
    			{
    				inputLine = in.readLine();
    				graphData[j][i] = Integer.parseInt(inputLine);
    			}
    		}
    		for( i=0; i<MAX_DATA_X; i++)
    		{
    			inputLine = in.readLine();
    			graphData[7][i] = Integer.parseInt(inputLine);
    			if(graphData[7][i] == 1)
    			graphData[7][i] = 10;
    		}
    		inputLine = in.readLine();
    		graph_data_rf_east = Integer.parseInt(inputLine);
    		/*Reading data for second link */
    		if(Integer.parseInt(getParameter("mode")) == 2)
    		{
    			for( j=8; j<15; j++)
    			{
    				for( i=0; i<MAX_DATA_X+1; i++)
    				{
    					inputLine = in.readLine();
    					graphData[j][i] = Integer.parseInt(inputLine);
    				}
    
    			}
    			for( i=0; i<MAX_DATA_X; i++)
    			{
    				inputLine = in.readLine();
    				graphData[15][i] = Integer.parseInt(inputLine);
    				if(graphData[15][i] == 1)
    				graphData[15][i] = 10;
    			}
    			inputLine = in.readLine();
    			graph_data_rf_west = Integer.parseInt(inputLine);
    		}
    		in.close();
    	}catch (Exception e) { System.out.println("Server Data Read Error:"+e); }
        }
    
        /******************************************************************************
         *
         * method - itemStateChanged
         *
         * 		This method will be called whenever event occured on this choice.
         *	it read the current status and changes scale accordingly.
         * *****************************************************************************/
    
        public void itemStateChanged(ItemEvent evt)
        {
    	if( evt.getSource() == timeChoice )
    		duration = Integer.parseInt(timeChoice.getSelectedItem());
    	else
    		viewMode = modeChoice.getSelectedIndex()+1;
    	repaint();
        }
        /******************************************************************************
         *
         * method - mouseMoved
         *
         * 		This method will be called whenever mouse cursor is moved over the
         *	applet. Depending on the cursor position, it will display Actual
         *	X and Y values of the graph.
         *****************************************************************************/
    
        public void mouseMoved(MouseEvent evt)
    	{
    		int x = evt.getX()-xOffset;
    		int y = evt.getY()-yOffset-5;
    
    		int a = evt.getX();
    		int b = evt.getY();
    
    		int duration = Integer.parseInt(timeChoice.getSelectedItem());
    		if( (x>=0) && (x<=SIZE_X) && (y>=0) && (y<=SIZE_Y) )
    		{
    			valueLabel1.setText("X ");
    			valueLabel2.setText("Y1 ");
    			valueLabel3.setText("Y2 ");
    			try
    			{
    				int x_max_value = ((SIZE_X*duration)/5);
    				int x1  = (int)((float)((float)((float)(SIZE_X*duration))/5)  * ((float)((float)(SIZE_X - x))/((float)SIZE_X)));
    				/*For Durations less than 16 scale starts with 1*/
    				int y1  = (int)((float)Y1_MAX_VALUE  * (((float)SIZE_Y - (float)y)/((float)SIZE_Y)));
    				int y2 = (int) Math.pow(10,((float)(3 * ((float)(1 - (float)y/((float)SIZE_Y1))))));
    				valueLabel1.setText("X="+x1);
    				valueLabel2.setText("X pix="+a);
    				valueLabel3.setText("Y="+y1);
    				valueLabel4.setText("Y pix="+b);
    				valueLabel5.setText("Y2="+y2);
    			}
    			catch(Exception e) {System.out.println("Mouse Moved Error" + e);}
    		}
    		else
    		{
    			valueLabel1.setText(" ");
    			valueLabel2.setText(" ");
    			valueLabel3.setText(" ");
    
    		}
    	}
    	public void mouseDragged(MouseEvent evt) { }
        }
    Last edited by chirayu; 08-09-2012 at 08:39 AM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    I don't know.
    What have you tried? What happened?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: Zooming in graph using awt/swing

    How can i implement zooming in above piece of code...Please analyze the code dont corelate with comment#1.....
    COMMENT #13 CODE First it reads all parameters from HTML tags and stores in global variables. Then it sends query to CGI to read graph data. Then it converts data for plotting and draws graph. There is a option for user to select 1-24 hours. Based on the selection graph will be plotted by plotting only selected data. Every 30 sec it sends query to CGI for collecting data.....

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Zooming in graph using awt/swing

    Can you explain what the problem is with the code? 700+ lines is a bit too much code to search through without some specific problem.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Swing Bar Graph will not display in JPanel
    By Redefine12 in forum AWT / Swing
    Replies: 5
    Last Post: 05-01-2012, 01:07 AM
  2. real time graph plotting in swing
    By elsanthosh in forum AWT / Swing
    Replies: 1
    Last Post: 11-22-2010, 04:06 PM
  3. Java graph swing
    By mohitgupta1989 in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2010, 08:27 AM
  4. Replies: 11
    Last Post: 05-08-2010, 03:52 AM
  5. Zooming in on fractal
    By Mr.Beans in forum Java 2D
    Replies: 1
    Last Post: 04-18-2009, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •