1
I’d like to know how to draw a line inside a JPanel
which sits within a JFrame
, by pressing a button. The layout of the button and the JPanel
within the JFrame
is shown in the image below:
When the button is pressed the program should draw a line from point 10,10 to point 100,100 JPanel
. I’ve seen examples like the code below to draw line.
public void desenha( Graphics g ){
g.setColor(red);
g.drawLine(10, 10, 100, 100);
}
The problem is I don’t know where to use this method to draw the line in JPanel
, and only when the button is pressed.
Codes: Main Class:
public class Teste {
public static void main(String[] args) {
Tela t = new Tela();
t.setVisible(true);
}
}
Screen Class:
public class Tela extends javax.swing.JFrame {
public Tela() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
botao = new javax.swing.JButton();
viewport = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
botao.setText("desenha");
botao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoActionPerformed(evt);
}
});
viewport.setBackground(new java.awt.Color(255, 255, 255));
viewport.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
javax.swing.GroupLayout viewportLayout = new javax.swing.GroupLayout(viewport);
viewport.setLayout(viewportLayout);
viewportLayout.setHorizontalGroup(
viewportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 361, Short.MAX_VALUE)
);
viewportLayout.setVerticalGroup(
viewportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 231, 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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addComponent(botao))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(viewport, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(botao)
.addGap(18, 18, 18)
.addComponent(viewport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void botaoActionPerformed(java.awt.event.ActionEvent evt) {
}
// Variables declaration - do not modify
public javax.swing.JButton botao;
public static javax.swing.JPanel viewport;
// End of variables declaration
}
Please present a [mcve] of your code so that it is possible to help you.
– user28595
Question edited with code
– AlexMauricio777
Now it’s unclear how the figures will be drawn.
– user28595
This is exactly what I do not know how to do, I need to draw some figure inside the Jpanel by clicking the button, but I have no idea which method to create or how to create.
– AlexMauricio777
Draw how? When?
– user28595
It can be a line. By pressing the button a line inside that Jpanel should be drawn. Should I create a method to draw this shape? Should I use some specific library?
– AlexMauricio777
I suggest you edit the question and try to explain better what you want to do, because in the comments you are asking something very different from the question.
– user28595
The answer to your question is that you can create a subclass of
JPanel
superimposing the methodpaintComponent(Graphics)
. See in the questions I marked as duplicate more details of how to do this.– Victor Stafusa
I’ve answered something similar here on the site, but it was about drawing as the mouse moves, take a look at this link and good studies ;)
– user28595
Okay, I edited the question to make it more specific, and I’m going to look at the answers cited by you to try to implement my code. Thanks for now.
– AlexMauricio777
Draw lines has also been answered here, by Victor himself: How to draw an arrow using Java2d?
– user28595
@Alexmauricio777 what I recommend is that Read a read on the questions we marked there at the beginning, because Cvoce needs to have an average nocao of how it works java2d to do this without having problems. It’s kind of boring to handle because the API is complex and sometimes confusing, but nothing impossible to learn, if you have the patience to read and try to understand how it behaves.
– user28595