Here's how I did it. Please let me know how you'd improve this beginner code! ;)
Essentially, here' the trick to get a button to draw rectangles:
Extend your main app class to a JFrame (or JComponent) or ???
Declare right at the top of your main app your class to draw (DrawCanvas) and extend to a JPanel.
Right at the top of your main app class declared an ArrayList to hold the stuff that you'll draw.
Declare a variable for the drawing class right at the top of your main app class.
In your control event (button in my case) use a function to prepare the stuff to draw (I used one called AddRectangle()).
In your drawing class, override the paintComponent and use a for each to draw all the stuff you squirrelled away in your array.
Since you can't control directly the drawing, you have to understand that each time repaint() gets called that drawing function will be called. That means that you have to stash away all your stuff like a bloody squirrel in order for the drawing method to draw or redraw the screen correctly.In the end its usually a bunch of arrays that you'll end up using and a bunch of for each loop to go through them.
package views;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
public class appMainWindow extends JFrame
{
class PdfLocation
{
public double xPos;
public double yPos;
}
class DrawCanvas extends JPanel
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (PdfLocation p : PdfLocations)
{
g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
repaint();
}
}
}
public void AddRectangle()
{
PdfImagesCount++;
lblPdfcount.setText(Integer.toString(PdfImagesCount));
PdfLocation rect = new PdfLocation();
if (PdfLocations.isEmpty() == false)
{
PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
rect.xPos = spot.xPos + 45;
rect.yPos = 10;
}
else
{
rect.xPos = 10;
rect.yPos = 10;
}
PdfLocations.add(rect);
}
private JFrame frame;
public ArrayList<PdfLocation> PdfLocations = new ArrayList<PdfLocation>();
public int PdfImagesCount = 0;
public static final int CANVAS_HEIGHT = 700;
public static final int CANVAS_WIDTH = 1000;
private DrawCanvas canvas;
private JLabel lblPdfcount;
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
public appMainWindow()
{
// Set up a custom drawing JPanel
canvas = new DrawCanvas();
canvas.setBackground(Color.WHITE);
canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT);
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 1280, 850);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnAddARectangle = new JButton("Add a rectangle");
btnAddARectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
AddRectangle();
repaint();
}
});
btnAddARectangle.setBounds(0, 6, 151, 29);
frame.getContentPane().add(btnAddARectangle);
frame.getContentPane().add(canvas);
lblPdfcount = new JLabel("PdfCount");
lblPdfcount.setBounds(10, 43, 61, 16);
frame.getContentPane().add(lblPdfcount);
}
}