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
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
my program
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 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
Code:
import java.awt.*;
import javax.swing.*;
public class JPanels extends JFrame {
public static void main(String[] args) {
new JPanels();
}
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;
JButton draw,showcoor;
int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
int shapes = 0;
public JPanels () {
super("Test Window");
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, 200));
JPanel rightside = new JPanel();
rightside.setBackground(Color.PINK);
rightside.setPreferredSize(new Dimension(400, 200));
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 ("Width");
label6=new JLabel ("Length");
rightside.add(text1);
rightside.add(text2);
rightside.add(text3);
rightside.add(text4);
rightside.add(text5);
rightside.add(text6);
rightside.add(text7);
rightside.add(text8);
bothsides.add(leftside);
bothsides.add(rightside);
content.add(bothsides);
pack();
setVisible(true);
}
}