I have the following code to draw on a JFrame
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class VentanaDisposicionMesas extends JComponent implements ActionListener{
static JFrame frmPrincipal;
private Point inicioArrastre;
private Point finArrastre;
private ArrayList lineas = new ArrayList();
JButton btnGuardar;
JButton btnCerrar;
JPanel pnlBotones;
public VentanaDisposicionMesas() {
super();
metodoCargarPanel();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { // cuando se presiona el mouse
inicioArrastre = new Point(e.getX(), e.getY());
repaint();
}
public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse
finArrastre = new Point(e.getX(), e.getY());
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y);
lineas.add(linea);
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse
finArrastre = new Point(e.getX(), e.getY());
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y);
lineas.add(linea);
inicioArrastre = new Point(finArrastre.x, finArrastre.y);
repaint();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
for (Object linea : lineas) {
g2.draw((Shape) linea);
}
}
private Line2D.Float crearLinea(int x1, int y1, int x2, int y2) {
return new Line2D.Float(x1, y1, x2, y2);
}
public static void main(String[] args) {
JFrame ventana = new JFrame("Ejemplo para la Disposición de Mesas");
ventana.setSize(600, 400);
ventana.setLocationRelativeTo(null);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.add(new VentanaDisposicionMesas());
ventana.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnCerrar){
this.setVisible(false);
}
else if (ae.getSource() == btnGuardar){
;
}
}
private void metodoCargarPanel() {
pnlBotones = new JPanel();
pnlBotones.setBounds(0, 550, 400, 30);
this.add(pnlBotones);
btnCerrar = new JButton("Cerrar");
btnCerrar.addActionListener(this);
btnCerrar.setBounds(50, 15, 150, 15);
pnlBotones.add(btnCerrar);
btnGuardar = new JButton("Cerrar");
btnGuardar.addActionListener(this);
btnGuardar.setBounds(205, 15, 150, 15);
pnlBotones.add(btnGuardar);
}
}
It works fine for me, but now I try to add the panel with the two buttons, and I have not succeeded in any way.
I have tried adding the buttons to this.add(btnClose); but it does not work
I have tried to make another JFrame and add this one from the drawing to the previous one, but I have not been able to
if I try to add it with the name of the JFrame (window.add();) it gives me an error
I'm starting with the subject of Java Swing, and I don't understand it very well, can someone enlighten me a bit on the way?
on the other hand, would it be possible to save the drawing made in the JFrame as an image? That would be the function of the save button
Regards, and thank you very much
You are not adding the button panel to the frame, but to the component (the drawing one), components if I remember correctly have FlowLayout by default, while JFrame has BorderLayout. FlowLayout won't display components if they don't have a defined size.
You can take advantage of the places that BorderLayout lets you use for this
BorderLayout adds to CENTER by default replacing any component found there and occupying all the available space while there are no components around it, in other words you can only add 1 component in each place (hence nested panels with BorderLayout)
For example:
But you are adding the button panel to the component...
1 - Not visible if it has no defined size (FlowLayout effect).
2 - I don't think you want to add it there since you could paint over the buttons and they would appear in the saved image.
The button panel must be added for example to NORTH but you must specify it because it is not by default as CENTER.
After loading/creating the panel you can add it on top with:
On the other hand, there is the option of saving what is drawn in an image, this is done by printing the graphics of the component in a
BufferedImage
and usingImageIO.write
can write the image.Keep in mind that it prints the drawn place, that is, the background is transparent with red lines, this is because you are painting a component from scratch, just as JButton has its graphics, you are creating your own for the drawing panel, if you want that it be on a white background only have a fill of white color.
The code is the same but I distributed it in different classes so as not to fight with static variables, since there was a conflict in the creation of the JFrame and the component (static main method and the buttons panel components) it is up to you to implement it however you want.
I hope this helps you