posted 14 years ago
Okay, I am still working on this but the functionality is working now. I would appreciate any comments/suggestions/insights, especially anything that would make my methods more elegant or my code more efficient and streamlined; here is what I came up with:
My component tree now looks like: JApplet -> JPanel -> JScrollPane -> JLayeredPane -> [DisplayJAI (contains a rendered PDF image; in DEFAULT layer) & JPanel (extended; in PALETTE layer)]
So at its core the functionality relies on a layered pane, the bottom layer containing a DisplayJAI displaying a PDF and the next layer up containing my custom JPanel. The layered pane, DisplayJAI and the custom JPanel all resize with the image (the application allows you to zoom in and out by scaling the image), so the problem I am working on next is getting the highlights and the text notes to resize along with everything else.
At any rate, the text notes and the highlighting are both implemented in the custom JPanel (called "edits" in my code). First I created a mouse listener, in the parent JPanel and listening to the custom JPanel (it might make more sense to eventually move this to the custom JPanel), and overrode mousePressed:
Then for highlighting, I also added mouseDragged and mouseReleased overrides:
In my custom JPanel I have an inner class called "Highlight" because I need to allow users to edit or delete highlights, and I need to save them in a database. All it does is save the information about the highlight (x, y, width, height, color, etc.). All the Highlight instances are stored in a HashMap. On a side note, I think I want to eventually use translucent components instead of fillRect(), but for now it works. Here are the highlight-related methods in my custom JPanel:
And, last but not least, I overrode the paintComponent method to draw all of my highlights, though I am not sure if this ruins the efficiency I was trying to achieve in passing dimension parameters into my repaint() methods:
The text notes are a little more straightforward, though I have had a very hard time getting the Swing components to do what I want them to do. I am kind of disappointed in the apparent lack of built-in functionality in Swing, from things like the variety of built-in events to more decorative things like the absence of drop shadows and other things indicative of a more modern GUI language. (I say "apparent" because I am still quite new to Swing and I still have a lot to learn.) The saving grace is that I can usually (eventually) add the non-decorative functionality I need (I am no graphic designer), but it would be nice if Swing straight out of the box was more up-to-par with some of the newer languages.
Any thoughts, suggestions, criticisms?
-TennSeven