Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:42:16+00:00 2026-05-26T23:42:16+00:00

I have a frame on which i have created a main panel. On that

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:42:16+00:00Added an answer on May 26, 2026 at 11:42 pm

    There are so many crazy stuff in this code. I think this is want you wanted to do:

    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 ) );
        public JPanel jp2 = new JPanel( new BorderLayout() );
        Myclass       my  = new Myclass( 100, 200, 300, 400, 500, 600, 700, 900, 800, 10000, 3000, 600 );
    
        public static void main( String args[] ) {
            new Iframe();
        }
    
        public Iframe() {
            Border border = BorderFactory.createTitledBorder( "Pentahexes" );
            jp1.setBorder( border );
    
            ImageIcon Pentaheximg1 = new ImageIcon( "E:/NetBeans/Pentahex-Game/src/pentahex/game/Pentahex1.jpg", "b1" );
            JButton Pentahexbutton1 = new JButton( Pentaheximg1 );
            Pentahexbutton1.addActionListener( this );
            Pentahexbutton1.setName( "b1" );
            jp1.add( Pentahexbutton1 );
    
            ImageIcon Pentaheximg2 = new ImageIcon( "E:/NetBeans/Pentahex-Game/src/pentahex/game/Pentahex2.jpg", "b1" );
            JButton Pentahexbutton2 = new JButton( Pentaheximg2 );
            Pentahexbutton2.addActionListener( this );
            Pentahexbutton2.setName( "b2" );
            jp1.add( Pentahexbutton2 );
    
            ImageIcon Pentaheximg3 = new ImageIcon( "E:/NetBeans/Pentahex-Game/src/pentahex/game/Pentahex3.jpg", "b1" );
            JButton Pentahexbutton3 = new JButton( Pentaheximg3 );
            Pentahexbutton3.addActionListener( this );
            Pentahexbutton3.setName( "b3" );
            jp1.add( Pentahexbutton3 );
    
            Border border2 = BorderFactory.createTitledBorder( "Puzzle" );
            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( "b1".equals( ((JButton)e.getSource()).getName() ) ) {
                my.setData( 100, 200, 300, 400, 500, 600, 700, 900, 800, 10000, 3000, 600 );
            }
            if( "b2".equals( ((JButton)e.getSource()).getName() ) ) {
                my.setData( 10, 20, 30, 400, 50, 60, 70, 80, 90, 100, 110, 120 );
            }
            if( "b3".equals( ((JButton)e.getSource()).getName() ) ) {
                my.setData( 10, 30, 45, 400, 60, 75, 90, 80, 90, 1000, 3010, 120 );
            }
            my.drawpoly();
        }
    }
    
    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 ) {
            setData( x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6 );
        }
    
        public void setData( 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() {
            invalidate();
            repaint();
        }
    
        @Override
        public void paintComponent( Graphics g ) {
            super.paintComponent( g );
            int xcoordinatesofthehexagon[] = new int[] { a, c, e, g1, i, k };
            int ycoordinatesofthehexagon[] = new int[] { b, d, f, h, j, l };
            g.drawPolygon( xcoordinatesofthehexagon, ycoordinatesofthehexagon, 6 );
        }
    }
    

    UPDATE

    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map.Entry;
    
    import javax.swing.border.Border;
    
    public class Iframe extends JFrame implements ActionListener {
    
        private static final String PATH = "E:/NetBeans/Pentahex-Game/src/pentahex/game/";
    
        HashMap<String, int[]> pentahexMap = new HashMap<String, int[]>() {{
    
            put( "Clear",     new int[]{ 0,0,0,0,0,0,0,0,0,0,0,0 } );
            put( "Pentahex1", new int[]{ 0,0,0,50,50,50,25,25,50,0,0,0 } );
            put( "Pentahex2", new int[]{ 100,100,100,150,150,150,125,125,150,100,100,100 } );
            put( "Pentahex3", new int[]{ 10, 30, 45, 400, 60, 75, 90, 80, 90, 500, 310, 120 } );
    
        }};
    
        private Myclass my = new Myclass();
    
        public static void main( String args[] ) {
            SwingUtilities.invokeLater( new Runnable() {
                @Override
                public void run() {
                    new Iframe().setVisible( true );
                }
            });
        }
    
        public Iframe() {
    
            JPanel jp0 = new JPanel( new GridLayout() );
            JPanel jp1 = new JPanel( new GridLayout( 1, 3 ) );
            JPanel jp2 = new JPanel( new BorderLayout() );
    
            for( Entry<String, int[]> entry : pentahexMap.entrySet() ) {
                JButton pentahexbutton1;
                try {
                    URL imgUrl                = new URL(PATH + entry.getKey() +".jpg");
                    ImageIcon pentaheximg1    = new ImageIcon( imgUrl );
                    pentahexbutton1           = new JButton( pentaheximg1 );
                } catch( MalformedURLException e ) {
                    pentahexbutton1 = new JButton( entry.getKey() );
                }
                pentahexbutton1.addActionListener( this );
                pentahexbutton1.setName( entry.getKey() );
                jp1.add( pentahexbutton1 );
            }
    
            jp1.setBorder( BorderFactory.createTitledBorder( "Pentahexes" ) );
            jp2.setBorder( BorderFactory.createTitledBorder( "Puzzle" ) );
    
            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 );
        }
    
        @Override
        public void actionPerformed( ActionEvent e ) {
            int[] data = pentahexMap.get( ((JButton)e.getSource()).getName() );
            if(data != null) {
                my.setData( data );
                my.invalidate();
                my.repaint();
            }
        }
    }
    
    class Myclass extends JPanel {
        int a, b, c, d, e, f, g, h, i, j, k, l;
        Myclass(){}
        Myclass( int... data ) {
            setData( data );
        }
        public void setData( int... data ) {
            if(data.length < 12) {
                throw new IllegalArgumentException("not enough data");
            }
            a = data[0];
            b = data[1];
            c = data[2];
            d = data[3];
            e = data[4];
            f = data[5];
            g = data[6];
            h = data[7];
            i = data[8];
            j = data[9];
            k = data[10];
            l = data[11];
        }
        @Override
        public void paintComponent( Graphics graphics ) {
            super.paintComponent( graphics );
            int[] xcoordinatesofthehexagon = new int[] { a, c, e, g, i, k };
            int[] ycoordinatesofthehexagon = new int[] { b, d, f, h, j, l };
            if(!isEmpty(xcoordinatesofthehexagon, ycoordinatesofthehexagon)){
                graphics.drawPolygon( xcoordinatesofthehexagon, ycoordinatesofthehexagon, 6 );
            }
        }
        private boolean isEmpty(int[] x,int[] y) {
            for( int i : y ) {
                if(i != 0) {
                    return false;
                }
            }
            for( int i : x ) {
                if(i != 0) {
                    return false;
                }
            }
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a wx.Frame, in which there is a main wx.Panel with several widgets
I have a data.frame called series_to_plot.df which I created by combining a number of
I have created a swing application as bellow which shows main tasks in tabs
I've created a frame (mainframe) for my program in the main class which I
I created view-based application. I have got a sampleGameViewContrioller.xib, which contains main View, and
I am about to present my work that I have created in MATLAB, but
I have main UIScrollView with lots off buttons which i create like this: UIButton
I have a UIScrollView which I create and size dynamically using... scrollView.contentSize = CGSizeMake(scrollView.frame.size.width
I have a WPF Window which has a among other controls hosts a Frame.
I have an XBAP WPF application which displays various pages inside of a Frame.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.