I have a frame on which i have created a main panel.
On that panel are present 2 more panels.What i want is, i have buttons on say panel1,and now, once i click the button, i want to draw a 2d object on the 2nd panel.
I tried a lot of things but it was in vain.Please help me in this case.Any help would be appreciated.
Thanks,
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.border.Border;
public class iframe extends JFrame implements ActionListener{
public JPanel jp0=new JPanel(new GridLayout());
public JPanel jp1=new JPanel(new GridLayout(1,3));
myclass my=new myclass(100,200,300,400,500,600,700,900,800,10000,3000,600);
public JPanel jp2=new JPanel(new BorderLayout());
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public static void main(String args[])
{
iframe f=new iframe();
}
public iframe(myclass my)
{
this.my=my;
Border border = BorderFactory.createTitledBorder("bordersarehers") ;
jp1.setBorder(border);
JButton button1=new JButton();
ImageIcon img1=new ImageIcon("","b1");
button1=new JButton(img1);
button1.addActionListener(this);
jp1.add(button1);
JButton button2=new JButton();
ImageIcon img2=new ImageIcon("","b1");
button2=new JButton(img2);
jp1.add(button2);
JButton button3=new JButton();
ImageIcon img3=new ImageIcon("","b1");
button3=new JButton(img3);
jp1.add(button3);
Border border2 = BorderFactory.createTitledBorder("");
jp2.setBorder(border2);
jp0.add(jp1);
jp0.add(jp2);
jp2.add(my);
this.add(jp0);
this.add(jp2);
this.add(jp1,BorderLayout.EAST);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()=="b1")
{
}
}
}
class myclass extends JPanel
{
int a,b,c,d,e,f,g1,h,i,j,k,l;
myclass(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int x6,int y6)
{
a=x1;
b=y1;
c=x2;
d=y2;
e=x3;
f=y3;
g1=x4;
h=y4;
i=x5;
j=y5;
k=x6;
l=y6;
}
public void drawpoly()
{
repaint();
}
public void paintComponent(Graphics g)
{
int xcoord[]=new int[]{a,c,e,g1,i,k};
int ycoord[]=new int[]{b,d,f,h,j,l};
g.drawPolygon(xcoord,ycoord,6);
}
}
There are so many crazy stuff in this code. I think this is want you wanted to do:
UPDATE