gpt4 book ai didi

java - 理解 JViewPorts 时遇到一些困难

转载 作者:行者123 更新时间:2023-11-30 04:25:12 25 4
gpt4 key购买 nike

我一直在尝试了解 JViewports 并尝试使用它。我一直在用这位先生的class ,它只是扩展了 JViewport 类。

如果我想在实现 JPanel 类的 6480*4860 对象中间设置视口(viewport),为什么这段代码会给我一个空白 View ,即使在调试时,我已经确认实现 JPanel 的类是我using 已添加到 View 中?我是否完全混淆了对不同方法的调用或对所使用的坐标及其含义的理解?还有一件事:是否打电话

    v.setOpaque(true);

仅将视口(viewport)设置为不透明,还是也会将其所有子视口(viewport)设置为不透明?我想如果我弄清楚如何正确使用视口(viewport),我很快就会知道这个问题的答案。

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,但我认为这应该足够简单,尽管如果你要求的话我可以快速拼凑出一些例子。

最佳答案

因为 GrabbableViewport 扩展了 JViewport,我猜测您应该在 JScrollPane 中使用它,如图 here 。如果没有,该示例可以帮助您构建 sscce使用ScrollPanePaint.MyPanel。

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

另请参阅相关的 example利用现有的 JScrollPane 操作。

使用setOpaque(true) promise 绘制每个像素,如here 中所述。 .

关于java - 理解 JViewPorts 时遇到一些困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16089620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com