Na przyszly tydzien musze napisac solver problemu NPuzzle ale jako ze jestem dosc slaby z Javy szybko napotkalem problem. W Netbeansie stworzylem proste GUI z kilkoma przyciskami jak na rysunku: http://img253.imageshack.us/my.php?image=guiyj2.png Pomaranczowa ramka do JPanel, w ktorym musi sie wyswietlac wczytany rysunek.
Jako, ze dopiero zaczalem prawie nic w kodzie nie ma oprocz GUI:
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
public class NpuzzleGUI extends javax.swing.JFrame {
private JFileChooser fc = new JFileChooser();
private int speed = 0;
private int size = 4;
/** Creates new form NpuzzleGUI */
public NpuzzleGUI() {
initComponents();
}
private void changeSpeed(String speedVal) {
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
start = new javax.swing.JButton();
reset = new javax.swing.JButton();
tileNumberChange = new javax.swing.JButton();
tileNumber = new javax.swing.JTextField();
tileLabel = new javax.swing.JLabel();
SpeedLabel = new javax.swing.JLabel();
loadImage = new javax.swing.JButton();
exitProgramme = new javax.swing.JButton();
speedChange = new javax.swing.JComboBox();
imagePanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Npuzzle");
start.setText("Start");
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
reset.setText("Reset");
reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetActionPerformed(evt);
}
});
tileNumberChange.setText("Set");
tileNumberChange.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tileNumberChangeActionPerformed(evt);
}
});
tileNumber.setText("4");
tileLabel.setText("Number of tiles:");
SpeedLabel.setText("Speed:");
loadImage.setText("Load image");
loadImage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loadImageActionPerformed(evt);
}
});
exitProgramme.setText("Exit");
exitProgramme.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitProgrammeActionPerformed(evt);
}
});
speedChange.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MIN", "MED", "MAX" }));
speedChange.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
speedChangeActionPerformed(evt);
}
});
javax.swing.GroupLayout imagePanelLayout = new javax.swing.GroupLayout(imagePanel);
imagePanel.setLayout(imagePanelLayout);
imagePanelLayout.setHorizontalGroup(
imagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 529, Short.MAX_VALUE)
);
imagePanelLayout.setVerticalGroup(
imagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 407, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(imagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(tileLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(start)
.addGap(18, 18, 18)
.addComponent(reset)
.addGap(18, 18, 18)
.addComponent(loadImage)
.addGap(18, 18, 18)
.addComponent(exitProgramme))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(tileNumber, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tileNumberChange, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(SpeedLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(speedChange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tileNumber, tileNumberChange});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tileLabel)
.addComponent(tileNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SpeedLabel)
.addComponent(speedChange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tileNumberChange)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(imagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(start)
.addComponent(reset)
.addComponent(loadImage)
.addComponent(exitProgramme))
.addGap(20, 20, 20))
);
pack();
}// </editor-fold>
private void tileNumberChangeActionPerformed(java.awt.event.ActionEvent evt) {
String x = tileNumber.getText();
size = Integer.parseInt(x);
}
private void exitProgrammeActionPerformed(java.awt.event.ActionEvent evt) {
dispose ();
}
private void loadImageActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == loadImage) {
int returnVal = fc.showOpenDialog(NpuzzleGUI.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String location = file.getAbsolutePath();
}
}
}
private void resetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void startActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void speedChangeActionPerformed(java.awt.event.ActionEvent evt) {
JComboBox cb = (JComboBox)evt.getSource();
String speedVal = (String)cb.getSelectedItem();
changeSpeed(speedVal);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NpuzzleGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel SpeedLabel;
private javax.swing.JButton exitProgramme;
private javax.swing.JPanel imagePanel;
private javax.swing.JButton loadImage;
private javax.swing.JButton reset;
private javax.swing.JComboBox speedChange;
private javax.swing.JButton start;
private javax.swing.JLabel tileLabel;
private javax.swing.JTextField tileNumber;
private javax.swing.JButton tileNumberChange;
// End of variables declaration
}
Jak widac przycisk Load Image otwiera FileChooser (w funkcji loadImageActionPerformed) i zapamietuje plik. Teraz pytanie - jak zrobic, zeby ten obrazek z pliku wyswietlil sie w moim JPanelu? Probowalem juz setki rzeczy ale zawsze albo cos sie chrzanilo albo nie dzialalo ;/ Jest to najwazniejsza rzecz w sumie w tym programie, bo potem musze podzielic ten rysunek na n^2 kwadratow i przeprowadzic na nim kilka operacji.
Z gory dzieki za pomoc.