Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on 

Muliple panels

TestFigurePanel  below creates three panels.

I want

1. Pane 2 and Panel 3 to remain blank. Only Panel 1 is to have the graphics.
2. I can close them one at a time. As it is now, when I close one, all three closes.


Could you please help me with these items?
import javax.swing.*;
import java.awt.*;

public class TestFigurePanel extends JFrame{
    public TestFigurePanel (){
        setLayout(new GridLayout(2,3,5,5));
        add(new FigurePanel(FigurePanel.LINE));
        add(new FigurePanel(FigurePanel.RECTANGLE));
        add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
        add(new FigurePanel(FigurePanel.OVAL));
        add(new FigurePanel(FigurePanel.RECTANGLE,true));
        add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
    }
    
    public static void main(String[] args){
        TestFigurePanel frame1=new TestFigurePanel();
        TestFigurePanel frame2=new TestFigurePanel();
        TestFigurePanel frame3=new TestFigurePanel();
        
        frame1.setSize(400,200);
        frame2.setSize(400,200);
        frame3.setSize(400,200);
        
        frame1.setTitle("Panel 1");
        frame2.setTitle("Panel 2");
        frame3.setTitle("Panel 3");
        
        frame1.setLocation(20,300);
        frame2.setLocation(440,300);
        frame3.setLocation(860,300);
        
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame1.setVisible(true);
        frame2.setVisible(true);
        frame3.setVisible(true);
        
    }
}

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

 public class FigurePanel extends JPanel {

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
//        super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
        }
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}

Open in new window

Java

Avatar of undefined
Last Comment
Mike Eghtebas
Avatar of evedder
evedder
Flag of Mexico image

for the first one you can set your add's inside an if and pass a parameter to your constructor, so you can control when FigurePanel draw the figures or not, something like this
//mode true= figures, mode false= no figures
  public TestFigurePanel (boolean mode){
        setLayout(new GridLayout(2,3,5,5));
       if(mode){
         add(new FigurePanel(FigurePanel.LINE));
         add(new FigurePanel(FigurePanel.RECTANGLE));
         add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
         add(new FigurePanel(FigurePanel.OVAL));
         add(new FigurePanel(FigurePanel.RECTANGLE,true));
         add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
      }
    }

TestFigurePanel frame1=new TestFigurePanel();
        TestFigurePanel frame2=new TestFigurePanel();
        TestFigurePanel frame3=new TestFigurePanel();
Avatar of evedder
evedder
Flag of Mexico image

for the second, i dont see anything wrong :S i have to check it out
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

               |             |              |
    LINE     |    Oval   |              |  
                |             |              |
------------------------------------
                |             |              |
                |  Rect     |              |        <-- panel 1
                |             |              |
------------------------------------
                |             |              |
    LINE     |             |              |  
                |             |              |
------------------------------------

All three panel are going to have some shape in them. If you could, add Line, Oval, and Rect as shown for Panel 1.

And, also add some (into few cells in) Panels 2 and 3.

This way I will exactly see how it is done. I wonder if you could do this.

Thank you,

Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

All three panel are going to have some shape in them eventually...
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

Thank you
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo