Witam skleiłem coś takiego:
klasa Image
import java.awt.image.*;
public class Image extends javax.swing.JFrame {
public Image() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
BufferedImage loadImg = AppUtil.openfileImg(jPanel1);
jPanel1 = new JImagePanel(loadImg, 0, 0);
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
/////////////////////////////////////////////////////////
jPanel1.setAutoscrolls(true);
/////////////////////////////////////////////////////////
jPanel1.setVerifyInputWhenFocusTarget(false);
javax.swing.GroupLayout podkladLayout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(podkladLayout);
podkladLayout.setHorizontalGroup(
podkladLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 267, Short.MAX_VALUE));
podkladLayout.setVerticalGroup(
podkladLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 203, Short.MAX_VALUE));
jTextArea1.setBackground(new java.awt.Color(153, 204, 255));
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setRows(5);
jTextArea1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
jScrollPane1.setViewportView(jTextArea1);
jTextArea1.getAccessibleContext().setAccessibleParent(this);
jTextField1.setEditable(false);
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setText("Jakiś mało interesujący tekst");
jTextField1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE).addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, loadImg.getWidth(), Short.MAX_VALUE).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)).addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
loadImg.getHeight(), Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Image cos = new Image();
cos.setTitle("Obrazek");
cos.setVisible(true);
}
});
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JPanel jPanel1;
}
klasa AppUtil
import java.awt.image.*;
import java.io.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class AppUtil {
public static BufferedImage openfileImg(JPanel f) {
BufferedImage image = null;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("../../.."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith(".gif") || name.endsWith(".jpg")
|| name.endsWith(".jpeg") || name.endsWith(".bmp")
|| name.endsWith(".png") || f.isDirectory();
}
public String getDescription() {
return "Pliki graficzne";
}
});
int r = chooser.showOpenDialog(f);
if (r == JFileChooser.APPROVE_OPTION) {
try {
image = ImageIO.read(chooser.getSelectedFile());
} catch (IOException ex) {
Logger.getLogger(AppUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return image;
} else {
System.exit(0);
return null;
}
}
public static File openfileTxt() {
File tekst = null;
JFrame f = null;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("../../../Pulpit"));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith(".txt") || name.endsWith(".csv") || f.isDirectory();
}
public String getDescription() {
return "Pliki tekstowe";
}
});
int r = chooser.showOpenDialog(f);
if (r == JFileChooser.APPROVE_OPTION) {
tekst = chooser.getSelectedFile();
return tekst;
} else {
System.exit(0);
return null;
}
}
}
i klasa JImagePanel
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class JImagePanel extends JPanel {
private javax.swing.JScrollPane jScrollPane1;
private BufferedImage image;
int x, y;
public JImagePanel(BufferedImage image, int x, int y) {
super();
this.image = image;
this.x = x;
this.y = y;
//jScrollPane1.setViewportView(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, x, y, null);
}
}
po otwarciu obrazka większego niż ekran lub zmniejszeniu okna nie uruchamiają się scrolle jak to naprawić? Jestem newbie więc proszę o litość w komentarzach [soczek]
-----edit----
nie żeby było pilne ale nie ma nikogo komu się chce zobaczyć? Proszę kogośkolwiek :)