JAVA设置新视口,java – 在更大的图像上移动视口; JLablel JScrollPane

这是一个非常基本的例子.它使用一个图像文件并将其放置在一个滚动窗格内(在一个圆形的方式).

从那里,它只是使用Swing Timer来随机生成点(在图像的边界内).

每次生成一个新点时,我只需使用scrollToRectVisible,传递它想要渲染的点的位置和大小.这将确保新点(和点)在滚动窗格中可见.

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Point;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JLayeredPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.Timer;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.border.LineBorder;

public class ScrollTest {

public static void main(String[] args) {

new ScrollTest();

}

private JScrollPane scrollPane;

private DesktopPane desktopPane;

public ScrollTest() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

try {

desktopPane = new DesktopPane();

scrollPane = new JScrollPane(desktopPane);

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(scrollPane);

frame.setSize(desktopPane.getPreferredSize().width / 2, desktopPane.getPreferredSize().height / 2);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

} catch (IOException exp) {

exp.printStackTrace();

}

}

});

}

public class DesktopPane extends JLayeredPane {

private List points;

public DesktopPane() throws IOException {

points = new ArrayList<>(25);

final BufferedImage img = ImageIO.read(new File("Desktop.jpg"));

final JLabel desktop = new JLabel(new ImageIcon(img));

final JPanel overlay = new JPanel() {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int xOff = desktop.getX();

int yOff = desktop.getY();

int count = 0;

FontMetrics fm = g.getFontMetrics();

int height = fm.getHeight();

for (Point p : points) {

g.setColor(Color.RED);

String text = Integer.toString(++count);

int width = fm.stringWidth(text);

int radius = Math.max(width, height) + 5;

int x = xOff + p.x - radius / 2;

int y = yOff + p.y - radius / 2;

g.fillOval(x, y, radius, radius);

g.setColor(Color.WHITE);

x += (radius - width) / 2;

y += ((radius - height) / 2) + fm.getAscent();

g.drawString(text, x, y);

}

}

};

overlay.setOpaque(false);

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.weightx = 1;

gbc.weighty = 1;

gbc.fill = GridBagConstraints.BOTH;

add(desktop, gbc);

add(overlay, gbc);

setLayer(desktop, 0);

setLayer(overlay, 5);

overlay.setBorder(new LineBorder(Color.RED));

Timer timer = new Timer(1000, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

int x = (int) Math.round(Math.random() * img.getWidth());

int y = (int) Math.round(Math.random() * img.getHeight());

points.add(new Point(x, y));

repaint();

FontMetrics fm = getFontMetrics(overlay.getFont());

int height = fm.getHeight();

String text = Integer.toString(points.size() - 1);

int width = fm.stringWidth(text);

int radius = Math.max(width, height) + 5;

scrollRectToVisible(new Rectangle(x - radius / 2, y - radius / 2, radius, radius));

}

});

timer.start();

}

}

}

现在,如果要将点显示为尽可能靠近中心,则需要额外的工作……

现在,如果你真的想玩得开心,可以将延迟设置为50-100毫秒;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值