我已經學習了很多次如何在 Javax swing 中添加影像。我使用了容器和所有影像圖示,但問題是我的照片太大而無法放入。它們的大小超過 1800*1700 像素,螢屏顯然無法容納它們。我會要求你們提出一種縮小影像的方法。僅供參考,我已經嘗試了大多數這些方法,例如使用 awt Image、setBounds、SCALE_DEFAULT 等。出于某種原因,我的 IDE,BlueJ 似乎不適用于很多建議的方法,但我仍然希望您能幫助我。這是代碼:
import javax.swing.*;
import java.awt.*;
class JLabelDemo
{
public JLabelDemo()
{
JFrame jfrm=new JFrame("Image from book");
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(jfrm.EXIT_ON_CLOSE);
jfrm.setSize(500,700);
ImageIcon ii=new ImageIcon("D:\\Dhruv Rana\\UNATIS Mandarmoni Trip\\Sunrise.jpg");
JLabel jl=new JLabel("Background",ii,JLabel.CENTER);
jfrm.add(jl);
jfrm.setVisible(true);
}
public static void main(String [] args)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
new JLabelDemo();
}
}
);
}
}
uj5u.com熱心網友回復:
我想這取決于您是否要保持加載到 JLabel 中的影像的縱橫比。
在任何情況下,下面是一個可運行的檔案,它考慮了縱橫比,以便影像在顯示時看起來不會失真(拉長等)。JFrame 視窗會根據影像縱橫比自動調整大小,以便根據要顯示的影像以縱向或橫向保持所需的表單大小。如果影像是縱向格式,那么 JFrame 視窗也是如此。如果影像是橫向格式,那么 JFrame 視窗也是如此。
兩個感興趣的方法是resizeImageIcon()和getAspectRatioDimension()方法。請務必閱讀代碼中的注釋。
這是代碼:
import java.awt.*;
import javax.swing.*;
public class JLabelDemo {
public JLabelDemo() {
int figNum = 1; // Image figure number (optional)
// Load in the Image
ImageIcon ii = new ImageIcon("D:\\Dhruv Rana\\UNATIS Mandarmoni Trip\\Sunrise.jpg");
/* Get the Aspect Ratio of the image in relation to the
desired size of JFrame form (500 x 700) */
java.awt.Dimension aspectDimensions = getAspectRatioDimension(
ii.getIconWidth(), ii.getIconHeight(), 500, 700);
// Create the Window
JFrame jfrm = new JFrame("Image from book");
jfrm.setLayout(new FlowLayout());
jfrm.setAlwaysOnTop(true); // Form will always be on top of other wondows.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Set the form's size in accordance to the Aspect Ratio.
40 is added to the hieght so as to accommodate for the
JLabel text. */
jfrm.setSize(aspectDimensions.width, aspectDimensions.height 40);
jfrm.setResizable(false); // Make form non-sizable
/* Resise the image in accordance to the Aspect Ratio
less 30 so as not to hug the edge of Window. */
ii = resizeImageIcon(ii, aspectDimensions.width - 30, aspectDimensions.height - 30);
// Create the JLabel and apply the image
JLabel jl = new JLabel("", ii, JLabel.CENTER);
// Un-comment if you want a border on the JLabel.
//jl.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
// Set the JLabel text below the Image and at center.
jl.setVerticalTextPosition(JLabel.BOTTOM);
jl.setHorizontalTextPosition(JLabel.CENTER);
// Provide the JLabel text (basic HTML is used here to add a little pizaze)
jl.setText("<html><font size='2' color=gray><i>Figure " figNum ": Background</i></font></html>");
jfrm.add(jl);
jfrm.pack();
jfrm.setLocationRelativeTo(null); // Set window to center Screen.
jfrm.setVisible(true); // Display Window
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JLabelDemo();
}
});
}
public static javax.swing.ImageIcon resizeImageIcon(javax.swing.ImageIcon icon, int resizedWidth, int resizedHeight) {
java.awt.Image img = icon.getImage();
java.awt.Image resizedImage = img.getScaledInstance(resizedWidth, resizedHeight, java.awt.Image.SCALE_SMOOTH);
return new javax.swing.ImageIcon(resizedImage);
}
/**
* This method will retrieve the required dimensions in order to maintain
* the aspect ratio between the original dimensions and the desired
* dimensions.<br><br>
* <p>
* <b>Example Usage:</b><pre>
*
* Dimension dim = getAspectRatioDimension(500, 300, 200, 200);
* System.out.println("Width = " dim.width " --- Height = " dim.height);
* // Output will be: Width = 200 --- Height = 120</pre>
*
* @param currentWidth (Integer) The width of current dimension to convert
* from.
* @param currentHeight (Integer) The height of current dimension to convert
* from.
* @param desiredWidth (Integer) The width we want to convert to.
* @param desiredHeight (Integer) The height we want to convert to.
*
* @return (Dimension) The actual width & height dimensions we need to
* maintain the Aspect Ratio from the original current dimensions.
*/
public java.awt.Dimension getAspectRatioDimension(final int currentWidth,
final int currentHeight, final int desiredWidth, final int desiredHeight) {
int original_width = currentWidth;
int original_height = currentHeight;
int bound_width = desiredWidth;
int bound_height = desiredHeight;
int new_width = original_width;
int new_height = original_height;
// first check if we need to scale width
if (original_width > bound_width) {
//scale width to fit
new_width = bound_width;
//scale height to maintain aspect ratio
new_height = (new_width * original_height) / original_width;
}
// then check if we need to scale even with the new height
if (new_height > bound_height) {
//scale height to fit instead
new_height = bound_height;
//scale width to maintain aspect ratio
new_width = (new_height * original_width) / original_height;
}
return new java.awt.Dimension(new_width, new_height);
}
}
uj5u.com熱心網友回復:
把它放在一個緩沖影像中
Graphics2D grph = image.createGraphics();
grph.scale(2.0, 2.0);
grph.dispose();
或查看如何縮放 BufferedImage
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/480517.html
上一篇:設定影像背景java
