/* * OracleUtils.java * * Created on January 28, 2003, 9:17 AM */ package project2; /** Utility classes in support of the Oracle/Omens project. * Most of the code was originally elsewhere. */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; public class OracleUtils { /** For this test method to run, you need to comment back in the * initialization of lt. */ public static void testit (String [] args) { PublicOracle lt = null; // lt= new MallLuckTester()*/; //COMMENT BACK IN //Change to the name of your own lucktester. PublicInfoPanel panel = new PublicInfoPanel(lt); JFrame frame = new JFrame("panel test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new FlowLayout()); frame.getContentPane().add(panel); frame.getContentPane().add(new PublicInfoPanel(lt)); frame.getContentPane().add(new PublicInfoPanel(lt)); frame.setSize(OracleMall.omallDefaultSize); //frame.pack(); frame.show(); } } /** A panel to display the items of a IPublicInformation object. */ class PublicInfoPanel extends JPanel { /** The object from which this panel gets the information. */ IPublicInformation info; private JTextArea description; private JTextArea instructions; private JPanel resultOmen; //private ImageIcon icon = null; private static final int panelWidth = OracleMall.omallDefaultWidth/3; private static final int panelHeight = OracleMall.omallDefaultHeight/2; final int maxIconWidth = panelWidth/2; final int maxIconHeight = panelHeight/4; private JLabel iconArea; private ImageIcon icon; //will be placed in iconArea only if loaded successfully private boolean badImage = false; //set true if image can't be loaded public PublicInfoPanel(final IPublicInformation infoObject) { super(); assert infoObject != null: "null argument to PublicInfoPanel constructor"; this.info = infoObject; this.setLayout(new FlowLayout()); //default anyway this.setMaximumSize(new Dimension(panelWidth+50, panelHeight+50)); //this.setPreferredSize(new Dimension(panelWidth-30, panelHeight)); String labelinfo = this.info.getObjectName() + " by " + this.info.getAuthor(); //this.add(new JLabel(labelinfo)); Border panelBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.pink, Color.magenta); Border titledBorder = new javax.swing.border.TitledBorder( panelBorder, labelinfo); //this.setBorder(panelBorder); this.setBorder(titledBorder); { String imageURL = infoObject.getImageURLString(); if (imageURL == null) { this.iconArea = new JLabel("[No image available]"); } else { this.iconArea = new JLabel("Attempting to load " + imageURL); java.net.URL url; try { url = new java.net.URL(imageURL); icon = new ImageIcon(url); //no malformed error -- start loading it } catch (Exception e) { System.out.println("\t" + imageURL + " is not a URL string."); //not a valid URL -- try is as just a file name icon = new ImageIcon(imageURL); //start loading it icon.setDescription("Image from " + imageURL); } //this.iconArea = new JLabel(); //this.iconArea.setMaximumSize(new Dimension(maxIconWidth, maxIconHeight)); //this.iconArea.setVisible(false); } assert this.iconArea != null; this.add(this.iconArea); } this.description = new JTextArea("", 5, 20); this.description.setLineWrap(true); //needed (at least when scrollbars weren't showing up) this.description.setWrapStyleWord(true); String descriptionText = this.info.getObjectDescription(); if (descriptionText == null || descriptionText.length() <= 0) { descriptionText = "No description available."; } else { descriptionText = "Description: " + descriptionText; } this.description.setText(descriptionText); //this.description.setPreferredSize(new Dimension(panelWidth-50, // panelHeight/3)); this.description.setFocusable(false); this.add(new JScrollPane(this.description)); String instructionsText = this.info.getInstructions(); if (instructionsText == null || instructionsText.length() <= 0) { instructionsText = "No instructions available."; } else { instructionsText = "Instructions: " + instructionsText; } this.instructions = new JTextArea(instructionsText, 5, 20); this.instructions.setLineWrap(true); this.instructions.setWrapStyleWord(true); this.instructions.setFocusable(false); this.instructions.setBackground(Color.white); this.add(new JScrollPane(this.instructions)); Color preferredColor = this.info.getColor(); if (preferredColor == null) { preferredColor = Color.white; } this.setBackground(preferredColor); resizeIcon(maxIconWidth, maxIconHeight); //end constructor } /** If an image has been loaded, resize it to fit. */ public void resizeIcon(int maxWidth, int maxHeight) { if (this.badImage) return; if (this.icon == null) return; int imageStatus = icon.getImageLoadStatus(); if (imageStatus != java.awt.MediaTracker.COMPLETE ) { if (imageStatus == java.awt.MediaTracker.LOADING) { this.iconArea.setText("loading image..."); } else { this.iconArea.setText("image error status =" + imageStatus); this.badImage = true; } return; } Image oldImage = icon.getImage(); assert oldImage != null; //The image has been loaded. Rescale it to fit. double iheight = icon.getIconHeight(); double iwidth = icon.getIconWidth(); if (iheight > maxHeight || iwidth > maxWidth) { double aRatio = iwidth/iheight; double scaledWidth = (int) (Math.min(maxWidth, maxHeight*aRatio)); double scaledHeight = Math.min(maxHeight, scaledWidth/aRatio); assert Math.abs(aRatio - scaledWidth/scaledHeight) <= 0.001; // - 1 for height in the following means "preserve aspect ratio" Image scaledImage = oldImage.getScaledInstance( (int) scaledWidth, (int) scaledHeight, 0); icon.setImage(scaledImage); } this.remove(this.iconArea); //remove the old label this.iconArea = new JLabel(icon); //this.iconArea.setPreferredSize(new Dimension(maxWidth, maxHeight)); this.add(this.iconArea); ((JComponent)iconArea.getParent()).revalidate(); //end resize image } public void repaint() { if (this.iconArea != null) { resizeIcon(maxIconWidth, maxIconHeight); } super.repaint(); } //end PublicInfoPanel } /** Miscellaneous static methods involving Swing and awt components. * This should be a public class in some other package. */ class SwingUtils { //end class }