Results 1 to 4 of 4
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default JPanel and Graphics zoom.

    I have a print preview on one of my panels and it is basically a JPanel with the paintComponent method overridden. It basically draws a print preview. I would like to be able to zoom in and out. Almost as if there is a camera looking at a 2d plane. The reason for this is for different resolution, I dont want to have to explain the layout for different resolutions. If it was more like a camera view of a predefined grid it would be better.

    What types of keywords, classes, and methods should i look up and study?
    Last edited by AcousticBruce; 02-18-2011 at 09:48 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I have a print preview on one of my panels and it is basically a JPanel with the paintComponent method overridden.
    paintComponent() takes a Graphics argument that can be cast to a Graphics2D. From that link: "All coordinates passed to a Graphics2D object are specified in a device-independent coordinate system called User Space, which is used by applications. The Graphics2D object contains an AffineTransform object as part of its rendering state that defines how to convert coordinates from user space to device-dependent coordinates in Device Space."

    The idea is that by setting the Graphics2D transform you shift, enlarge and rotate what is drawn however you like.

    (All this does is render your component at size given size and orientation - ie it makes no attempt to work with page size, orientation and margins etc that users typically associate with a print preview.)

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    i was wondering why
    Java Code:
    public void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2d = (Graphics2D) g;

    instead of

    Java Code:
    public void paintComponent(Graphics2D g) {
    I understand the polymorphism relationship (or at least I think), mainly I just wonder why you cant call a Graphics2D object instead of Graphics?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Graphics2D was introduced with Java 1.2 and the Component class already had a paintComponent(Graphics) method that would be called whenever there was painting to be done. They couldn't change how that method was declared without breaking code that had already been written.

    But what they could do is ensure that when the method was called it would actually be a reference to an instance of Graphics2D that was passed as the argument. Hence you can always cast (to keep the compiler happy) and use the zoom etc methods. Somewhere or other (I don't know where) there will be documentation to the effect that "this cast won't result in a runtime ClassCastException, we promise."

    You could define your own method with a different declared argument type as you suggest - the only problem is that this one will not be used when Swing determines that a component needs to be repainted: the paintComponent(Graphics) one will be.

Similar Threads

  1. Can't display my graphics in a JPanel
    By handro1104 in forum AWT / Swing
    Replies: 2
    Last Post: 02-14-2011, 04:40 PM
  2. Having difficulty adding graphics to a JPanel
    By DrKilljoy in forum New To Java
    Replies: 20
    Last Post: 07-20-2010, 09:40 PM
  3. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 02:14 AM
  4. Clear Graphics Objects from Jpanel
    By DavidG24 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2009, 10:34 PM
  5. Newbie need help on JPanel graphics
    By junpogi in forum AWT / Swing
    Replies: 7
    Last Post: 10-21-2008, 08:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •