gpt4 book ai didi

java - 如何缩放图像

转载 作者:行者123 更新时间:2023-11-30 03:54:45 24 4
gpt4 key购买 nike

我正在尝试创建一个使用 slider 放大图像的程序。我已经让它与矩形和圆形一起工作,但是当我尝试添加图像时,它们会显示,但是当我滑动 slider 时,它们不会放大,它只会在屏幕上对角移动或平移,根本不会调整其大小。有人可以帮我解决这个问题吗?这是我的代码:

import javax.swing.*;


public class Parker
{
public static void main(String[] args)
{

TheWindow w = new TheWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //X wont close the window with out this line
w.setSize(375,375);
w.setVisible(true);
}

}



import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;


public class TheWindow extends JFrame
{
private JSlider slider; //declare slider
private drawRect myPanel; //declare/ create panel


public TheWindow()
{
super("Slider Example"); //make title
myPanel = new drawRect();
myPanel.setBackground(Color.cyan); //change background color

slider = new JSlider(SwingConstants.VERTICAL, 0, 315, 10);// restrains the slider from scaling square to 0-300 pixels
slider.setMajorTickSpacing(20); //will set tick marks every 10 pixels
slider.setPaintTicks(true); //this actually paints the ticks on the screen

slider.addChangeListener
(
new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
myPanel.setD(slider.getValue()); //Wherever you set the slider, it will pass that value and that will paint on the screen
}
}

);

add(slider, BorderLayout.WEST); //similar to init method, adds slider and panel to GUI
add(myPanel, BorderLayout.CENTER);


}






}




import java.awt.*;
import javax.swing.*;

public class drawRect extends JPanel
{

private int d = 20; //this determines the beginning size of the rect.

public void paintComponent(Graphics g)//paints obj on the screen
{
super.paintComponent(g); //prepares graphic object for drawing

ImageIcon i = new ImageIcon("A:\\Capture.png"); //location of Image
i.paintIcon(this, g, d, d); //paints icon on screen

int originX = getWidth() / 2; //this is subtracting half of 'd' from the center point to scale it form the center
int originY = getHeight() / 2;

int x = originX - (d / 2);
int y = originY - (d / 2);
System.out.println(x + "x" + y);

// g.fillRect(x, y, d, d); //paints rectangle on screen
//x , y, width, height
}

public void setD(int newD)
{
d = (newD >= 0 ? newD : 10); //if number is less than zero it will use 10 for diameter(compressed if statement)
repaint();

}

public Dimension getPrefferedSize()
{

return new Dimension(200, 200);
}

public Dimension getMinimumSize()
{
return getPrefferedSize();
}

}

最佳答案

这是调整图像大小的方法。将其放入您的代码中。

public BufferedImage resizeImage(Image originalImage, int newD) throws IOException {
BufferedImage resizedImage = new BufferedImage(newD, newD,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newD, newD, null);
g.dispose();
return resizedImage;
}
<小时/>

--编辑--

优先考虑组合而不是继承

除非您要覆盖现有逻辑,否则不要扩展任何类。

完整的示例代码:

class drawRect {

private Image image;
private JLabel label;

public JLabel getLabel() {
return label;
}

public drawRect() {
try {
label = new JLabel();
image = ImageIO.read(new File("resources/1.png"));
} catch (IOException e) {
e.printStackTrace();
}
setD(10);
}

public void setD(int newD) {
int d = (newD >= 0 ? newD : 10);

try {
label.setIcon(new ImageIcon(resizeImage(image, d)));
} catch (IOException e) {
e.printStackTrace();
}
}

private BufferedImage resizeImage(Image originalImage, int newD) throws IOException {
BufferedImage resizedImage = new BufferedImage(newD, newD, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newD, newD, label);
g.dispose();
return resizedImage;
}

}

关于java - 如何缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505305/

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