470,602 Members | 1,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,602 developers. It's quick & easy.

Problem with java layout and 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
Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. public class CaseStudy extends JApplet
  6. {
  7.  
  8.     public void init()
  9.     {
  10.         Container content=getContentPane();
  11.         Mypanel panel=new Mypanel();
  12.         content.add(panel);
  13.         resize(800,400);
  14.         show();
  15.     }
  16. }
  17. class Mypanel extends JPanel implements ActionListener
  18.  
  19.     {
  20.         JRadioButton line,circle,rectangle,triangle,pie,trapezoid,octagon;
  21.         ButtonGroup  group;
  22.         JTextField text1,text2,text3,text4,text5,text6,text7,text8;
  23.         JLabel label1,label2,label3,label4,label5,label6,label7,label8;
  24.         JButton draw,showcoor;
  25.         int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
  26.         int shapes = 0;
  27.         public Mypanel()
  28.  
  29.         {
  30.  
  31.  
  32.  
  33.  
  34.  
  35.             JPanel panel1=new JPanel();
  36.             panel1.setLayout(new GridLayout(1,2));
  37.             panel1.setBackground(Color.red);
  38.  
  39.             JPanel panel2=new JPanel();
  40.             panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
  41.             panel2.setBackground(Color.green);
  42.  
  43.             JPanel panel3=new JPanel();
  44.             panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
  45.             panel3.setBackground(Color.blue);
  46.  
  47.  
  48.             JPanel panel4=new JPanel();
  49.             panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
  50.             panel4.setBackground(Color.pink);
  51.  
  52.  
  53.  
  54.  
  55.             line = new JRadioButton("LINE" , true);
  56.             line.addActionListener(this);
  57.             circle = new JRadioButton("CIRCLE" , false);
  58.             circle.addActionListener(this);
  59.             rectangle = new JRadioButton("RECTANGLE" , false);
  60.             rectangle.addActionListener(this);
  61.             triangle = new JRadioButton("TRIANGLE" , false);
  62.             triangle.addActionListener(this);
  63.             pie = new JRadioButton("PIE"  , false);
  64.             pie.addActionListener(this);
  65.             trapezoid = new JRadioButton("TRAPEZOID"  , false);
  66.             trapezoid .addActionListener(this);
  67.             octagon = new JRadioButton("OCTAGON"  , false);
  68.             octagon .addActionListener(this);
  69.  
  70.  
  71.             text1=new JTextField(4);
  72.             text2=new JTextField(4);
  73.             text3=new JTextField(4);
  74.             text4=new JTextField(4);
  75.             text5=new JTextField(4);
  76.             text6=new JTextField(4);
  77.             text7=new JTextField(4);
  78.             text8=new JTextField(4);
  79.             label1=new JLabel ("X Coor");
  80.             label2=new JLabel ("Y Coor");
  81.             label3=new JLabel ("Width");
  82.             label4=new JLabel ("Length");
  83.             label5=new JLabel ("Start Angle");
  84.             label6=new JLabel ("Arc Angle");
  85.             label7=new JLabel ("X 2 Coor");
  86.             label8=new JLabel ("Y 2 Coor");
  87.  
  88.  
  89.             draw=new JButton("DRAW");
  90.             showcoor=new JButton("Show Coordinates");
  91.             panel2.add(line);
  92.             panel2.add(circle);
  93.             panel2.add(rectangle);
  94.             panel2.add(triangle);
  95.             panel2.add(pie);
  96.             panel2.add(trapezoid);
  97.             panel2.add(octagon);
  98.             panel3.add(text1);
  99.             panel3.add(text2);
  100.             panel3.add(text3);
  101.             panel3.add(text4);
  102.             panel3.add(text5);
  103.             panel3.add(text6);
  104.             panel3.add(text7);
  105.             panel3.add(text8);
  106.             panel4.add(label1);
  107.             panel4.add(label2);
  108.             panel4.add(label3);
  109.             panel4.add(label4);
  110.             panel4.add(label5);
  111.             panel4.add(label6);
  112.             panel4.add(label7);
  113.             panel4.add(label8);
  114.             panel2.add(draw);
  115.             panel2.add(showcoor);
  116.             draw.addActionListener(this);
  117.  
  118.  
  119.             group=new ButtonGroup();
  120.             group.add(line);
  121.             group.add(circle);
  122.             group.add(rectangle);
  123.             group.add(triangle);
  124.             group.add(pie);
  125.             group.add(trapezoid);
  126.             group.add(octagon);
  127.  
  128.  
  129.  
  130.             add(panel1);
  131.             add(panel2);
  132.             add(panel3);
  133.             add(panel4);
  134.             }
  135.             public void actionPerformed(ActionEvent ev)
  136.             {
  137.             String pindot = ev.getActionCommand();
  138.  
  139.  
  140.         if(pindot=="LINE"){
  141.         shapes =1;
  142.             repaint();
  143.         }else if (pindot== "CIRCLE"){
  144.         shapes =2;
  145.             repaint();
  146.         }else if (pindot== "RECTANGLE"){
  147.         shapes =3;
  148.             repaint();
  149.         }else if (pindot== "TRIANGLE"){
  150.         shapes =4;
  151.             repaint();
  152.         }else if (pindot== "PIE"){
  153.                 shapes =5;
  154.             repaint();
  155.         }else if (pindot== "TRAPEZOID"){
  156.                 shapes =6;
  157.             repaint();
  158.         }else if (pindot== "OCTAGON"){
  159.                 shapes =7;
  160.             repaint();
  161.  
  162.             x1=Integer.parseInt(text1.getText());
  163.             x2=Integer.parseInt(text2.getText());
  164.             x3=Integer.parseInt(text3.getText());
  165.             x4=Integer.parseInt(text4.getText());
  166.             y1=Integer.parseInt(text5.getText());
  167.             y2=Integer.parseInt(text6.getText());
  168.             y3=Integer.parseInt(text7.getText());
  169.             y4=Integer.parseInt(text8.getText());
  170.  
  171.  
  172.  
  173.         }
  174.  
  175.         }
  176.         public void paintComponent(Graphics a)
  177.             {
  178.                 a.setColor(new Color(162,251,160));
  179.                 a.fillRect(0,0,400,400);
  180.  
  181.                 a.setColor(Color.black);
  182.                         for(int i=10;i<400;i=i+10){
  183.                             a.drawLine(0,i,400,i);
  184.                             a.drawLine(i,0,i,400);
  185.                         }
  186.  
  187.  
  188.                     if (shapes==1){
  189.                     a.setColor(new Color(116,19,215));
  190.                     a.drawLine(x1,x2,x3,x4);
  191.  
  192.                     }else if (shapes==2){
  193.                     a.setColor(new Color(116,19,215));
  194.                     a.fillOval(x1,x2,x3,x4);
  195.  
  196.                     }else if (shapes==3){
  197.                     a.setColor(new Color(116,19,215));
  198.                     a.fillRect(x1,x2,x3,x4);
  199.  
  200.  
  201.                     }else if (shapes==4){
  202.                     a.setColor(new Color(116,19,215));
  203.                     int x []={x1,x2,x3};
  204.                     int y []={x4,y1,y2};
  205.                     a.fillPolygon(x,y,3);
  206.  
  207.                     }else if (shapes==5){
  208.                     a.setColor(new Color(116,19,215));
  209.                     a.fillArc(x1,x2,x3,x4,y1,y2);
  210.  
  211.  
  212.                     }else if (shapes==6){
  213.  
  214.                     a.setColor(new Color(116,19,215));
  215.                     a.fillRect(x1,x2,x3,x4);
  216.                     a.setColor(new Color(162,251,160));
  217.                     int x12 []={x1,x1,x1+x3/4};
  218.                     int y12 []={x2,x2+x4,x2};
  219.                     a.fillPolygon(x12,y12,3);
  220.                     a.setColor(new Color(162,251,160));
  221.                     int x13 []={x1+x3,x1+x3,x1+x3/4*3};
  222.                     int y13 []={x2,x2+x4,x2};
  223.                     a.fillPolygon(x13,y13,3);
  224.  
  225.  
  226.                     }if (shapes==7){
  227.  
  228.                     a.setColor(new Color(116,19,215));
  229.                     int x []={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
  230.                     int y []={x2,x2-x4/2,x2-x4/2,x2};
  231.                     a.fillPolygon(x,y,4);
  232.                     a.setColor(new Color(116,19,215));
  233.                     int x11[]={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
  234.                     int y11 []={x2+x4,x2+x4+x4/2,x2+x4+x4/2,x2+x4};
  235.                     a.fillPolygon(x11,y11,4);
  236.                     a.setColor(new Color(116,19,215));
  237.                     a.fillRect(x1,x2,x3,x4);
  238.  
  239.  
  240.  
  241. }
  242.  
  243.  
  244.             }
  245.  
  246.     }
  247.  

and this is the layout out i wanted the grid should be in the left panel and all the components are in the right panel so when i draw i specific shape the shape will be in the left panel

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class JPanels extends JFrame {
  5. public static void main(String[] args) {
  6. new JPanels();
  7. }
  8.  
  9. JRadioButton line,circle,rectangle,triangle,pie,trapezoid,octagon;
  10. ButtonGroup  group;
  11. JTextField text1,text2,text3,text4,text5,text6,text7,text8;
  12. JLabel label1,label2,label3,label4,label5,label6;
  13. JButton draw,showcoor;
  14. int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
  15. int shapes = 0;
  16.  
  17. public JPanels () {
  18. super("Test Window");
  19. Container content = getContentPane();
  20. content.setBackground(Color.lightGray);
  21.  
  22. JPanel bothsides = new JPanel(new GridLayout(1, 2));
  23. bothsides.setBackground(Color.DARK_GRAY);
  24.  
  25.  
  26. JPanel leftside = new JPanel();
  27. leftside.setBackground(Color.DARK_GRAY);
  28. leftside.setPreferredSize(new Dimension(400, 200));
  29.  
  30.  
  31. JPanel rightside = new JPanel();
  32. rightside.setBackground(Color.PINK);
  33. rightside.setPreferredSize(new Dimension(400, 200));
  34.  
  35.  
  36. text1=new JTextField(4);
  37.             text2=new JTextField(4);
  38.             text3=new JTextField(4);
  39.             text4=new JTextField(4);
  40.             text5=new JTextField(4);
  41.             text6=new JTextField(4);
  42.             text7=new JTextField(4);
  43.             text8=new JTextField(4);
  44.             label1=new JLabel ("X Coor");
  45.             label2=new JLabel ("Y Coor");
  46.             label3=new JLabel ("Width");
  47.             label4=new JLabel ("Length");
  48.             label5=new JLabel ("Width");
  49.             label6=new JLabel ("Length");
  50.  
  51.  
  52.         rightside.add(text1);
  53.         rightside.add(text2);
  54.         rightside.add(text3);
  55.         rightside.add(text4);
  56.         rightside.add(text5);
  57.         rightside.add(text6);
  58.         rightside.add(text7);
  59.         rightside.add(text8);
  60.  
  61.  
  62.  
  63. bothsides.add(leftside);
  64. bothsides.add(rightside);
  65.  
  66. content.add(bothsides);
  67.  
  68. pack();
  69. setVisible(true);
  70. }
  71. }
  72.  
  73.  
Mar 19 '11 #1
0 1635

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by knoak | last post: by
2 posts views Thread by Avin Patel | last post: by
12 posts views Thread by Mahesh Devjibhai Dhola [MVP] | last post: by
2 posts views Thread by desperateboy | last post: by
3 posts views Thread by Andrea Gasperi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.