gpt4 book ai didi

java - 程序应该显示整个图像只显示它的一部分

转载 作者:行者123 更新时间:2023-12-01 08:09:24 26 4
gpt4 key购买 nike

public class Main extends JPanel{
private static final long serialVersionUID = 1L;

static BufferedImage image;
private static JPanel imagePanel;
private static JFrame frame;

private static String dir = "image\\pic.jpg";

public Main(){
try{
image = ImageIO.read(new File(dir));
}catch (IOException e){
// If it cannot read from the directory inform the client
System.out.println("Error reading from directory: " + e.getMessage());
}
}
//We set our preffered size if we succeed in loading the image
public Dimension getPrefferedSize(){
if(image == null){
return new Dimension(100, 100);
}else{
return new Dimension(image.getWidth(null), image.getHeight(null));
}
}
//Draw our image on the screen with Graphic's "drawImage()" method
public void paint(Graphics g){
g.drawImage(image, 0, 0, null);
}

public static void main(String[] args){
frame = new JFrame("Loading image from file example");
imagePanel = new JPanel();
//Release the resource window handle as we close the frame
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
image = ImageIO.read(new File(dir));
if(image.getWidth(null) != 0){
System.out.println(image.getWidth(null));
System.out.println(image.getHeight(null));
}
}catch(Exception e){
e.printStackTrace();
}
imagePanel.add(new Main());
frame.add(imagePanel);
//frame.pack();
frame.setSize(200, 300);
frame.setVisible(true);
}
/*
public static BufferedImage enlarge(BufferedImage image, int n){
int w = n * image.getWidth();
int h = n * image.getHeight();

BufferedImage enlargedImage = new BufferedImage(w, h, image.getType());
for (int y=0;y<h;y++){
for(int x=0;x<w;x++){
enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n));
}
}

return enlargedImage;
}*/
}

我只获取我提供的图像(“pic.jpg”)的左上角 8x8 像素。谁能解释一下吗?

最佳答案

看看这段代码,它纠正了各种错误。大多数更改都有注释。否则,请检查文档。用于替代方法。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel{

static BufferedImage image;
private static JPanel imagePanel;
private static JFrame frame;

private static String dir = "image\\pic.jpg";

public Main(){
image = new BufferedImage(400,140,BufferedImage.TYPE_INT_RGB);
}

//We set our preffered size if we succeed in loading the image
@Override // TO DETECT WRONG SPELLING!
public Dimension getPreferredSize(){
/*
public Dimension getPrefferedSize(){
*/
if(image == null){
return new Dimension(100, 100);
}else{
// A JPanel is an ImageObserver
return new Dimension(image.getWidth(this), image.getHeight(this));
}
}

//Draw our image on the screen with Graphic's "drawImage()" method
// For Swing components.
@Override
public void paintComponent(Graphics g){
// public void paint(Graphics g){
super.paintComponent(g);
// A JPanel is an ImageObserver
g.drawImage(image, 0, 0, this);
}

public static void main(String[] args){
frame = new JFrame("Loading image from file example");
imagePanel = new JPanel();
//Release the resource window handle as we close the frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
/*frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});*/
imagePanel.add(new Main());
frame.add(imagePanel);
frame.pack();
frame.setVisible(true);
}
}

关于java - 程序应该显示整个图像只显示它的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687689/

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