Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /* This is the MainClass with the main() method.
- This is just where the program runs.
- */
- public class MainClass
- {
- public static void main(String[] args)
- {
- ResizeFrame gui = new ResizeFrame();
- gui.setVisible(true);
- }
- }
- /* This is the ResizeFrame class. This is the class
- that is the main GUI for my application.
- I am trying to get its height and width to be
- repainted in the debugFrame object of this class
- as the user drags from the bottom-right.
- */
- class ResizeFrame extends JFrame implements ActionListener, MouseMotionListener, ComponentListener
- {
- private JFrame debugFrame = new JFrame("ResizeFrame Debug"); // The JFrame that holds the JPanel that I call repaint() for
- private CanvasPanel canvas = null; // This is the JPanel that repaints the frame information
- public ResizeFrame()
- {
- setTitle("ResizeFrame SSCCE");
- setLayout(new BorderLayout());
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 300, 300);
- JButton btnDebug = new JButton("Show Debug"); // Click this button to show the debugFrame
- btnDebug.addActionListener(this); // This is what shows the debugFrame
- add(new JLabel("Click to show debug window"), BorderLayout.NORTH);
- add(btnDebug, BorderLayout.CENTER);
- debugFrame.setDefaultCloseOperation(HIDE_ON_CLOSE); // The debugFrame isn't shown yet. It also just hides when closed
- debugFrame.setLayout(new BorderLayout());
- debugFrame.setBounds(450, 50, 300, 300);
- canvas = new CanvasPanel(this, debugFrame); // Constructing the CanvasPanel with parameters this and debugFrame
- debugFrame.add(canvas, BorderLayout.CENTER);
- addMouseMotionListener(this); // Adding the mouse motion listener to the ResizeFrame
- addComponentListener(this); // Adding the component listener to the ResizeFrame
- debugFrame.addMouseMotionListener(this); // Adding the mouse motion listener to the debugFrame
- debugFrame.addComponentListener(this); // Adding the component listener to the debugFrame
- }
- public void actionPerformed (ActionEvent e ) // Overriding this method so the debugFrame can be shown
- {
- String command = e.getActionCommand();
- if (command.equals("Show Debug") && !debugFrame.isVisible())
- debugFrame.setVisible(true);
- }
- public void mouseMoved(MouseEvent e) // Nothing needed here yet
- {
- }
- /* This is the overridden method that SHOULD allow the height and width
- to be repainted in real-time. It does not do this when the bottom-right
- corner is dragged, but it does work when the top-left is dragged.
- This is the main problem of my question.
- */
- public void mouseDragged(MouseEvent e)
- {
- validate();
- canvas.repaint();
- }
- /* I added these in as a last minute check, just to show that
- even with both listeners in the ResizeFrame class, that it
- doesn't repaint in real time when dragged from the bottom.
- */
- public void componentHidden(ComponentEvent e)
- {
- validate();
- debugFrame.validate();
- repaint();
- }
- public void componentMoved(ComponentEvent e)
- {
- validate();
- debugFrame.validate();
- repaint();
- }
- public void componentShown(ComponentEvent e)
- {
- validate();
- debugFrame.validate();
- repaint();
- }
- public void componentResized(ComponentEvent e)
- {
- validate();
- debugFrame.validate();
- repaint();
- }
- }
- /* This is the class that actually has the painted JPanel
- I also have ComponentListener and MouseMotionListener methods here.
- */
- class CanvasPanel extends JPanel implements ComponentListener, MouseMotionListener
- {
- ResizeFrame frame = null; // Using instances of these JFrames so I can retrieve their data
- JFrame debug = null;
- public CanvasPanel(ResizeFrame frame, JFrame debug) // Simple construction of the CanvasPanel object
- {
- this.frame = frame;
- this.frame.addComponentListener(this);
- this.frame.addMouseMotionListener(this); // I have added both listeners to the ResizeFrame here, on top of the listener it already has
- this.debug = debug;
- debug.addComponentListener(this);
- debug.addMouseMotionListener(this); // I also added both listeners to the debug frame
- }
- /* This is where all the data is painted on the CanvasPanel.
- What I want to happen is the data gets repainted as the user
- drags the mouse to resize the JFrame. You can try and
- resize both frames and observe what happens.
- */
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- g.drawRect(5,5,135,65);
- g.drawString("Main window:",10,20);
- g.drawString("Width: " + Integer.toString(frame.getWidth()),20,35);
- g.drawString("Height: " + Integer.toString(frame.getHeight()),20,50);
- g.drawString("Position: (" + Integer.toString(frame.getX()) + ", " + Integer.toString(frame.getY()) + ")",20,65);
- g.drawRect(5,75,135,65);
- g.drawString("Debug window: ",10,90);
- g.drawString("Width: " + Integer.toString(debug.getWidth()),20,105);
- g.drawString("Height: " + Integer.toString(debug.getHeight()),20,120);
- g.drawString("Position: (" + Integer.toString(debug.getX()) + ", " + Integer.toString(debug.getY()) + ")",20,135);
- }
- /*
- Overriding MouseMotionListner methods
- */
- public void mouseMoved(MouseEvent e)
- {
- }
- public void mouseDragged(MouseEvent e)
- {
- frame.validate();
- debug.validate();
- repaint();
- }
- /*
- Overriding all ComponentListener methods to call repaint()
- */
- public void componentHidden(ComponentEvent e)
- {
- frame.validate();
- debug.validate();
- repaint();
- }
- public void componentMoved(ComponentEvent e)
- {
- frame.validate();
- debug.validate();
- repaint();
- }
- public void componentShown(ComponentEvent e)
- {
- frame.validate();
- debug.validate();
- repaint();
- }
- public void componentResized(ComponentEvent e)
- {
- frame.validate();
- debug.validate();
- repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement