Monday 19 August 2019

java - How to add an image to JPanel that is in a JPanel?



I have 7 JPanel containers in total. I'd like to add a png image that I generate, or buffer it with the help of the button(charger image) in the JPanel(imagePan)



Most of the examples I've seen so far in the Swing Tutorials use ImageIcon




  1. The images generated are at 326X254

  2. How to properly add an image to a panel?




Here you'll find the code generating the window below:



   import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;


import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import javax.swing.border.Border;

public class View {
private JFrame frame;
private JPanel globalPan, firstHorisontalPan, secondhorisontalPan,
calibrationPan, imagePan, manipPan, solutionPan; // susp
private JButton raproche, ecarter, sauvgarder, demarrer, stop, charger;
private BorderLayout BorderGlobalePan, BorderSecondPane, BorderManipPane,
BorderFirstHorisontalPan, BorderResolPan, BorderCalibPan,
BorderChargerPan;

private JTextArea console;
private Box calibrationBox, solutionBox;


public void init() {
// declaration de JFrame
frame = new JFrame("Rubi's Cube IHM");

// JPanle
globalPan = new JPanel();

firstHorisontalPan = new JPanel();
secondhorisontalPan = new JPanel();
imagePan = new JPanel();
manipPan = new JPanel();
calibrationPan = new JPanel();
solutionPan = new JPanel();

//
calibrationBox = Box.createVerticalBox();
solutionBox = Box.createVerticalBox();


// borderLayout
BorderGlobalePan = new BorderLayout();
BorderSecondPane = new BorderLayout();
BorderManipPane = new BorderLayout();
BorderFirstHorisontalPan = new BorderLayout();
BorderResolPan = new BorderLayout();
BorderCalibPan =new BorderLayout();
BorderChargerPan = new BorderLayout();


// JButton
raproche = new JButton("raprocher");
ecarter = new JButton("ecarter");
sauvgarder = new JButton("sauvgarder");
demarrer = new JButton("demarrer");
stop = new JButton("stop");
charger = new JButton("charger image");

console = new JTextArea();


//add JPanel names
firstHorisontalPan.setBorder(BorderFactory.createTitledBorder("Etat"));
calibrationPan.setBorder(BorderFactory.createTitledBorder("calibration"));
solutionPan.setBorder(BorderFactory.createTitledBorder("résolution & manipulation"));
imagePan.setBorder(BorderFactory.createTitledBorder("visualisation"));

// definition of JButton size
raproche.setPreferredSize(new Dimension(200, 30));
ecarter.setPreferredSize(new Dimension(200, 30));
sauvgarder.setPreferredSize(new Dimension(200, 30));

demarrer.setPreferredSize(new Dimension(200, 30));
stop.setPreferredSize(new Dimension(200, 30));
charger.setPreferredSize(new Dimension(200, 30));

//definition of JPanel size
globalPan.setPreferredSize(new Dimension(1024, 600));
firstHorisontalPan.setPreferredSize(new Dimension(1024, 130));
secondhorisontalPan.setPreferredSize(new Dimension(1024, 480));
imagePan.setPreferredSize(new Dimension(850, 480));
manipPan.setPreferredSize(new Dimension(150, 480));

calibrationPan.setPreferredSize(new Dimension(200, 200));
solutionPan.setPreferredSize(new Dimension(200, 100));

calibrationBox.setPreferredSize(new Dimension(200, 200));
solutionBox.setPreferredSize(new Dimension(200, 100));

firstHorisontalPan.setLayout(BorderFirstHorisontalPan);
firstHorisontalPan.add(console);
//image




// JPane calibration
calibrationBox.add(Box.createVerticalStrut(10));
calibrationBox.add(raproche);
calibrationBox.add(Box.createVerticalStrut(10));
calibrationBox.add(ecarter);
calibrationBox.add(Box.createVerticalStrut(10));
calibrationBox.add(sauvgarder);


calibrationPan.setLayout(BorderCalibPan);
calibrationPan.add(calibrationBox, BorderLayout.CENTER);

// JPane resolution & manipulation
solutionBox.add(Box.createVerticalStrut(10));
solutionBox.add(demarrer);
solutionBox.add(Box.createVerticalStrut(10));
solutionBox.add(stop);

solutionPan.setLayout(BorderResolPan);

solutionPan.add(solutionBox, BorderLayout.CENTER);


//JPane ManipPane
manipPan.setLayout(BorderManipPane);
manipPan.add(calibrationPan, BorderLayout.NORTH);
BorderManipPane.setVgap(20);
manipPan.add(solutionPan, BorderLayout.CENTER);

//JPane secondPane

secondhorisontalPan.setLayout(BorderSecondPane);
secondhorisontalPan.add(manipPan, BorderLayout.WEST);
BorderSecondPane.setHgap(7);
secondhorisontalPan.add(imagePan, BorderLayout.CENTER);

//JPane GlobalHorisontalPane
globalPan.setLayout(BorderGlobalePan);
globalPan.add(firstHorisontalPan, BorderLayout.NORTH);
BorderGlobalePan.setVgap(10);
globalPan.add(secondhorisontalPan, BorderLayout.CENTER);


//Jpane imagePan
BorderChargerPan.setVgap(10);
imagePan.add(charger);

// window
frame.add(globalPan);
frame.setSize(1024, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

frame.setTitle("cubeBerry");
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


IHM




Answer





How to proprely add an image to a Jpanel?





  1. Create an ImageIcon.

  2. Add the icon to a JLabel.

  3. Add the label to the JPanel.




Read the section from the Swing tutorial on How to Use Icons for more information and working examples.



Also, from your posted code, get rid of all the setPreferredSize() statements. The layout manager will determine the preferred size of the component. Swing was designed to be used with layout managers. Let the layout manager do its job.



console = new JTextArea();


When creating a JTextArea do something like:



console = new JTextArea(5, 30);



The will suggest the size should be 5 rows and 30 columns. Now the layout manager can calculate a preferred size based on this information.



private BorderLayout BorderGlobalePan, BorderSecondPane, BorderManipPane, ...


Variable names should NOT start with an upper case character. Most of your variable are correct, but not all. Be consistent!!!



frame.setSize(1024, 600);



Don't hard code a size. You don't know what the resolution of my computer is. Instead use the pack() method and let the layout managers do their job.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...