gpt4 book ai didi

java - 如何使用 Java Swing 库实现缩放面板

转载 作者:行者123 更新时间:2023-11-30 08:07:58 24 4
gpt4 key购买 nike

我已经使用 Java Swing 库实现了 JFrame 来可视化 map 。我想在这个框架内实现缩放功能。这是我的 JFrame 的代码:

package components;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;




public class MyFrame extends JFrame {

private static final long serialVersionUID = 1L;
private Container contentPane;
public static final int startWidth = 1280;
public static final int startHeight = 800;
public static MyPanel EarthPanel;

public MyFrame(){
initComponents();
}


public void initComponents(){
contentPane = getContentPane();
this.setTitle("TLE Graphic Propagator");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(startWidth, startHeight);
this.setLocation(0,0);
this.setLayout(new BorderLayout());
EarthPanel = new MyPanel(startWidth,startHeight);
JScrollPane scroll = new JScrollPane(EarthPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
MySlider slider = new MySlider();
contentPane.add(scroll, BorderLayout.CENTER);
contentPane.add(slider, BorderLayout.SOUTH);
}
}

正如您在上面看到的,框架包含 JScrollPane 中的 JPanel 和我用于缩放功能的 JSlider。它遵循面板的代码..

package components;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class MyPanel extends JPanel{

private static final long serialVersionUID = 1L;
private BufferedImage img;
private int width, height;
private String path = "images/earth1280x800.jpg";
private static final int UPDATE_RATE = 10; //#volte al secondo


public MyPanel(int larghezzaFrame, int altezzaFrame){

width = larghezzaFrame;
height = altezzaFrame;

this.setPreferredSize(new Dimension(width, height));

img = loadImage();
}


public BufferedImage loadImage() {
BufferedImage bimg = null;
BufferedImage ret = null;
try {
bimg = ImageIO.read(new File(path));
} catch (Exception e) {
e.printStackTrace();
}
ret = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = ret.createGraphics();
g.drawImage(bimg, 0, 0, null);
g.dispose();

return ret;
}

@Override
protected void paintComponent(Graphics g) {
setOpaque(false);

Graphics2D g2d = (Graphics2D)g;
g2d.scale(MySlider.SCALE, MySlider.SCALE);
g2d.drawImage(img, 0, 0, null);
super.paintComponent(g2d);
}

}

..以及 slider 的一个:

package components;

import java.awt.Dimension;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MySlider extends JSlider implements ChangeListener{

private static final long serialVersionUID = 1L;
public static double SCALE = 1;

public MySlider(){
super(JSlider.HORIZONTAL, 100, 400, 100);
setMajorTickSpacing(50);
setMinorTickSpacing(10);
setPaintTicks(true);
setPaintLabels(true);
addChangeListener(this);
}

@Override
public void stateChanged(ChangeEvent arg0) {
int value = ((JSlider) arg0.getSource()).getValue();
SCALE = value/100.0;
MyFrame.EarthPanel.setPreferredSize(new Dimension((int)(MyFrame.startWidth * MySlider.SCALE),(int)(MyFrame.startHeight * MySlider.SCALE)));
MyFrame.EarthPanel.repaint();
}

}

要运行代码,您只需插入以下 Main 函数:

package main;

import components.MyFrame;

public class MainPersonalFrame {
public static void main(String[] args){
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}

注意:需要在源代码的同一路径中插入一个名为“earth1280x800.jpg”的图像的“images”包。

通常出现的问题是,当我使用缩放功能、向右/向左和向上/向下滚动时,有时图像不再包含在框架中。我希望滚动条自动刷新,并且图像不会从边框中出来。我该怎么做?

谢谢大家。

最佳答案

我不是 100% 确定问题是什么,但我怀疑问题是当图像更改大小时,您没有更改图像 JPanel 的首选大小。如果是这样,那么解决方案是在显示 JPanel 的图像中覆盖 getPreferredSize() 并返回与缩放图像相匹配的尺寸。

<小时/>

编辑,不,您有点这样做,但您没有重新验证或重新绘制 JScrollPane 的视口(viewport),这是您必须做的。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainPersonalFrame {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}

class MyFrame extends JFrame {

private static final long serialVersionUID = 1L;
private Container contentPane;
public static final int startWidth = 1280;
public static final int startHeight = 800;

// !! This should be private and not static
private MyPanel earthPanel;

// !! this should be a field
private JScrollPane scroll;

public MyFrame() {
initComponents();
}

public void initComponents() {
contentPane = getContentPane();
this.setTitle("TLE Graphic Propagator");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// !! avoid setting sizes if possible
this.setSize(startWidth, startHeight);
this.setLocation(0, 0);
this.setLayout(new BorderLayout());
earthPanel = new MyPanel(startWidth, startHeight);

// !! again, scroll is now a field
scroll = new JScrollPane(earthPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
MySlider slider = new MySlider(this);
contentPane.add(scroll, BorderLayout.CENTER);
contentPane.add(slider, BorderLayout.SOUTH);
}

// !! to avoid having classes directly manipulate other class's fields
public void setEarthPanelSize(Dimension size) {
earthPanel.setPreferredSize(size);
}

// !! allow other classes the ability to revalidate/repaint viewport
public void revalidateViewport() {
scroll.getViewport().revalidate();
scroll.getViewport().repaint();
}
}

class MyPanel extends JPanel {

private static final long serialVersionUID = 1L;
private BufferedImage img;
private int width, height;

// !! changes so that I can run your program
// !! with an internet image
// !! private String path = "images/earth1280x800.jpg";
private String urlPath = "http://image.desk7.net/"
+ "Space%20Wallpapers/1422_1280x800.jpg";

// !! private static final int UPDATE_RATE = 10; // !! never used

public MyPanel(int larghezzaFrame, int altezzaFrame) {
setOpaque(false); // !! this should be here
width = larghezzaFrame;
height = altezzaFrame;

this.setPreferredSize(new Dimension(width, height));

img = loadImage();
}

public BufferedImage loadImage() {
BufferedImage bimg = null;
BufferedImage ret = null;
try {
URL imgUrl = new URL(urlPath);
// !! bimg = ImageIO.read(new File(path)); // !!
bimg = ImageIO.read(imgUrl); // !! use image available to all
// !! } catch (Exception e) {
// e.printStackTrace();
} catch (IOException e) {
// !! use more specific exception
e.printStackTrace();
}
ret = new BufferedImage(bimg.getWidth(), bimg.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = ret.createGraphics();
g.drawImage(bimg, 0, 0, null);
g.dispose();

return ret;
}

@Override
protected void paintComponent(Graphics g) {
// !! this shouldn't be in paintComponent:
// !! setOpaque(false);

Graphics2D g2d = (Graphics2D) g;
g2d.scale(MySlider.SCALE, MySlider.SCALE);
g2d.drawImage(img, 0, 0, null);
super.paintComponent(g2d);
}

}

class MySlider extends JSlider implements ChangeListener {

private static final long serialVersionUID = 1L;
public static double SCALE = 1;
private MyFrame myFrame;

public MySlider(MyFrame myFrame) {
super(JSlider.HORIZONTAL, 100, 400, 100);
this.myFrame = myFrame;
setMajorTickSpacing(50);
setMinorTickSpacing(10);
setPaintTicks(true);
setPaintLabels(true);
addChangeListener(this);
}

@Override
public void stateChanged(ChangeEvent arg0) {
int value = ((JSlider) arg0.getSource()).getValue();
SCALE = value / 100.0;
int w = (int) (MyFrame.startWidth * MySlider.SCALE);
int h = (int) (MyFrame.startHeight * MySlider.SCALE);
Dimension size = new Dimension(w, h);
myFrame.setEarthPanelSize(size); // !!
myFrame.revalidateViewport(); // !!
// !! MyFrame.earthPanel.repaint(); // No, don't do this
}
}

关于java - 如何使用 Java Swing 库实现缩放面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30822483/

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