Solved

trying to implement a mutiple jpanels some of which need to be able to paint.Please help!

Posted on 2004-09-22
5
257 Views
Last Modified: 2013-11-23
I'm trying to create a 4x4 grid with buttons as the 4x4 and text around it to label coordinates. Ideally the buttons will have 0's in them once clicked they will turn into 1's. around the 4x4 grid will be this
     
    00  01  11  10
        |   |    |
00    |   |    |
----------------------------
01    |   |    |
------------------------------
11    |   |   |
-------------- ----------------
10    |   |   |



so there is a very rough skketch to help give the idea of what i'm treying to draw. My problem is that i have the buttons created and that was fine. but now i need to draw the fonts 00 , 01,etc around the top and too the left of buttons to give the look of a graph. problem is the clas extends jframe so in order to overide pazint component i need a class that extends jpanel. So i create that class as follows

import javax.swing.JPanel;
import java.awt.*;

class PrettyPanel extends JPanel
{
  PrettyPanel()
  {

   }
   protected void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(Color.blue);
      g2.drawLine(100,250,300,250);
      g2.fillRect(50,50,300,300);

   }
}


ok then in the jframe class i have jgridpanel which is the buttons and a PrettyPanel prettpanel which is the class i then try to add them to the content pain. But my damn gfillrect and drawline  are not painting from paint component that i'm using to test to see if i can get any drawing any ideas why?


code

=====================



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class Kir extends JFrame
{

    private JTextField display;
    private  JButton jbutton;
    public PrettyPanel prettypanel;
    private JPanel leftgridLabel;
    private JPanel topgridLabel;

    public Kir()
    {
        JPanel jpanel = new JPanel();
        jpanel.setLayout(new FlowLayout());
        createControlPanel();





        pack();

    }


    private void createControlPanel()
    {

       // Makes the buttons
        JPanel jgridpanel = new JPanel();
        jgridpanel.setLayout(new GridLayout(4, 4));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));
        jgridpanel.add(makeButton("0"));

        prettypanel = new PrettyPanel();
        prettypanel.setLayout(new FlowLayout());


        //display to show answer
        display = new JTextField("Click solve button to see simplification");
        display.setEditable(false);

        getContentPane().add(prettypanel, "North");

        getContentPane().add(jgridpanel, "Center");
        getContentPane().add(display, "South");
    }


public void drawGrid(Graphics g)
{
      g.drawLine(23,100,300,100);
}

    public JButton makeButton(final String op)
    {
         jbutton  = new JButton(op);
        class ButtonListener
            implements ActionListener
        {

            public void actionPerformed(ActionEvent evt)
            {
                if(evt.getSource() == jbutton)
                {
                           jbutton = new JButton("1");
                    System.out.println("test button creation");
                    display.setText("ll");

                }

            }

            ButtonListener()
            {
            }
        }

        ButtonListener buttonlistener = new ButtonListener();
        jbutton.addActionListener(buttonlistener);
        return jbutton;
    }

public static void main ( String[] args )
  {

        Kir cal = new Kir();

         cal.setTitle("Kirschov Method");
          cal.setSize   ( 500, 500 );
        cal.setVisible( true );




  }


}

============================

import javax.swing.JPanel;
import java.awt.*;

class PrettyPanel extends JPanel
{
  PrettyPanel()
  {

   }
   protected void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(Color.blue);
      g2.drawLine(100,250,300,250);
      g2.fillRect(50,50,300,300);

   }
}





0
Comment
Question by:tyweed420
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12129980
try setting the preferred size for your prettypanel instance
0
 

Author Comment

by:tyweed420
ID: 12129991
in the constructor somehow? i'm not quite sure what you mean? i just looked in the java api under jpanel and found nothing thast would give me the ability to specifiy how large.

it's wierd the code i have the 4x4 grid and the jtextfield take up the entire frame?
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12130001
> it's wierd the code i have the 4x4 grid and the jtextfield take up the entire frame?

I'd say thats because your north panel is empty.

try:

prettypanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 100));
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12138196
First make sure your method is running. Put in a System.out.println("DEBUG"); If it's running we can go from there...
0
 

Author Comment

by:tyweed420
ID: 12152035
Ok i'm still running into major problems. I have tried many different alternatives all have got me back in the same spot.No Where!

the problem is that i'm trying to get the 4x4 grid of buttons and two panels that paint into one single panel. but they wont mix i either get one or the other. Either the buttons show up or just the two paint show up?

    00  01  11  10  <===== thats painted along with the one going horizontal
        |   |    |
00    |   |    |
----------------------------
01    |   |    |
------------------------------
11    |   |   |
-------------- ----------------
10    |   |   |


how do you mix painted components and swing components?

my current idea was to create a class that painted the top numbers

==========================

public class KirPanel1 extends JPanel
{



   public KirPanel1() //Initializes all the mess
   {

   }



    private void paintleftGrid(Graphics g)
    {
                  g.drawString("00" ,15,100);
                  g.drawString("01" ,15,200);
                  g.drawString("11" ,15,300);
                  g.drawString("10" ,15,400);




      }
   /**
      This method is called by the framework each time this component is repainted.
      That happens when the operating system determines that this component needs repainting,
      after minimizing and then restoring, for example, or in response to the user's request via
      the method <code>repaint()</code>. This mechanism is basic to all graphics programming.

      @param g  a <code>Graphics</code> object created by the framework and passed to
      the method. In reality, it is actually a <code>Graphics2D</code> object and can be
      used as such, after appropriate cast
   */
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g); //Should always be here, this is for some housekeeping

      Graphics2D g2 = (Graphics2D)g; //Cast g to <code>Graphics2D</code>

      paintleftGrid(g);

              return;


   }

}








==================================

then another to paint the vertical numbers. Then have a class with three jpanels one to hold the left paint class, one to hold the top paint class,and another to hold the 4x4 grid of buttons


==================== holds everything===========================


public class KirWindow extends javax.swing.JFrame implements ActionListener
{
    private KirPanel1 onepanel;
    private KirPanel2 twopanel;

    private JPanel jgridpanel;
    private JPanel toppanel;

    private JPanel buttPanel;
    private JButton buttSolve;
    private JButton buttExit;

       private JButton one;
         private JButton two;
         private JButton three;
         private JButton four;
         private JButton five;
         private JButton six;
         private JButton seven;
         private JButton eight;
         private JButton nine;
         private JButton ten;
         private JButton eleven;
         private JButton twelve;
         private JButton thirteen;
         private JButton fourteen;
         private JButton fifteen;
         private JButton sixteen;





    /**
      Constructs a basic, closable window.
      @param title - the title to be displayed in the upper bar
    */
    public KirWindow(String title)
    {   super.setTitle(title);

        //Make the window closable , and determine the initial
        //appearence
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Determines the original size and position
        Toolkit kit = getToolkit();
        int width = kit.getScreenSize().width - 10;
        int height = kit.getScreenSize().height - 50;
        this.setSize(width, height);


        //Create the entral, display panel, All the drawing takes place here
        onepanel = new KirPanel1();  <=============left panel which i want on the left
        twopanel = new KirPanel2(); <=============this will be top panel


        toppanel = new JPanel();
               toppanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 100));

        toppanel.setLayout( new FlowLayout());
        toppanel.add(onepanel);                                 <========  this should place them on page but does not!!
        toppanel.add(createControlPanel());
        toppanel.add(twopanel);


        //Add centPanel to the DemoWindow being constructed
        this.getContentPane().add(toppanel);

        //createControlPanel();

        //Bottom panel to hold the buttons
        buttPanel = new JPanel();
        buttPanel.setBackground(Color.black);

        //Create the buttons and add them to the to the buttPanel

        buttSolve = new JButton("Solve");
        buttExit = new JButton("Exit");



        buttSolve.setBackground(Color.gray);
            buttSolve.addActionListener(this);
        buttPanel.add(buttSolve);

        buttExit.setBackground(Color.gray);
            buttExit.addActionListener(this);
        buttPanel.add(buttExit);





        //Add the buttPanel to the DemoWindow object being constructed
        this.getContentPane().add(buttPanel, "South");
    }

   private JPanel createControlPanel()
    {
            // Makes the buttons
                    jgridpanel = new JPanel();
                    jgridpanel.setLayout(new GridLayout(4, 4));
                    jgridpanel.add(one = new JButton("0"));
                    jgridpanel.add(two = new JButton("0"));
                    jgridpanel.add(three = new JButton("0"));
                    jgridpanel.add(four = new JButton("0"));
                    jgridpanel.add(five = new JButton("0"));
                    jgridpanel.add(six = new JButton("0"));
                    jgridpanel.add(seven = new JButton("0"));
                    jgridpanel.add(eight = new JButton("0"));
                    jgridpanel.add(nine = new JButton("0"));
                    jgridpanel.add(ten = new JButton("0"));
                    jgridpanel.add(eleven = new JButton("0"));
                    jgridpanel.add(twelve = new JButton("0"));
                    jgridpanel.add(thirteen = new JButton("0"));
                    jgridpanel.add(fourteen = new JButton("0"));
                    jgridpanel.add(fifteen = new JButton("0"));
                jgridpanel.add(sixteen = new JButton("0"));

               return  jgridpanel;  <===============this is the button panel




    }

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question