Hello!
Thanks TorstenH. for the link, it was quite good. I'd love to hear how I can make this code better, being a new programmer and all... ;) Your comments are appreciated!
So to get it to work I had to start by having my main application class extend another class (I chose JFrame). If I don't extend my main class like that I can't draw(?).
Then inside my main app class I created right at the top my two classes. One is simply to store the location of a rectangle. The other is to draw.That drawing class extends JPanel (sort of the norm for swing).
What's special about Java drawing is that since you're at the mercy of the system (you can't access the drawing directly) you have to stash all your drawing stuff in a bag like a bloody squirrel. In my case, I have to use an array, and every time the screen is repainted (repaint()) the whole thing needs to be repainted by going through everything I put inside my array.
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();
}
}
}
So above everytime repaint() is called, either from there or from somewhere else in my code (I think the fact I extend my main app class to a JFrame allows me to use repaint() anywhere in my code) this for each construct will repaint everything.
This means if you're doing some sort of drawing app, you'd need to store all your shapes as Shape objects. There's tons of stuff on the web for that.
So in my case, I needed to draw rectangles (Pdf Imposition app I'm writing) on the screen based on the click event of a button. My button simply calls a function called AddRectangle() and then repaint() the screen, for good measure.
JButton btnAddARectangle = new JButton("Add a rectangle");
btnAddARectangle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
AddRectangle();
repaint();
}
});
This AddRectangle() function in turns prepares the stuff to be drawn on the screen. Again, since you need to act like a bloody squirrel and stash your stuff away in arrays, the function that I use to prepare to draw looks at the array to see where we are and adds a rectangle next to the previous one.
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);
}
Thanks again for your link it was very good TorstenH.! :)
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()
{
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);
}
}
Here's another link that is quite good as well for those like me starting with Java and drawing.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J4b_CustomGraphics.html[
^]
Hope this can help someone else trying to draw stuff based on events from buttons and what not!