Results 1 to 5 of 5

Thread: PAINTCompnent

  1. #1
    sims is offline Member
    Join Date
    Jul 2008
    Posts
    9
    Rep Power
    0

    Default PAINTCompnent

    Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;   
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import java.awt.geom.*;
    
    class proj  extends JFrame  implements  ChangeListener ,Runnable,ActionListener   {	
    
    	
    	public 	int bottom;
    	public	 Thread t;
    		int padding = 4;
    		 JFrame f;
    		 JTabbedPane jt ,jt1;
    		JPanel jp,jp1,jp2,jp3,jp4;
    		 JButton j,j1,j2;
    		JLabel lab,lab1;
    		JComboBox jcb,jcb1;
    		JColorChooser jc,jc1;
    		 Color col1,c2;
    	public	 Graphics g;
    	public	 Graphics g1;
    	public	 Graphics2D g2d;
    	             JDialog  dialog;
    	String	 k;
    	Line2D	 shape1;
    	f p1 = new f();
    	
    
    		public void run()
    		{
    	
    			 f = new JFrame("Graph");
    			pan jp = new pan();
    			pan jp1 = new pan(); ;
    		
    
    			jt = new JTabbedPane(JTabbedPane.TOP);
    			jt.setTabPlacement(JTabbedPane.TOP);
    		 
    		
    	       	jp.setPreferredSize(new Dimension (50,50));
    			jp.setLayout(new BorderLayout());
    			jp.setBackground(Color.gray);
    			jp.setPreferredSize(new Dimension(60,50));	
    		
    			jp4 = new JPanel();
    			j = new JButton("Draw the graph");
    			j.addActionListener(this);
    			jp4.setBorder(BorderFactory.createRaisedBevelBorder());
    			jp4.setPreferredSize(new Dimension(130,20));
    			jp4.add(j);
    		
    			jp.add(jp4,BorderLayout.EAST);	
    			String[] a = {"select  "};
    			lab = new JLabel("x parameters");
    			lab.setLocation(700,700);
    			jp4.add(lab);
    			jcb= new JComboBox(a);
    			jp4.add(jcb);
    			lab1 = new JLabel("Y parameters");
    			jp4.add(lab1);
    			jcb1= new JComboBox(a);
    			jp4.add(jcb1);
    			jt.addTab("graph1", jp);
    			
    		
    		
    			jp1.setBackground(Color.black);	
    			jt.addTab("Graph2",jp1);
    			JPanel jp3=new JPanel();
    
    		
    			jc = new JColorChooser(Color.yellow);
    			jc.createDialog(jp3,"Choose Background Color",true, jc, null,null);
    			jc.getSelectionModel().addChangeListener(this);
    			jp3.add(jc);
    			jt.addTab("Change Grid Color", jp3);
    			f.add(jt);
    			f.pack();
    			f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    			f.setSize(400,400);
    			f.setVisible(true);
    	           	 }
    		
    		public void stateChanged(ChangeEvent e)
    		{
    			
    		}
    
    		
    		public void actionPerformed(ActionEvent e)
    		{	
    		 	
    			  if((JButton)e.getSource() == j)
    		 	  {
    			/*jc1 = new JColorChooser();
    			 JColorChooser.showDialog(jp4,"Select The Color", Color.red);
    			c2=jc1.getColor();*/
    			
    			p1.repaint();
    			
    			
    		   	 }
    		}		
    
      //MAIN
    		
    		public static void main(String[]args)
    	
    		{
    			proj lp = new proj();
    			Thread t = new Thread(lp);
    			t.start();
    		}	
    
    		public class pan extends JPanel
    		{	
    			Graphics g;	
    			 public void paintComponent(Graphics g)
    			  {	
    			   	super.paintComponent(g);
    				Color col1=jc.getColor();
    				g.setColor(col1);
    				g.drawRect( 30,30,800,600);//600 = width 400 = height
    				for(int i=30;i<=620; i=i+20)
    				{	
    					g.drawLine(30,i,830,i);       				}
    				g.setColor(Color.white);
    			
    				for( Integer i=10;i<=620; i=i+20)
    				{
    					k=Integer.toString(i);
    					g.drawString(k,5,i+27);
    			
    				}
    				for(Integer i=10;i<=820; i=i+20)
    				{
    					int l = i/10;
    					g.drawString(Integer.toString(l),i+17,650);
    				}
    				g.setColor(col1);
    				for(int i =0;i<=800;i=i+20)
    				g.drawLine(i+30,30,i+30,630);            			     }
    			
    		}	
    }
    poblem is when a user clicks on the button ....a line should be drawn.......
    which is creating problem.

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

    Default

    which is creating problem
    What is the problem? What is happening/not happening that you want to change?

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*; 
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import java.awt.geom.*;
    
    class projRx extends JFrame implements ChangeListener,
                                           Runnable,
                                           ActionListener
    { 
    //    public int bottom;
    //    public Thread t;
    //    int padding = 4;
        JFrame f;
        JTabbedPane jt ,jt1;
        JPanel /*jp,jp1,*/ jp2,jp3,jp4;
        JButton j,j1,j2;
        JLabel lab,lab1;
        JComboBox jcb,jcb1;
        JColorChooser jc,jc1;
        Color col1,c2;
    //    public Graphics g;
    //    public Graphics g1;
    //    public Graphics2D g2d;
    //    JDialog dialog;
        String k;
    //    Line2D shape1;
    //    f p1 = new f();
        // we need to access fields inside this class
        pan jp;
        pan jp1;
    
        public projRx()
        {
            f = new JFrame("Graph");
            /*pan*/ jp = new pan();
            /*pan*/ jp1 = new pan();
    
            jt = new JTabbedPane(JTabbedPane.TOP);
            jt.setTabPlacement(JTabbedPane.TOP);
    
            jp.setPreferredSize(new Dimension(50,50));
            jp.setLayout(new BorderLayout());
            jp.setBackground(Color.gray);
            jp.setPreferredSize(new Dimension(60,50)); 
    
            jp4 = new JPanel();
            j = new JButton("Draw the graph");
            j.addActionListener(this);
            jp4.setBorder(BorderFactory.createRaisedBevelBorder());
            jp4.setPreferredSize(new Dimension(130,20));
            jp4.add(j);
    
            jp.add(jp4,BorderLayout.EAST); 
            String[] a = "1 2 3 4 5".split("\\s");
            System.out.println(Arrays.toString(a));
            lab = new JLabel("x parameters");
            lab.setLocation(700,700);
            jp4.add(lab);
            jcb= new JComboBox(a);
            jcb.addActionListener(this);
            jp4.add(jcb);
            lab1 = new JLabel("Y parameters");
            jp4.add(lab1);
            jcb1= new JComboBox(a);
            jcb1.addActionListener(this);
            jp4.add(jcb1);
            jt.addTab("graph1", jp);
    
            jp1.setBackground(Color.black); 
            jt.addTab("Graph2",jp1);
            JPanel jp3=new JPanel();
    
            jc = new JColorChooser(Color.yellow);
            jc.createDialog(jp3,"Choose Background Color",true, jc, null,null);
            jc.getSelectionModel().addChangeListener(this);
            jp3.add(jc);
            jt.addTab("Change Grid Color", jp3);
            f.add(jt);
            f.pack();
            f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
            f.setSize(400,400);
            f.setVisible(true);
        }
    
    
        public void run()
        {
            // this is for animations, not intitalizing your gui.
            System.out.println("run");
        }
    
        public void stateChanged(ChangeEvent e)
        {
    
        }
    
        public void actionPerformed(ActionEvent e)
        {
            System.out.println(e.getActionCommand());
            if(e.getSource() == jcb)
            {
                int n = jcb.getSelectedIndex() +1;
                System.out.println("run = " + n);
                jp.run = n;
            }
            if(e.getSource() == jcb1)
            {
                int n = jcb1.getSelectedIndex() +1;
                System.out.println("rise = " + n);
                jp.rise = n;
            }
            jp.repaint();
        }
    
        public static void main(String[]args)
        {
            projRx lp = new projRx();
            Thread t = new Thread(lp);
            t.start();
        } 
    
        public class pan extends JPanel
        {
            // Set these two member variables from the
            // JComboBox ActionListener above.
            public int run = 1;
            public int rise = 1; 
    
            protected void paintComponent(Graphics g)
            { 
                super.paintComponent(g);
                Color col1=jc.getColor();
                g.setColor(col1);
                Rectangle clip = new Rectangle(30,30,800,600);
                g.drawRect( 30,30,800,600); //600 = width 400 = height
                // draw vertical lines
                for(int i=30;i<=620; i=i+20)
                { 
                    g.drawLine(30,i,830,i);
                }
                // draw ordinate labels
                g.setColor(Color.white);
                for(int i=10;i<=620; i=i+20)
                {
                    k=Integer.toString(i); // or String.valueOf(i)
                    g.drawString(k,5,i+27);
                }
    
                // draw horizontal lines
                for(int i=10;i<=820; i=i+20)
                {
                    int l = i/10;
                    g.drawString(Integer.toString(l),i+17,650);
                }
                // draw abcissa labels
                g.setColor(col1);
                for(int i =0;i<=800;i=i+20)
                {
                    g.drawLine(i+30,30,i+30,630);
                }
    
                int S = 30;  // cellSize
                int unitsPerCell = 20;
                int w = 800;
                int h = 600;
                // line origin at graph origin
                int x0 = S;
                int y0 = h + S;
    
                double slope = 1.0;
                if(run != 0)
                {
                    slope = (double)rise/run;
                }
                int x = w + S;
                int y = y0 - (int)(slope*x);
                g.setColor(Color.red);
                // show only the line segment that is
                // inside clip/graph bounds
                Shape origClip = g.getClip();
                g.setClip(clip);
                g.drawLine(x0, y0, x, y);
                // reset, g is usede by other methods later on
                g.setClip(origClip);
    /*
                // navigation markers for design/debug
                g.setColor(Color.green.darker());
                // mark origin
                g.fillOval(x0-2, y0-2, 4, 4);
                // mark se corner of graph
                g.fillOval(S+w-2, y0-2, 4, 4);
                // mark ne corner
                g.fillOval(S+w-2, S-2, 4, 4);
    */
            }
        } 
    }

  4. #4
    sims is offline Member
    Join Date
    Jul 2008
    Posts
    9
    Rep Power
    0

    Default

    sir hello...
    here on gui after ordinates are drawn ,,i want the user to specify drawLine parameters(i.e x1,y1,x2,y2),,dynamically.
    my second requirment is graph should be not be drawn at once but with movements with slight delay visible to the user.user should be able to draw any number of lines.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Use sleep() to delay, but NOT in the paint method. Do the computations for the line location on a thread call repaint() to have the next line drawn, sleep some and repeat.

Posting Permissions

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