日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2023/12/4 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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毫秒;)

總結

以上是生活随笔為你收集整理的JAVA设置新视口,java – 在更大的图像上移动视口; JLablel JScrollPane的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。