Results 1 to 2 of 2
  1. #1
    jupzter is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Problem with java layout , panels and containers

    OK Guyz I'm very new to JAVA I have two programs here the one is my Case Study where you put coordinates width and height and you choose from a list of shapes and its draw it... My main problem is I cannot arrange the JButton JRadioButton and JTextField and JLabel to be in the right direction I wanted it to have two separate panels kinda like this program i grab in the net the problem is I cannot incorporate the two programs together Any help will be much appreciated

    my program

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class CaseStudy extends JApplet
    {
    
    	public void init()
    	{
    		Container content=getContentPane();
    		Mypanel panel=new Mypanel();
    		content.add(panel);
    		resize(800,400);
    		show();
    	}
    }
    class Mypanel extends JPanel implements ActionListener
    
    	{
    		JRadioButton line,circle,rectangle,triangle,pie,trapezoid,octagon;
    		ButtonGroup  group;
    		JTextField text1,text2,text3,text4,text5,text6,text7,text8;
    		JLabel label1,label2,label3,label4,label5,label6,label7,label8;
    		JButton draw,showcoor;
    		int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
    		int shapes = 0;
    		public Mypanel()
    
    		{
    
    
    
    
    
    			JPanel panel1=new JPanel();
    			panel1.setLayout(new GridLayout(1,2));
    			panel1.setBackground(Color.red);
    
    			JPanel panel2=new JPanel();
    			panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    			panel2.setBackground(Color.green);
    
    			JPanel panel3=new JPanel();
    			panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
    			panel3.setBackground(Color.blue);
    
    
    			JPanel panel4=new JPanel();
    			panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
    			panel4.setBackground(Color.pink);
    
    
    
    
    			line = new JRadioButton("LINE" , true);
    			line.addActionListener(this);
    			circle = new JRadioButton("CIRCLE" , false);
    			circle.addActionListener(this);
    			rectangle = new JRadioButton("RECTANGLE" , false);
    			rectangle.addActionListener(this);
    		    triangle = new JRadioButton("TRIANGLE" , false);
    		    triangle.addActionListener(this);
    		    pie = new JRadioButton("PIE"  , false);
    		    pie.addActionListener(this);
    		    trapezoid = new JRadioButton("TRAPEZOID"  , false);
    		    trapezoid .addActionListener(this);
    		    octagon = new JRadioButton("OCTAGON"  , false);
    		    octagon .addActionListener(this);
    
    
    			text1=new JTextField(4);
    			text2=new JTextField(4);
    			text3=new JTextField(4);
    			text4=new JTextField(4);
    			text5=new JTextField(4);
    			text6=new JTextField(4);
    			text7=new JTextField(4);
    			text8=new JTextField(4);
    			label1=new JLabel ("X Coor");
    			label2=new JLabel ("Y Coor");
    			label3=new JLabel ("Width");
    			label4=new JLabel ("Length");
    			label5=new JLabel ("Start Angle");
    			label6=new JLabel ("Arc Angle");
    			label7=new JLabel ("X 2 Coor");
    			label8=new JLabel ("Y 2 Coor");
    
    
    			draw=new JButton("DRAW");
    			showcoor=new JButton("Show Coordinates");
    			panel2.add(line);
    			panel2.add(circle);
    			panel2.add(rectangle);
    			panel2.add(triangle);
    			panel2.add(pie);
    			panel2.add(trapezoid);
    			panel2.add(octagon);
    			panel3.add(text1);
    			panel3.add(text2);
    			panel3.add(text3);
    			panel3.add(text4);
    			panel3.add(text5);
    			panel3.add(text6);
    			panel3.add(text7);
    			panel3.add(text8);
    			panel4.add(label1);
    			panel4.add(label2);
    			panel4.add(label3);
    			panel4.add(label4);
    			panel4.add(label5);
    			panel4.add(label6);
    			panel4.add(label7);
    			panel4.add(label8);
    			panel2.add(draw);
    			panel2.add(showcoor);
    			draw.addActionListener(this);
    
    
    			group=new ButtonGroup();
    			group.add(line);
    			group.add(circle);
    			group.add(rectangle);
    			group.add(triangle);
    			group.add(pie);
    			group.add(trapezoid);
    			group.add(octagon);
    
    
    
    			add(panel1);
    			add(panel2);
    			add(panel3);
    			add(panel4);
    			}
    			public void actionPerformed(ActionEvent ev)
    			{
    			String pindot = ev.getActionCommand();
    
    
    		if(pindot=="LINE"){
    		shapes =1;
    			repaint();
    		}else if (pindot== "CIRCLE"){
    		shapes =2;
    			repaint();
    		}else if (pindot== "RECTANGLE"){
    		shapes =3;
    			repaint();
    		}else if (pindot== "TRIANGLE"){
    		shapes =4;
    			repaint();
    		}else if (pindot== "PIE"){
    				shapes =5;
    			repaint();
    		}else if (pindot== "TRAPEZOID"){
    				shapes =6;
    			repaint();
    		}else if (pindot== "OCTAGON"){
    				shapes =7;
    			repaint();
    
    			x1=Integer.parseInt(text1.getText());
    			x2=Integer.parseInt(text2.getText());
    			x3=Integer.parseInt(text3.getText());
    			x4=Integer.parseInt(text4.getText());
    			y1=Integer.parseInt(text5.getText());
    			y2=Integer.parseInt(text6.getText());
    			y3=Integer.parseInt(text7.getText());
    			y4=Integer.parseInt(text8.getText());
    
    
    
    		}
    
    		}
    		public void paintComponent(Graphics a)
    			{
    				a.setColor(new Color(162,251,160));
    				a.fillRect(0,0,400,400);
    
    				a.setColor(Color.black);
    						for(int i=10;i<400;i=i+10){
    							a.drawLine(0,i,400,i);
    							a.drawLine(i,0,i,400);
    						}
    
    
    					if (shapes==1){
    					a.setColor(new Color(116,19,215));
    					a.drawLine(x1,x2,x3,x4);
    
    					}else if (shapes==2){
    					a.setColor(new Color(116,19,215));
    					a.fillOval(x1,x2,x3,x4);
    
    					}else if (shapes==3){
    					a.setColor(new Color(116,19,215));
    					a.fillRect(x1,x2,x3,x4);
    
    
    					}else if (shapes==4){
    					a.setColor(new Color(116,19,215));
    					int x []={x1,x2,x3};
    					int y []={x4,y1,y2};
    					a.fillPolygon(x,y,3);
    
    					}else if (shapes==5){
    					a.setColor(new Color(116,19,215));
    					a.fillArc(x1,x2,x3,x4,y1,y2);
    
    
    					}else if (shapes==6){
    
    					a.setColor(new Color(116,19,215));
    					a.fillRect(x1,x2,x3,x4);
    					a.setColor(new Color(162,251,160));
    					int x12 []={x1,x1,x1+x3/4};
    					int y12 []={x2,x2+x4,x2};
    					a.fillPolygon(x12,y12,3);
    					a.setColor(new Color(162,251,160));
    					int x13 []={x1+x3,x1+x3,x1+x3/4*3};
    					int y13 []={x2,x2+x4,x2};
    					a.fillPolygon(x13,y13,3);
    
    
    					}if (shapes==7){
    
    					a.setColor(new Color(116,19,215));
    					int x []={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
    					int y []={x2,x2-x4/2,x2-x4/2,x2};
    					a.fillPolygon(x,y,4);
    					a.setColor(new Color(116,19,215));
    					int x11[]={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
    					int y11 []={x2+x4,x2+x4+x4/2,x2+x4+x4/2,x2+x4};
    					a.fillPolygon(x11,y11,4);
    					a.setColor(new Color(116,19,215));
    					a.fillRect(x1,x2,x3,x4);
    
    
    
    }
    			}
    	}
    and this is the interface i wanted there are two panels all the components like JButton JTextField JLabel are on the rightpanel and the grid are in the left panel and the paintCOmponents should appear in the leftpanel unfortunately the graphics are not working the grid is gone and theres no more output


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class CaseStudy extends JApplet
    {
    
    	public void init()
    	{
    
    		Mypanel bothsides=new Mypanel();
    
    	}
    }
    class Mypanel extends JFrame implements ActionListener
    
    	{
    		JRadioButton line,circle,rectangle,triangle,pie,trapezoid,octagon;
    		ButtonGroup  group;
    		JTextField text1,text2,text3,text4,text5,text6,text7,text8;
    		JLabel label1,label2,label3,label4,label5,label6,label7,label8;
    		JButton draw,showcoor;
    		int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
    		int shapes = 0;
    		public Mypanel()
    
    		{
    
    
    			Container content = getContentPane();
    			content.setBackground(Color.lightGray);
    
    			JPanel bothsides = new JPanel(new GridLayout(1, 2));
    			bothsides.setBackground(Color.DARK_GRAY);
    
    
    			JPanel leftside = new JPanel();
    			leftside.setBackground(Color.DARK_GRAY);
    			leftside.setPreferredSize(new Dimension(400, 400));
    
    
    			JPanel rightside = new JPanel();
    			rightside.setBackground(Color.PINK);
    			rightside.setPreferredSize(new Dimension(400, 400));
    
    
    
    
    			line = new JRadioButton("LINE" , true);
    			line.addActionListener(this);
    			circle = new JRadioButton("CIRCLE" , false);
    			circle.addActionListener(this);
    			rectangle = new JRadioButton("RECTANGLE" , false);
    			rectangle.addActionListener(this);
    		    triangle = new JRadioButton("TRIANGLE" , false);
    		    triangle.addActionListener(this);
    		    pie = new JRadioButton("PIE"  , false);
    		    pie.addActionListener(this);
    		    trapezoid = new JRadioButton("TRAPEZOID"  , false);
    		    trapezoid .addActionListener(this);
    		    octagon = new JRadioButton("OCTAGON"  , false);
    		    octagon .addActionListener(this);
    
    
    			text1=new JTextField(4);
    			text2=new JTextField(4);
    			text3=new JTextField(4);
    			text4=new JTextField(4);
    			text5=new JTextField(4);
    			text6=new JTextField(4);
    			text7=new JTextField(4);
    			text8=new JTextField(4);
    			label1=new JLabel ("X Coor");
    			label2=new JLabel ("Y Coor");
    			label3=new JLabel ("Width");
    			label4=new JLabel ("Length");
    			label5=new JLabel ("Start Angle");
    			label6=new JLabel ("Arc Angle");
    			label7=new JLabel ("X 2 Coor");
    			label8=new JLabel ("Y 2 Coor");
    
    
    			draw=new JButton("DRAW");
    			showcoor=new JButton("Show Coordinates");
    			rightside.add(line);
    			rightside.add(circle);
    			rightside.add(rectangle);
    			rightside.add(triangle);
    			rightside.add(pie);
    			rightside.add(trapezoid);
    			rightside.add(octagon);
    			rightside.add(text1);
    			rightside.add(text2);
    			rightside.add(text3);
    			rightside.add(text4);
    			rightside.add(text5);
    			rightside.add(text6);
    			rightside.add(text7);
    			rightside.add(text8);
    			rightside.add(label1);
    			rightside.add(label2);
    			rightside.add(label3);
    			rightside.add(label4);
    			rightside.add(label5);
    			rightside.add(label6);
    			rightside.add(label7);
    			rightside.add(label8);
    			rightside.add(draw);
    			rightside.add(showcoor);
    			draw.addActionListener(this);
    
    
    			group=new ButtonGroup();
    			group.add(line);
    			group.add(circle);
    			group.add(rectangle);
    			group.add(triangle);
    			group.add(pie);
    			group.add(trapezoid);
    			group.add(octagon);
    
    
    
    			bothsides.add(leftside);
    			bothsides.add(rightside);
    
    			content.add(bothsides);
    
    			pack();
    			setVisible(true);
    
    
    			}
    			public void actionPerformed(ActionEvent ev)
    			{
    			String pindot = ev.getActionCommand();
    
    
    		if(pindot=="LINE"){
    		shapes =1;
    			repaint();
    		}else if (pindot== "CIRCLE"){
    		shapes =2;
    			repaint();
    		}else if (pindot== "RECTANGLE"){
    		shapes =3;
    			repaint();
    		}else if (pindot== "TRIANGLE"){
    		shapes =4;
    			repaint();
    		}else if (pindot== "PIE"){
    				shapes =5;
    			repaint();
    		}else if (pindot== "TRAPEZOID"){
    				shapes =6;
    			repaint();
    		}else if (pindot== "OCTAGON"){
    				shapes =7;
    			repaint();
    
    			x1=Integer.parseInt(text1.getText());
    			x2=Integer.parseInt(text2.getText());
    			x3=Integer.parseInt(text3.getText());
    			x4=Integer.parseInt(text4.getText());
    			y1=Integer.parseInt(text5.getText());
    			y2=Integer.parseInt(text6.getText());
    			y3=Integer.parseInt(text7.getText());
    			y4=Integer.parseInt(text8.getText());
    
    
    
    		}
    
    		}
    		public void paintComponent(Graphics a)
    			{
    				a.setColor(new Color(162,251,160));
    				a.fillRect(0,0,400,400);
    
    				a.setColor(Color.black);
    						for(int i=10;i<400;i=i+10){
    							a.drawLine(0,i,400,i);
    							a.drawLine(i,0,i,400);
    						}
    
    
    					if (shapes==1){
    					a.setColor(new Color(116,19,215));
    					a.drawLine(x1,x2,x3,x4);
    
    					}else if (shapes==2){
    					a.setColor(new Color(116,19,215));
    					a.fillOval(x1,x2,x3,x4);
    
    					}else if (shapes==3){
    					a.setColor(new Color(116,19,215));
    					a.fillRect(x1,x2,x3,x4);
    
    
    					}else if (shapes==4){
    					a.setColor(new Color(116,19,215));
    					int x []={x1,x2,x3};
    					int y []={x4,y1,y2};
    					a.fillPolygon(x,y,3);
    
    					}else if (shapes==5){
    					a.setColor(new Color(116,19,215));
    					a.fillArc(x1,x2,x3,x4,y1,y2);
    
    
    					}else if (shapes==6){
    
    					a.setColor(new Color(116,19,215));
    					a.fillRect(x1,x2,x3,x4);
    					a.setColor(new Color(162,251,160));
    					int x12 []={x1,x1,x1+x3/4};
    					int y12 []={x2,x2+x4,x2};
    					a.fillPolygon(x12,y12,3);
    					a.setColor(new Color(162,251,160));
    					int x13 []={x1+x3,x1+x3,x1+x3/4*3};
    					int y13 []={x2,x2+x4,x2};
    					a.fillPolygon(x13,y13,3);
    
    
    					}if (shapes==7){
    
    					a.setColor(new Color(116,19,215));
    					int x []={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
    					int y []={x2,x2-x4/2,x2-x4/2,x2};
    					a.fillPolygon(x,y,4);
    					a.setColor(new Color(116,19,215));
    					int x11[]={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
    					int y11 []={x2+x4,x2+x4+x4/2,x2+x4+x4/2,x2+x4};
    					a.fillPolygon(x11,y11,4);
    					a.setColor(new Color(116,19,215));
    					a.fillRect(x1,x2,x3,x4);
    
    
    
    }
    			}
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

Similar Threads

  1. java on mac - gui layout problem
    By dalo in forum AWT / Swing
    Replies: 0
    Last Post: 06-19-2010, 10:15 AM
  2. Not understanding panels and layout?
    By lost_soul in forum New To Java
    Replies: 3
    Last Post: 04-09-2010, 04:31 PM
  3. Problem while using the multiple panels in swings
    By anand206 in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2010, 04:33 PM
  4. Replies: 7
    Last Post: 07-09-2008, 12:06 AM
  5. First Java-game: Containers
    By eastviolence in forum New To Java
    Replies: 0
    Last Post: 04-04-2008, 06:09 PM

Posting Permissions

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