{"id":10295,"date":"2015-08-26T07:10:30","date_gmt":"2015-08-26T11:10:30","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10295"},"modified":"2015-08-24T08:50:13","modified_gmt":"2015-08-24T12:50:13","slug":"a-demo-providing-multiple-buttons-to-select-a-playing-card-a-panel-using-cardlayout-control-which-of-four-possible-subpanels-holding-a-different-card-to-display","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10295","title":{"rendered":"A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display"},"content":{"rendered":"<pre>#######################\r\n# CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images:\r\n\u00a0\u00a0\u00a0 * CardPanel.java A Panel that displays a playing card.\r\n\u00a0\u00a0\u00a0 * ImageLabel.java A Canvas for displaying images.\r\n\u00a0\u00a0\u00a0 * Ace.gif, King.gif, Queen.gif, Jack.gif.\r\n#####################\r\nimport java.applet.Applet;\r\nimport java.awt.*;\r\nimport java.awt.event.*;\r\n\r\n\/** An example of CardLayout. The right side of the window holds\r\n\u00a0*\u00a0 a Panel that uses CardLayout to control four possible\r\n\u00a0*\u00a0 subpanels (each of which is a CardPanel that shows a\r\n\u00a0*\u00a0 picture of a playing card). The buttons on the left side\r\n\u00a0*\u00a0 of the window manipulate the \"cards\" in this layout by\r\n\u00a0*\u00a0 calling methods in the right-hand panel's layout manager.\r\n\u00a0*\r\n\u00a0*********************************\r\n\r\npublic class CardDemo extends Applet implements ActionListener {\r\n\u00a0 private Button first, last, previous, next;\r\n\u00a0 private String[] cardLabels = { \"Jack\",\"Queen\",\"King\",\"Ace\" };\r\n\u00a0 private CardPanel[] cardPanels = new CardPanel[4];\r\n\u00a0 private CardLayout layout;\r\n\u00a0 private Panel cardDisplayPanel;\r\n\r\n\u00a0 public void init() {\r\n\u00a0\u00a0\u00a0 setBackground(Color.white);\r\n\u00a0\u00a0\u00a0 setLayout(new BorderLayout());\r\n\u00a0\u00a0\u00a0 addButtonPanel();\r\n\u00a0\u00a0\u00a0 addCardDisplayPanel();\r\n\u00a0 }\r\n\r\n\u00a0 private void addButtonPanel() {\r\n\u00a0\u00a0\u00a0 Panel buttonPanel = new Panel();\r\n\u00a0\u00a0\u00a0 buttonPanel.setLayout(new GridLayout(9, 1));\r\n\u00a0\u00a0\u00a0 Font buttonFont = new Font(\"SansSerif\", Font.BOLD, 18);\r\n\u00a0\u00a0\u00a0 buttonPanel.setFont(buttonFont);\r\n\u00a0\u00a0\u00a0 for(int i=0; inot use CardLayout. Rather, instances of CardPanel\r\n\u00a0*\u00a0 are contained in another window used in the CardDemo\r\n\u00a0*\u00a0 example. It is this enclosing window that uses CardLayout\r\n\u00a0*\u00a0 to manipulate which CardPanel it shows.\r\n\u00a0*\r\n\u00a0*************************\r\n\r\npublic class CardPanel extends Panel {\r\n\u00a0 private Label name;\r\n\u00a0 private ImageLabel picture;\r\n\r\n\u00a0 public CardPanel(String cardName,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 URL directory, String imageFile) {\r\n\u00a0\u00a0\u00a0 setLayout(new BorderLayout());\r\n\u00a0\u00a0\u00a0 name = new Label(cardName, Label.CENTER);\r\n\u00a0\u00a0\u00a0 name.setFont(new Font(\"SanSerif\", Font.BOLD, 50));\r\n\u00a0\u00a0\u00a0 add(name, BorderLayout.NORTH);\r\n\u00a0\u00a0\u00a0 picture = new ImageLabel(directory, imageFile);\r\n\u00a0\u00a0\u00a0 Panel picturePanel = new Panel();\r\n\u00a0\u00a0\u00a0 picturePanel.add(picture);\r\n\u00a0\u00a0\u00a0 add(picturePanel, BorderLayout.CENTER);\r\n\u00a0\u00a0\u00a0 setSize(getPreferredSize());\r\n\u00a0 }\r\n\r\n\u00a0 public Label getLabel() {\r\n\u00a0\u00a0\u00a0 return(name);\r\n\u00a0 }\r\n\r\n\u00a0 public ImageLabel getImageLabel() {\r\n\u00a0\u00a0\u00a0 return(picture);\r\n\u00a0 }\r\n}\r\n*******************\r\nImageLabel.java A Canvas for displaying images.\r\n*******************\r\nimport java.awt.*;\r\nimport java.net.*; \r\n\r\n\/** A class for displaying images. It places the Image\r\n\u00a0*\u00a0 into a canvas so that it can moved around by layout\r\n\u00a0*\u00a0 managers, will get repainted automatically, etc.\r\n\u00a0*\u00a0 No mouseXXX or action events are defined, so it is\r\n\u00a0*\u00a0 most similar to the Label Component.\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\u00a0 By default, with FlowLayout the ImageLabel takes\r\n\u00a0*\u00a0 its minimum size (just enclosing the image). The\r\n\u00a0*\u00a0 default with BorderLayout is to expand to fill\r\n\u00a0*\u00a0 the region in width (North\/South), height\r\n\u00a0*\u00a0 (East\/West) or both (Center). This is the same\r\n\u00a0*\u00a0 behavior as with the builtin Label class. If you\r\n\u00a0*\u00a0 give an explicit setSize or\r\n\u00a0*\u00a0 setBounds call before adding the\r\n\u00a0*\u00a0 ImageLabel to the Container, this size will\r\n\u00a0*\u00a0 override the defaults.\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\u00a0 Here is an example of its use:\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0* \u00a0\r\n\r\n\u00a0*\u00a0 public class ShowImages extends Applet {\r\n\u00a0*\u00a0\u00a0\u00a0 private ImageLabel image1, image2;\r\n\u00a0*\r\n\u00a0*\u00a0\u00a0\u00a0 public void init() {\r\n\u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 image1 = new ImageLabel(getCodeBase(),\r\n\u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"some-image.gif\");\r\n\u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 image2 = new ImageLabel(getCodeBase(),\r\n\u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"other-image.jpg\");\r\n\u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 add(image1);\r\n\u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 add(image2);\r\n\u00a0*\u00a0\u00a0\u00a0 }\r\n\u00a0*\u00a0 }\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\r\n\u00a0 *\/\r\n\r\npublic class ImageLabel extends Canvas {\r\n\u00a0 \/\/ Instance variables.\r\n\u00a0 \r\n\u00a0 \/\/ The actual Image drawn on the canvas. \r\n\u00a0 private Image image;\r\n\r\n\u00a0 \/\/ A String corresponding to the URL of the image you will \r\n\u00a0 \/\/ get if you call the constructor with no arguments.\r\n\u00a0 private static String defaultImageString\r\n\u00a0\u00a0\u00a0 = \"http:\/\/java.sun.com\/lib\/images\/\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \"logo.java.color-transp.55x60.gif\";\r\n\r\n\u00a0 \/\/ The URL of the image. But sometimes we will use an existing\r\n\u00a0 \/\/ image object (e.g. made by createImage) for which this info \r\n\u00a0 \/\/ will not be available, so a default string is used here.\r\n\u00a0 private String imageString = \"\";\r\n\r\n\u00a0 \/\/ Turn this on to get verbose debugging messages. \r\n\u00a0 private boolean debug = false;\r\n\r\n\u00a0 \/** Amount of extra space around the image. *\/\r\n\u00a0 \r\n\u00a0 private int border = 0;\r\n\r\n\u00a0 \/** If there is a non-zero border, what color should it be? \r\n\u00a0\u00a0 *\u00a0 Default is to use the background color of the Container.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 private Color borderColor = null;\r\n\u00a0 \r\n\u00a0 \/\/ Width and height of the Canvas. This is the\r\n\u00a0 \/\/\u00a0 width\/height of the image plus twice the border.\r\n\u00a0 private int width, height;\r\n\r\n\u00a0 \/** Determines if it will be sized automatically. If the user\r\n\u00a0\u00a0 *\u00a0 issues a setSize() or setBounds()call before adding the \r\n\u00a0\u00a0 *\u00a0 label to the Container, or if the LayoutManager resizes \r\n\u00a0\u00a0 *\u00a0 before drawing (as with BorderLayout), then those sizes\r\n\u00a0\u00a0 *\u00a0 override the default, which is to make the label the same \r\n\u00a0\u00a0 *\u00a0 size as the image it holds (after reserving space for the \r\n\u00a0\u00a0 *\u00a0 border, if any). This flag notes this, so subclasses that\r\n\u00a0\u00a0 *\u00a0 override ImageLabel need to check this flag, and if it is \r\n\u00a0\u00a0 *\u00a0 true, and they draw modified image, then they need to draw \r\n\u00a0\u00a0 *\u00a0 them based on the width height variables, not just blindly \r\n\u00a0\u00a0 *\u00a0 drawing them full size.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 private boolean explicitSize = false;\r\n\u00a0 private int explicitWidth=0, explicitHeight=0;\r\n\u00a0 \r\n\u00a0 \/\/ The MediaTracker that can tell if image has been loaded \r\n\u00a0 \/\/ before trying to paint it or setSize based on its size.\r\n\u00a0 private MediaTracker tracker;\r\n\u00a0 \r\n\u00a0 \/\/ Used by MediaTracker to be sure image is loaded before \r\n\u00a0 \/\/ paint &amp; setSize, since you can't find out the size until it\r\n\u00a0 \/\/ is done loading.\r\n\u00a0 private static int lastTrackerID=0;\r\n\u00a0 private int currentTrackerID;\r\n\u00a0 private boolean doneLoading = false;\r\n\r\n\u00a0 private Container parentContainer;\r\n\r\n\u00a0 \/** Create an ImageLabel with the default image.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @see #getDefaultImageString\r\n\u00a0\u00a0 * @see #setDefaultImageString\r\n\u00a0\u00a0 *\/\r\n\u00a0 \/\/ Remember that the funny \"this()\" syntax calls\r\n\u00a0 \/\/ constructor of same class\r\n\u00a0 public ImageLabel() {\r\n\u00a0\u00a0\u00a0 this(defaultImageString);\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 \/** Create an ImageLabel using the image at URL\r\n\u00a0\u00a0 *\u00a0 specified by the string.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @param imageURLString A String specifying the\r\n\u00a0\u00a0 *\u00a0\u00a0 URL of the image.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public ImageLabel(String imageURLString) {\r\n\u00a0\u00a0\u00a0 this(makeURL(imageURLString));\r\n\u00a0 }\r\n\r\n\u00a0 \/** Create an ImageLabel using the image at URL\r\n\u00a0\u00a0 *\u00a0 specified.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @param imageURL The URL of the image.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public ImageLabel(URL imageURL) {\r\n\u00a0\u00a0\u00a0 this(loadImage(imageURL));\r\n\u00a0\u00a0\u00a0 imageString = imageURL.toExternalForm();\r\n\u00a0 }\r\n\r\n\u00a0 \/** Create an ImageLabel using the image in the file\r\n\u00a0\u00a0 *\u00a0 in the specified directory.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @param imageDirectory Directory containing image\r\n\u00a0\u00a0 * @param file Filename of image\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public ImageLabel(URL imageDirectory, String file) {\r\n\u00a0\u00a0\u00a0 this(makeURL(imageDirectory, file));\r\n\u00a0\u00a0\u00a0 imageString = file;\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 \/** Create an ImageLabel using the image specified. The other\r\n\u00a0\u00a0 *\u00a0 constructors eventually call this one, but you may want \r\n\u00a0\u00a0 *\u00a0 to call it directly if you already have an image (e.g. \r\n\u00a0\u00a0 *\u00a0 created through createImage).\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @param image The image\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public ImageLabel(Image image) {\r\n\u00a0\u00a0\u00a0 this.image = image;\r\n\u00a0\u00a0\u00a0 tracker = new MediaTracker(this);\r\n\u00a0\u00a0\u00a0 currentTrackerID = lastTrackerID++;\r\n\u00a0\u00a0\u00a0 tracker.addImage(image, currentTrackerID);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Makes sure that the Image associated with the Canvas is \r\n\u00a0\u00a0 *\u00a0 done loading before returning, since loadImage spins off\r\n\u00a0\u00a0 *\u00a0 a separate thread to do the loading. Once you get around \r\n\u00a0\u00a0 *\u00a0 to drawing the image, this will make sure it is loaded,\r\n\u00a0\u00a0 *\u00a0 waiting if not. The user does not need to call this at \r\n\u00a0\u00a0 *\u00a0 all, but if several ImageLabels are used in the same \r\n\u00a0\u00a0 *\u00a0 Container, this can cause several repeated layouts, so \r\n\u00a0\u00a0 *\u00a0 users might want to explicitly call this themselves before\r\n\u00a0\u00a0 *\u00a0 adding the ImageLabel to the Container. Another \r\n\u00a0\u00a0 *\u00a0 alternative is to start asynchronous loading by calling \r\n\u00a0\u00a0 *\u00a0 prepareImage on the ImageLabel's image (see getImage). \r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @param doLayout Determines if the Container should be \r\n\u00a0\u00a0 *\u00a0\u00a0 re-laid out after you are finished waiting. This \r\n\u00a0\u00a0 *\u00a0\u00a0 should be true when called from user functions, \r\n\u00a0\u00a0 *\u00a0\u00a0 but is set to false when called from getPreferredSize \r\n\u00a0\u00a0 *\u00a0\u00a0 to avoid an infinite loop. This is needed when using \r\n\u00a0\u00a0 *\u00a0\u00a0 BorderLayout, which calls getPreferredSize\r\n\u00a0\u00a0 *\u00a0\u00a0 before calling paint.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public void waitForImage(boolean doLayout) {\r\n\u00a0\u00a0\u00a0 if (!doneLoading) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 debug(\"[waitForImage] - Resizing and waiting for \"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + imageString);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 try { tracker.waitForID(currentTrackerID); } \r\n\u00a0\u00a0\u00a0\u00a0\u00a0 catch (InterruptedException ie) {} \r\n\u00a0\u00a0\u00a0\u00a0\u00a0 catch (Exception e) { \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Error loading \"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + imageString + \": \"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + e.getMessage()); \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 e.printStackTrace(); \r\n\u00a0\u00a0\u00a0\u00a0\u00a0 } \r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (tracker.isErrorID(0)) \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new Throwable(\"Error loading image \"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + imageString).printStackTrace();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 doneLoading = true;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (explicitWidth != 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 width = explicitWidth;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 width = image.getWidth(this) + 2*border;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (explicitHeight != 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 height = explicitHeight;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 height = image.getHeight(this) + 2*border;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 setSize(width, height);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 debug(\"[waitForImage] - \" + imageString + \" is \"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + width + \"x\" + height + \".\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ If no parent, you are OK, since it will have\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ been resized before being added. But if\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ parent exists, you have already been added,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ and the change in size requires re-layout. \r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (((parentContainer = getParent()) != null)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &amp;&amp; doLayout) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 setBackground(parentContainer.getBackground());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 parentContainer.doLayout();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** Moves the image so that it is centered at\r\n\u00a0\u00a0 *\u00a0 the specified location, as opposed to the setLocation\r\n\u00a0\u00a0 *\u00a0 method of Component which places the top left\r\n\u00a0\u00a0 *\u00a0 corner at the specified location.\r\n\u00a0\u00a0 * \u00a0\r\n\r\n\r\n\u00a0\u00a0 *\u00a0 Note: The effects of this could be undone\r\n\u00a0\u00a0 *\u00a0 by the LayoutManager of the parent Container, if\r\n\u00a0\u00a0 *\u00a0 it is using one. So this is normally only used\r\n\u00a0\u00a0 *\u00a0 in conjunction with a null LayoutManager.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @param x The X coord of center of the image\r\n\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (in parent's coordinate system)\r\n\u00a0\u00a0 * @param y The Y coord of center of the image\r\n\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (in parent's coordinate system)\r\n\u00a0\u00a0 * @see java.awt.Component#setLocation\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void centerAt(int x, int y) {\r\n\u00a0\u00a0\u00a0 debug(\"Placing center of \" + imageString + \" at (\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + x + \",\" + y + \")\");\r\n\u00a0\u00a0\u00a0 setLocation(x - width\/2, y - height\/2); \r\n\u00a0 }\r\n\r\n\u00a0 \/** Determines if the x and y (in the ImageLabel's\r\n\u00a0\u00a0 *\u00a0 own coordinate system) is inside the\r\n\u00a0\u00a0 *\u00a0 ImageLabel. Put here because Netscape 2.02 has\r\n\u00a0\u00a0 *\u00a0 a bug in which it doesn't process contains() and\r\n\u00a0\u00a0 *\u00a0 locate() tests correctly. \r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public synchronized boolean contains(int x, int y) {\r\n\u00a0\u00a0\u00a0 return((x &gt;= 0) &amp;&amp; (x &lt;= width)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &amp;&amp; (y &gt;= 0) &amp;&amp; (y &lt;= height));\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 \/** Draws the image. If you override this in a\r\n\u00a0\u00a0 *\u00a0 subclass, be sure to call super.paint.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public void paint(Graphics g) {\r\n\u00a0\u00a0\u00a0 if (!doneLoading)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 waitForImage(true);\r\n\u00a0\u00a0\u00a0 else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (explicitSize)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 g.drawImage(image, border, border,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 width-2*border, height-2*border,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 g.drawImage(image, border, border, this);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 drawRect(g, 0, 0, width-1, height-1,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 border, borderColor);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** Used by layout managers to calculate the usual\r\n\u00a0\u00a0 *\u00a0 size allocated for the Component. Since some\r\n\u00a0\u00a0 *\u00a0 layout managers (e.g. BorderLayout) may\r\n\u00a0\u00a0 *\u00a0 call this before paint is called, you need to\r\n\u00a0\u00a0 *\u00a0 make sure that the image is done loading, which\r\n\u00a0\u00a0 *\u00a0 will force a setSize, which determines the values\r\n\u00a0\u00a0 *\u00a0 returned.\r\n\u00a0\u00a0 *\/\r\n\u00a0 public Dimension getPreferredSize() {\r\n\u00a0\u00a0\u00a0 if (!doneLoading)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 waitForImage(false);\r\n\u00a0\u00a0\u00a0 return(super.getPreferredSize());\r\n\u00a0 }\r\n\r\n\u00a0 \/** Used by layout managers to calculate the smallest\r\n\u00a0\u00a0 *\u00a0 size allocated for the Component. Since some\r\n\u00a0\u00a0 *\u00a0 layout managers (e.g. BorderLayout) may\r\n\u00a0\u00a0 *\u00a0 call this before paint is called, you need to\r\n\u00a0\u00a0 *\u00a0 make sure that the image is done loading, which\r\n\u00a0\u00a0 *\u00a0 will force a setSize, which determines the values\r\n\u00a0\u00a0 *\u00a0 returned.\r\n\u00a0\u00a0 *\/\r\n\u00a0\u00a0 public Dimension getMinimumSize() {\r\n\u00a0\u00a0\u00a0\u00a0 if (!doneLoading)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 waitForImage(false);\r\n\u00a0\u00a0\u00a0\u00a0 return(super.getMinimumSize());\r\n\u00a0\u00a0 }\r\n\u00a0 \r\n\u00a0 \/\/ LayoutManagers (such as BorderLayout) might call\r\n\u00a0 \/\/ setSize or setBounds with only 1 dimension of\r\n\u00a0 \/\/ width\/height non-zero. In such a case, you still\r\n\u00a0 \/\/ want the other dimension to come from the image\r\n\u00a0 \/\/ itself.\r\n\r\n\u00a0 \/** Resizes the ImageLabel. If you don't setSize the\r\n\u00a0\u00a0 *\u00a0 label explicitly, then what happens depends on\r\n\u00a0\u00a0 *\u00a0 the layout manager. With FlowLayout, as with\r\n\u00a0\u00a0 *\u00a0 FlowLayout for Labels, the ImageLabel takes its\r\n\u00a0\u00a0 *\u00a0 minimum size, just enclosing the image. With\r\n\u00a0\u00a0 *\u00a0 BorderLayout, as with BorderLayout for Labels,\r\n\u00a0\u00a0 *\u00a0 the ImageLabel is expanded to fill the\r\n\u00a0\u00a0 *\u00a0 section. Stretching GIF\/JPG files does not always\r\n\u00a0\u00a0 *\u00a0 result in clear looking images. So just as\r\n\u00a0\u00a0 *\u00a0 with builtin Labels and Buttons, don't\r\n\u00a0\u00a0 *\u00a0 use FlowLayout if you don't want the Buttons to\r\n\u00a0\u00a0 *\u00a0 get resized. If you don't use any\r\n\u00a0\u00a0 *\u00a0 LayoutManager, then the ImageLabel will also\r\n\u00a0\u00a0 *\u00a0 just fit the image.\r\n\u00a0\u00a0 * \u00a0\r\n\r\n\r\n\u00a0\u00a0 *\u00a0 Note that if you setSize explicitly, you must do\r\n\u00a0\u00a0 *\u00a0 it before the ImageLabel is added to the\r\n\u00a0\u00a0 *\u00a0 Container. In such a case, the explicit size\r\n\u00a0\u00a0 *\u00a0 overrides the image dimensions.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @see #setBounds\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public void setSize(int width, int height) {\r\n\u00a0\u00a0\u00a0 if (!doneLoading) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 explicitSize=true;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (width &gt; 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 explicitWidth=width;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (height &gt; 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 explicitHeight=height;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 super.setSize(width, height);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Resizes the ImageLabel. If you don't setSize the\r\n\u00a0\u00a0 *\u00a0 label explicitly, then what happens depends on\r\n\u00a0\u00a0 *\u00a0 the layout manager. With FlowLayout, as with\r\n\u00a0\u00a0 *\u00a0 FlowLayout for Labels, the ImageLabel takes its\r\n\u00a0\u00a0 *\u00a0 minimum size, just enclosing the image. With\r\n\u00a0\u00a0 *\u00a0 BorderLayout, as with BorderLayout for Labels,\r\n\u00a0\u00a0 *\u00a0 the ImageLabel is expanded to fill the\r\n\u00a0\u00a0 *\u00a0 section. Stretching GIF\/JPG files does not always\r\n\u00a0\u00a0 *\u00a0 result in clear looking images. So just as\r\n\u00a0\u00a0 *\u00a0 with builtin Labels and Buttons, don't\r\n\u00a0\u00a0 *\u00a0 use FlowLayout if you don't want the Buttons to\r\n\u00a0\u00a0 *\u00a0 get resized. If you don't use any\r\n\u00a0\u00a0 *\u00a0 LayoutManager, then the ImageLabel will also\r\n\u00a0\u00a0 *\u00a0 just fit the image.\r\n\u00a0\u00a0 * \u00a0\r\n\r\n\r\n\u00a0\u00a0 *\u00a0 Note that if you setSize explicitly, you must do\r\n\u00a0\u00a0 *\u00a0 it before the ImageLabel is added to the\r\n\u00a0\u00a0 *\u00a0 Container. In such a case, the explicit size\r\n\u00a0\u00a0 *\u00a0 overrides the image dimensions.\r\n\u00a0\u00a0 *\r\n\u00a0\u00a0 * @see #setSize\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 public void setBounds(int x, int y,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int width, int height) {\r\n\u00a0\u00a0\u00a0 if (!doneLoading) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 explicitSize=true;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (width &gt; 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 explicitWidth=width;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (height &gt; 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 explicitHeight=height;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 super.setBounds(x, y, width, height);\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 \/\/ You can't just set the background color to\r\n\u00a0 \/\/ the borderColor and skip drawing the border,\r\n\u00a0 \/\/ since it messes up transparent gifs. You\r\n\u00a0 \/\/ need the background color to be the same as\r\n\u00a0 \/\/ the container.\r\n\u00a0 \r\n\u00a0 \/** Draws a rectangle with the specified OUTSIDE\r\n\u00a0\u00a0 *\u00a0 left, top, width, and height.\r\n\u00a0\u00a0 *\u00a0 Used to draw the border.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \u00a0\r\n\u00a0 protected void drawRect(Graphics g,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int left, int top,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int width, int height,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int lineThickness,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Color rectangleColor) {\r\n\u00a0\u00a0\u00a0 g.setColor(rectangleColor);\r\n\u00a0\u00a0\u00a0 for(int i=0; i\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: \u00a0\u00a0\u00a0 * CardPanel.java A Panel that displays a playing card. \u00a0\u00a0\u00a0 * ImageLabel.java A Canvas for displaying images. \u00a0\u00a0\u00a0 * Ace.gif, &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10295\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,285],"class_list":["post-10295","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":27229,"url":"http:\/\/bangla.sitestree.com\/?p=27229","url_meta":{"origin":10295,"position":0},"title":"A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 14, 2021","format":false,"excerpt":"####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: * CardPanel.java A Panel that displays a playing card. * ImageLabel.java A Canvas for displaying images.\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10282,"url":"http:\/\/bangla.sitestree.com\/?p=10282","url_meta":{"origin":10295,"position":1},"title":"Places a Panel holding 100 buttons in a ScrollPane","author":"","date":"August 26, 2015","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; \/** Places a Panel holding 100 buttons in a ScrollPane that is \u00a0*\u00a0 too small to hold it. \u00a0* \u00a0 *\/ public class ScrollPaneTest extends Applet { \u00a0 public void init() { \u00a0\u00a0\u00a0 setLayout(new BorderLayout()); \u00a0\u00a0\u00a0 ScrollPane pane = new ScrollPane(); \u00a0\u00a0\u00a0 Panel bigPanel = new\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27217,"url":"http:\/\/bangla.sitestree.com\/?p=27217","url_meta":{"origin":10295,"position":2},"title":"Places a Panel holding 100 buttons in a ScrollPane #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 14, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; \/** Places a Panel holding 100 buttons in a ScrollPane that is * too small to hold it. * *\/ public class ScrollPaneTest extends Applet { public void init() { setLayout(new BorderLayout()); ScrollPane pane = new ScrollPane(); Panel bigPanel = new Panel(); bigPanel.setLayout(new GridLayout(10, 10)); for(int\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27000,"url":"http:\/\/bangla.sitestree.com\/?p=27000","url_meta":{"origin":10295,"position":3},"title":"Eight buttons: four each in two panels #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; *************************** \/** Eight buttons: four each in two panels. * *\/ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { \"Start\", \"Stop\", \"Pause\", \"Resume\" }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + \" Thread1\"));\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10333,"url":"http:\/\/bangla.sitestree.com\/?p=10333","url_meta":{"origin":10295,"position":4},"title":"Eight buttons: four each in two panels","author":"","date":"August 26, 2015","format":false,"excerpt":"mport java.applet.Applet; import java.awt.*; *************************** \/** Eight buttons: four each in two panels. * *\/ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { \"Start\", \"Stop\", \"Pause\", \"Resume\" }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + \" Thread1\"));\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26948,"url":"http:\/\/bangla.sitestree.com\/?p=26948","url_meta":{"origin":10295,"position":5},"title":"Creates three radio buttons and illustrates handling #Programming Code Examples #Java\/J2EE\/J2ME #Basic Swing","author":"Author-Check- Article-or-Video","date":"May 6, 2021","format":false,"excerpt":"JRadioButtonTest.java Creates three radio buttons and illustrates handling ItemEvents in response to selecting a radio button. import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.*; import java.awt.event.*; import javax.swing.*; \/** *\/ public class JRadioButtonTest extends JPanel implements ItemListener { public JRadioButtonTest() { String[] labels = {\"Java Swing\",\"Java Servlets\", \"JavaServer Pages\"}; JRadioButton[] buttons =\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10295"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10295\/revisions"}],"predecessor-version":[{"id":10296,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10295\/revisions\/10296"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10295"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}