2013-04-18 3 views
1

JViewport를 이해하려고 노력 중이고 JViewport를 사용하려고했습니다. 나는이 신사의 class을 사용하여 JViewport 클래스를 확장했습니다.JViewPorts를 이해하는 데 약간의 어려움이 있습니다.

JPanel 클래스를 구현하는 6480 * 4860 객체의 중간에 뷰포트를 설정하려는 경우 디버깅 할 때 JPanel I을 구현하는 클래스가 사용이보기에 추가 되었습니까? 나는 다른 방법들에 대한 나의 부름에서 또는 사용 된 좌표와 그들의 의미에 대한 나의 이해에서 완전하게 혼합되어 있는가? 그리고 하나 더 : 이

v.setOpaque(true); 

를 호출 하는가는 불투명에만 뷰포트를 설정하거나 너무 모든 자식을 설정합니다? 뷰포트를 올바르게 사용하는 방법을 알면이 답변을 빨리 알게 될 것입니다.

public myProgram(){ 
    ... 

    myCustomJPanel = new MyCustonJPanel(); 
    myCustomJPanel.setBackground(Color.WHITE); 
    GrabbableViewport v = new GrabbableViewport(); 
    v.setViewSize(new Dimension(720,540)); 
    v.setViewPosition(new Point(720,540)); 
    v.setView(myCustomJPanel); 
    v.setViewPosition(new Point(720,540)); 
    v.setLocation(43, 5); 
    appropriateParentPanel.add(v); 
} 

다음은 내가 사용하려고하는 클래스입니다. 이 짧은 질문에는 훨씬 더 많은 공간이 필요하지만 형식이 지정된 텍스트는 평이한 텍스트 이상의 것을 제공해야합니다! 착각하지 않는다면 문제는 여기에서 오지 말아야합니다.

// Copyright (c) 2006 - 2008, Markus Strauch. 
// All rights reserved. 
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met: 
// 
// * Redistributions of source code must retain the above copyright notice, 
// this list of conditions and the following disclaimer. 
// * Redistributions in binary form must reproduce the above copyright notice, 
// this list of conditions and the following disclaimer in the documentation 
// and/or other materials provided with the distribution. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE. 

package net.sf.sdedit.ui.components; 

import java.awt.Component; 
import java.awt.Cursor; 
import java.awt.Image; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
import java.awt.event.MouseEvent; 

import javax.swing.ImageIcon; 
import javax.swing.JComponent; 
import javax.swing.JViewport; 
import javax.swing.event.MouseInputListener; 

/** 
* A <tt>GrabbableViewport</tt> is a <tt>JViewport</tt> that scrolls its 
* view when the mouse is dragged. While the mouse is being dragged, its cursor 
* is set to a &quot;grabbing hand&quot;, like in applications such as Acrobat 
* Reader. 
* 
* @author Markus Strauch 
* 
*/ 
public class GrabbableViewport extends JViewport implements MouseInputListener { 

    private static Cursor HAND = new Cursor(Cursor.MOVE_CURSOR); 

    private static Cursor DFLT = new Cursor(Cursor.DEFAULT_CURSOR); 

    public static void setHandCursorIcon(ImageIcon icon) { 
     Image grabbingHand = icon.getImage(); 
     HAND = Toolkit.getDefaultToolkit().createCustomCursor(grabbingHand, 
       new Point(0, 0), "hand cursor"); 
    } 

    /** 
    * Constructor. 
    */ 
    public GrabbableViewport() { 
     super(); 
    } 

    private Rectangle rect; 

    private Point point;  

    private JComponent view;  

    public void setView(Component view) { 
     super.setView(view); 
     if (this.view != view) { 
      if (this.view != null) { 
       this.view.removeMouseListener(this); 
       this.view.removeMouseMotionListener(this); 
      } 
      if (view != null) { 
       view.addMouseListener(this); 
       view.addMouseMotionListener(this); 
      } 
      this.view = (JComponent) view; 
     } 
    } 

    public void mouseClicked(MouseEvent e) { 
    } 

    public void mouseEntered(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 
    } 

    public void mousePressed(MouseEvent e) { 
     view.setCursor(HAND); 
     //((Component) e.getSource()).setCursor(HAND); 
     rect = getViewRect(); 
     point = screenLocation(e); 
    } 

    private Point screenLocation(MouseEvent e) { 
     Point root = view.getLocationOnScreen(); 
     Point mouse = e.getPoint(); 
     if (rect != null && !rect.contains(mouse)) { 
      return null; 
     } 
     Point screenPoint = new Point(root.x + mouse.x, root.y + mouse.y); 
     return screenPoint; 
    } 

    public void mouseReleased(MouseEvent e) { 
     view.setCursor(DFLT); 
     scrollTo(screenLocation(e)); 
     clear(); 
    } 

    public void mouseDragged(MouseEvent e) { 
     scrollTo(screenLocation(e)); 
    } 

    public void mouseMoved(MouseEvent e) { 
    } 

    private void scrollTo(Point newPoint) { 
     if (point != null && newPoint != null && rect != null) { 
      int deltaX = point.x - newPoint.x; 
      int deltaY = point.y - newPoint.y; 
      rect.x = rect.x + deltaX; 
      rect.y = rect.y + deltaY; 
      ((JComponent) getView()).scrollRectToVisible(rect); 
       point = newPoint; 
     } 
    } 

      private void clear() { 
       rect = null; 
      point = null; 
     } 
    } 

내가 당신에게 sscce을 줄 수 있지만, 난 당신이 묻는다면 나는 빠르게 몇 가지 예를 조각 같이 수 있지만이 충분히 간단해야한다고 생각합니다.

+0

1. @kleopatra [about similair issue]에 대한 답변보기 (http://stackoverflow.com/a/15877630/714968), 2. 기타 게시물 및 [SSCCE] (http://sscce.org/), 짧고, 실행 가능하고, 컴파일 가능하며, 문제 3에 불과하다. GrabbableViewport는 JPanel을 JScrollPane 또는 JPanels 좌표로 중심을 맞추는 것과 아무런 관련이 없기 때문에 GrabbableViewport를 사용하지 않는다. JViewport도 JComponent 또는 Container가 아니다. 대신에 JLayer (Java7)를 사용하십시오. 5. viewPort.setScrollMode가 잘못되었습니다. JViewport를 컨테이너로 사용하도록 제안하지 마십시오. [아마도 도움이 될 것입니다] (http://stackoverflow.com/q/8614972/714968) , GlassPane을 사용하십시오 – mKorbel

+0

고마워요 음 ... 그 대답은 이해하기 어렵습니다. 나는 trashgod의 해결책을 시도해보고 작동하지 않으면 다시 돌아올 것입니다. – gabarise

+1

@mKorbel : gabarise는'JScrollPane'에서'GrabbableViewport'를'JViewport'로 사용하여 성공을보고합니다. 나는'JViewport'가'Container'뿐만 아니라'JComponent'라고 생각합니다. 내가 오해하면 미안해. – trashgod

답변

3

GrabbableViewport extends JViewport 때문에, 나는 here을 같이 당신이하는 JScrollPane에서 사용하는 거 야을 추측에는 요. 그렇지 않다면 을 사용하여 sscce을 구성하는 데 도움이 될 수 있습니다.

myCustomJPanel = new MyCustonJPanel(); 
GrabbableViewport v = new GrabbableViewport(); 
v.setView(myCustomJPanel); 
JScrollPane scrollPane = new JScrollPane(); 
scrollPane.setViewport(v); 
enclosingContainer.add(scrollPane); 

또한 기존 JScrollPane 조치를 활용이 관련 example를 참조하십시오.

setOpaque(true)을 사용하면 here과 같이 모든 픽셀을 페인트 할 수 있습니다.

+0

두 답장을 보내 주셔서 감사합니다. – gabarise

+0

예. – gabarise

+0

감사합니다. 잘 알고 있습니다. 필자는 보통'paintComponent()'에서 이것을 처리하지만,'GrabbableViewport'는 면밀한 연구가 필요할 것입니다. – trashgod