Java 程序应该显示整个图像,只显示其中的一部分 public类Main扩展了JPanel{ 私有静态最终长serialVersionUID=1L; 静态缓冲图像; 专用静态JPanel imagePanel; 私有静态JFrame; 私有静态字符串dir=“image\\pic.jpg”; 公用干管(){ 试一试{ image=ImageIO.read(新文件(dir)); }捕获(IOE异常){ //如果无法从目录中读取,请通知客户端 System.out.println(“从目录读取时出错:+e.getMessage()); } } //如果成功加载图像,我们将设置首选大小 公共维度GetPreferedSize(){ if(image==null){ 返回新维度(100100); }否则{ 返回新维度(image.getWidth(null)、image.getHeight(null)); } } //使用Graphic的“drawImage()”方法在屏幕上绘制图像 公共空间涂料(图g){ g、 drawImage(image,0,0,null); } 公共静态void main(字符串[]args){ frame=newjframe(“从文件示例加载图像”); imagePanel=newjpanel(); //关闭框架时释放资源窗口句柄 frame.addWindowListener(新的WindowAdapter(){ 公共无效窗口关闭(WindowEvent e){ 系统出口(0); } }); 试一试{ image=ImageIO.read(新文件(dir)); if(image.getWidth(null)!=0){ System.out.println(image.getWidth(null)); System.out.println(image.getHeight(null)); } }捕获(例外e){ e、 printStackTrace(); } 添加(新的Main()); frame.add(图像面板); //frame.pack(); 框架设置尺寸(200300); frame.setVisible(true); } /* 公共静态缓冲区图像放大(缓冲区图像,整数n){ int w=n*image.getWidth(); int h=n*image.getHeight(); BuffereImage放大图像=新的BuffereImage(w,h,image.getType()); 对于(int y=0;y
使用布局管理器,该布局管理器使用Java 程序应该显示整个图像,只显示其中的一部分 public类Main扩展了JPanel{ 私有静态最终长serialVersionUID=1L; 静态缓冲图像; 专用静态JPanel imagePanel; 私有静态JFrame; 私有静态字符串dir=“image\\pic.jpg”; 公用干管(){ 试一试{ image=ImageIO.read(新文件(dir)); }捕获(IOE异常){ //如果无法从目录中读取,请通知客户端 System.out.println(“从目录读取时出错:+e.getMessage()); } } //如果成功加载图像,我们将设置首选大小 公共维度GetPreferedSize(){ if(image==null){ 返回新维度(100100); }否则{ 返回新维度(image.getWidth(null)、image.getHeight(null)); } } //使用Graphic的“drawImage()”方法在屏幕上绘制图像 公共空间涂料(图g){ g、 drawImage(image,0,0,null); } 公共静态void main(字符串[]args){ frame=newjframe(“从文件示例加载图像”); imagePanel=newjpanel(); //关闭框架时释放资源窗口句柄 frame.addWindowListener(新的WindowAdapter(){ 公共无效窗口关闭(WindowEvent e){ 系统出口(0); } }); 试一试{ image=ImageIO.read(新文件(dir)); if(image.getWidth(null)!=0){ System.out.println(image.getWidth(null)); System.out.println(image.getHeight(null)); } }捕获(例外e){ e、 printStackTrace(); } 添加(新的Main()); frame.add(图像面板); //frame.pack(); 框架设置尺寸(200300); frame.setVisible(true); } /* 公共静态缓冲区图像放大(缓冲区图像,整数n){ int w=n*image.getWidth(); int h=n*image.getHeight(); BuffereImage放大图像=新的BuffereImage(w,h,image.getType()); 对于(int y=0;y,java,image,swing,java-2d,Java,Image,Swing,Java 2d,使用布局管理器,该布局管理器使用JPanel的整个区域,而不是默认的FlowLayout,后者仅使用组件首选的大小。您可以使用 public class Main extends JPanel{ private static final long serialVersionUID = 1L; static BufferedImage image; private static JPanel imagePanel; private
JPanel
的整个区域,而不是默认的FlowLayout
,后者仅使用组件首选的大小。您可以使用
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;
}*/
}
在Swing中进行自定义绘制时,也要覆盖
paintComponent
而不是paintComponent
,并记住调用super.paintComponent(g)
使用布局管理器,该管理器使用JPanel
的整个区域,而不是默认的FlowLayout
,后者只使用组件首选的大小。您可以使用
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;
}*/
}
在Swing中进行自定义绘制时,也要覆盖
paintComponent
而不是paint
,并记住调用super。paintComponent(g)
查看此代码以更正各种错误。大多数更改都有注释。否则,请查看文档。有关替代方法
imagePanel.setLayout(new GridLayout());
请查看此代码,该代码更正了各种错误。大多数更改都有注释。否则,请查看文档。以了解替代方法
imagePanel.setLayout(new GridLayout());
请在此处而不是在pastebin中发布您的相关代码。您可以单击网站编辑器中的“帮助”按钮了解如何设置代码的格式。请在此处而不是在pastebin中发布您的相关代码。您可以单击网站编辑器中的“帮助”按钮了解如何设置代码的格式。