Advertisement
Guest User

ResizeFrame SSCCE

a guest
Dec 28th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. /* This is the MainClass with the main() method.
  6.    This is just where the program runs.
  7. */
  8. public class MainClass
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         ResizeFrame gui = new ResizeFrame();
  13.         gui.setVisible(true);
  14.     }
  15. }
  16.  
  17. /* This is the ResizeFrame class. This is the class
  18.    that is the main GUI for my application.
  19.    I am trying to get its height and width to be
  20.    repainted in the debugFrame object of this class
  21.    as the user drags from the bottom-right.
  22. */
  23. class ResizeFrame extends JFrame implements ActionListener, MouseMotionListener, ComponentListener
  24. {
  25.     private JFrame debugFrame = new JFrame("ResizeFrame Debug"); // The JFrame that holds the JPanel that I call repaint() for
  26.     private CanvasPanel canvas = null; // This is the JPanel that repaints the frame information
  27.    
  28.     public ResizeFrame()
  29.     {
  30.         setTitle("ResizeFrame SSCCE");
  31.         setLayout(new BorderLayout());
  32.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.         setBounds(100, 100, 300, 300);
  34.  
  35.         JButton btnDebug = new JButton("Show Debug"); // Click this button to show the debugFrame
  36.         btnDebug.addActionListener(this); // This is what shows the debugFrame
  37.         add(new JLabel("Click to show debug window"), BorderLayout.NORTH);
  38.         add(btnDebug, BorderLayout.CENTER);
  39.        
  40.         debugFrame.setDefaultCloseOperation(HIDE_ON_CLOSE); // The debugFrame isn't shown yet. It also just hides when closed
  41.         debugFrame.setLayout(new BorderLayout());
  42.         debugFrame.setBounds(450, 50, 300, 300);
  43.        
  44.         canvas = new CanvasPanel(this, debugFrame); // Constructing the CanvasPanel with parameters this and debugFrame
  45.        
  46.         debugFrame.add(canvas, BorderLayout.CENTER);
  47.         addMouseMotionListener(this); // Adding the mouse motion listener to the ResizeFrame
  48.         addComponentListener(this); // Adding the component listener to the ResizeFrame
  49.         debugFrame.addMouseMotionListener(this); // Adding the mouse motion listener to the debugFrame
  50.         debugFrame.addComponentListener(this); // Adding the component listener to the debugFrame
  51.     }
  52.    
  53.     public void actionPerformed (ActionEvent e ) // Overriding this method so the debugFrame can be shown
  54.     {
  55.         String command = e.getActionCommand();
  56.        
  57.         if (command.equals("Show Debug") && !debugFrame.isVisible())
  58.             debugFrame.setVisible(true);
  59.     }
  60.  
  61.     public void mouseMoved(MouseEvent e) // Nothing needed here yet
  62.     {
  63.     }
  64.  
  65.     /* This is the overridden method that SHOULD allow the height and width
  66.        to be repainted in real-time. It does not do this when the bottom-right
  67.        corner is dragged, but it does work when the top-left is dragged.
  68.        This is the main problem of my question.
  69.     */
  70.     public void mouseDragged(MouseEvent e)
  71.     {
  72.         validate();
  73.         canvas.repaint();
  74.     }
  75.    
  76.     /* I added these in as a last minute check, just to show that
  77.        even with both listeners in the ResizeFrame class, that it
  78.        doesn't repaint in real time when dragged from the bottom.
  79.     */
  80.     public void componentHidden(ComponentEvent e)
  81.     {
  82.         validate();
  83.         debugFrame.validate();
  84.         repaint();
  85.     }
  86.     public void componentMoved(ComponentEvent e)
  87.     {
  88.         validate();
  89.         debugFrame.validate();
  90.         repaint();
  91.     }
  92.     public void componentShown(ComponentEvent e)
  93.     {
  94.         validate();
  95.         debugFrame.validate();
  96.         repaint();
  97.     }
  98.     public void componentResized(ComponentEvent e)
  99.     {
  100.         validate();
  101.         debugFrame.validate();
  102.         repaint();
  103.     }
  104. }
  105.  
  106. /* This is the class that actually has the painted JPanel
  107.    I also have ComponentListener and MouseMotionListener methods here.
  108. */
  109. class CanvasPanel extends JPanel implements ComponentListener, MouseMotionListener
  110. {
  111.     ResizeFrame frame = null; // Using instances of these JFrames so I can retrieve their data
  112.     JFrame debug = null;
  113.    
  114.     public CanvasPanel(ResizeFrame frame, JFrame debug) // Simple construction of the CanvasPanel object
  115.     {
  116.         this.frame = frame;
  117.         this.frame.addComponentListener(this);
  118.         this.frame.addMouseMotionListener(this); // I have added both listeners to the ResizeFrame here, on top of the listener it already has
  119.        
  120.         this.debug = debug;
  121.         debug.addComponentListener(this);
  122.         debug.addMouseMotionListener(this); // I also added both listeners to the debug frame
  123.     }
  124.    
  125.     /* This is where all the data is painted on the CanvasPanel.
  126.        What I want to happen is the data gets repainted as the user
  127.        drags the mouse to resize the JFrame. You can try and
  128.        resize both frames and observe what happens.
  129.     */
  130.     public void paintComponent(Graphics g)
  131.     {
  132.         super.paintComponent(g);      
  133.        
  134.         g.drawRect(5,5,135,65);
  135.         g.drawString("Main window:",10,20);
  136.         g.drawString("Width: " + Integer.toString(frame.getWidth()),20,35);
  137.         g.drawString("Height: " + Integer.toString(frame.getHeight()),20,50);
  138.         g.drawString("Position: (" + Integer.toString(frame.getX()) + ", " + Integer.toString(frame.getY()) + ")",20,65);
  139.        
  140.         g.drawRect(5,75,135,65);
  141.         g.drawString("Debug window: ",10,90);
  142.         g.drawString("Width: " + Integer.toString(debug.getWidth()),20,105);
  143.         g.drawString("Height: " + Integer.toString(debug.getHeight()),20,120);
  144.         g.drawString("Position: (" + Integer.toString(debug.getX()) + ", " + Integer.toString(debug.getY()) + ")",20,135);
  145.     }
  146.    
  147.     /*
  148.         Overriding MouseMotionListner methods
  149.     */
  150.     public void mouseMoved(MouseEvent e)
  151.     {
  152.     }
  153.  
  154.     public void mouseDragged(MouseEvent e)
  155.     {
  156.         frame.validate();
  157.         debug.validate();
  158.         repaint();
  159.     }
  160.    
  161.     /*
  162.         Overriding all ComponentListener methods to call repaint()
  163.     */
  164.     public void componentHidden(ComponentEvent e)
  165.     {
  166.         frame.validate();
  167.         debug.validate();
  168.         repaint();
  169.     }
  170.     public void componentMoved(ComponentEvent e)
  171.     {
  172.         frame.validate();
  173.         debug.validate();
  174.         repaint();
  175.     }
  176.     public void componentShown(ComponentEvent e)
  177.     {
  178.         frame.validate();
  179.         debug.validate();
  180.         repaint();
  181.     }
  182.     public void componentResized(ComponentEvent e)
  183.     {
  184.         frame.validate();
  185.         debug.validate();
  186.         repaint();
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement