Witam, moj problem polega na tym ze chce wczytac obrazek do JPanelu. Chce zeby ten JPanel mial jakis staly rozmiar lub zeby wiekkoscia dopasowywal sie do obrazka. I aktualnie jesli chodz o edycje to skupiam sie na obracaniu ale to tez dziwnie dziala. Pewnie gdzies jest blad ale siedze juz jakis czas nad tym i nie smiga jak to sobie wyobrazam. Zasada dziala niby jest ok ale efekt nie jest zadowalajacy.
Prosze o pomoc lub wskazowki.
Oto kod programu:
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.JButton;
import java.io.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
public class Obrazek extends JFrame implements ActionListener {
// private static final long serialVersionUID = 1L;
JMenuBar pasek;
JMenu menu1, menu2, menu3, menu4, menu5, menu6;
JMenuItem jmi1, jmi2, jmi3, jmi4, jmi5, jmi6;
JTextArea pole;
JButton bt0 = new JButton("Open", new ImageIcon("open.png"));
JButton bt1 = new JButton("Save", new ImageIcon("save.png"));
JButton bt2 = new JButton("Reset", new ImageIcon("delete.png"));
JButton bt3 = new JButton("Histogram", new ImageIcon("histogram.jpg"));
JButton bt4 = new JButton("Rotate", new ImageIcon("rotation.png"));
JPanel panelLeft = new JPanel();
JPanel panelRight = new JPanel();
public Obrazek() {
super("Operacja :)");
setSize(800, 600);
setVisible(true);
setResizable(false);
Container kontener = getContentPane();
// kontener.setBackground(Color.green);
kontener.setLayout(new BorderLayout());
JPanel panelPrzyciskow = new JPanel();
panelPrzyciskow.setLayout(new GridLayout(1, 5));
panelPrzyciskow.add(bt0);
panelPrzyciskow.add(bt1);
panelPrzyciskow.add(bt2);
panelPrzyciskow.add(bt3);
panelPrzyciskow.add(bt4);
kontener.add(panelPrzyciskow, "North");
JSplitPane splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPanel.setBackground(Color.green);
// splitLeft.setSize(100,100);
kontener.add(splitPanel, BorderLayout.CENTER);
splitPanel.setLeftComponent(panelLeft);
splitPanel.setRightComponent(panelRight);
pasek = new JMenuBar();
setJMenuBar(pasek);
menu1 = new JMenu("File");
pasek.add(menu1);
menu2 = new JMenu("Image");
pasek.add(menu2);
menu3 = new JMenu("Transform");
pasek.add(menu3);
menu4 = new JMenu("Rotate");
pasek.add(menu4);
menu5 = new JMenu("Filters");
pasek.add(menu5);
menu6 = new JMenu("Options");
pasek.add(menu6);
jmi1 = new JMenuItem("Odswiez", new ImageIcon("refresh.gif"));
jmi1.addActionListener(this);
jmi2 = new JMenuItem("Zamknij", new ImageIcon("ikona.png"));
jmi2.addActionListener(this);
menu1.add(jmi1);
// menu1.addSeparator();
menu1.add(jmi2);
jmi3 = new JMenuItem("Wytnij", new ImageIcon(".gif"));
jmi3.addActionListener(this);
menu2.add(jmi3);
jmi4 = new JMenuItem("Kopiuj", new ImageIcon(".gif"));
jmi4.addActionListener(this);
menu3.add(jmi4);
jmi5 = new JMenuItem("Wklej", new ImageIcon(".gif"));
jmi5.addActionListener(this);
menu4.add(jmi5);
jmi6 = new JMenuItem("O programie", new ImageIcon(".gif"));
jmi6.addActionListener(this);
menu5.add(jmi6);
bt0.addActionListener(this);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
/*
* pole = new JTextArea(); pole.setBounds(10, 10, 570, 230);
* pole.setToolTipText("Edytor tekstu"); kontener.add(pole);
*/
// validate();
// repaint();
}
JFileChooser fc = new JFileChooser();;
BufferedImage img = null;
AffineTransform transform = new AffineTransform();
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt0) {
System.out.println("bt0");
int returnVal = fc.showOpenDialog(Obrazek.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
img = ImageIO.read(fc.getSelectedFile());
File file = fc.getSelectedFile();
repaint();
System.out.println("Opening: " + file.getName() + ".");
} catch (final Exception a) {
}
}
} else if (e.getSource() == bt1) {
System.out.println("bt1");
int returnVal = fc.showSaveDialog(Obrazek.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
final boolean rc = ImageIO.write(img, "jpeg", fc.getSelectedFile());
File file = fc.getSelectedFile();
repaint();
System.out.println("Saving: " + file + " is: " + rc + ".");
} catch (final Exception a) {
}
}
}
if (e.getSource() == bt2) {
System.out.println("bt2");
img=null;
repaint();
}
if (e.getSource() == bt3) {
System.out.println("bt3");
}
if (e.getSource() == bt4) {
System.out.println(img);
try {
if(img!=null){
transform.setToTranslation(14, 80);
transform.rotate(Math.toRadians(40));
repaint();
}
} catch (final Exception a) {
}
}
if (e.getSource() == jmi1) {
validate();// odswiezanie
}
if (e.getSource() == jmi2) {
int wybor = JOptionPane.showConfirmDialog(null, "Zamknąć edytor?","Zamykanie programu", 1);
if (wybor == JOptionPane.OK_OPTION) {
System.exit(0);
} else {
return;
}
}
if (e.getSource() == jmi3) {
pole.cut();
System.out.println("You clicked the button");
}
if (e.getSource() == jmi4) {
pole.copy();
}
if (e.getSource() == jmi5) {
pole.paste();
}
if (e.getSource() == jmi6) {
JOptionPane.showMessageDialog(null, "Test");
}
}
public void paint(final Graphics g) {
final Graphics2D g2 = (Graphics2D)panelLeft.getGraphics();
if (img != null) {
g2.drawImage(img, transform, this);
repaint();
}
bt0.repaint();
bt1.repaint();
bt2.repaint();
bt4.repaint();
}
public static void main(String args[]) {
Obrazek ramka = new Obrazek();
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// zamkniecie po
// kliknieciu na
// x
}
}