• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need a little guidance

 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to develop what I think is a simple application. I want to create a Swing Application that has two parts(two panels I think). One panel will contain gif images that the user can move to the other panel. I think I know what to do, but I would like to know if my approach is correct.

1. I need to create one main frame that consists of two panels, one will hold the gif images, and the other will be the area to which you can drag the gif images.

2. I need to create JButtons with images. These can be dragged by using actionEvents (mouseClicked or something like that) and by updating their coordinates.

I know this is a very rough outline, but if you forsee any problems with this approach or if you have any advice, please let me know. I am a C++ (command-line) programmer and I am having trouble grasping these concepts. GUI's are new to me. Also, if you know of any good java or GUI books I would appreciate it. It seems I can only learn by seeing other programmers examples.

Thanks for your time and help,
James
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of ways to put this together. A component approach is possible. In this I would suggest using a JLabel for the images because JButton will consume your 'mousePressed' event and will require more work dealing with appearance issues.

I favor a graphics approach which may involve more event code translation. The main issue to consider is whether or not you want the image to drag smoothly across the boundry between the JPanels. If so you could use a GlassPane (see JRootPane api and the tutorial) which may involve mouse event coordinate translation for your whole JFrame. A slightly lighter solution is to use an OverlayLayout to cover only the two JPanels, ie, it won't cover other widgits in the gui.

For the OverlayLayout approach you could put both JPanels on an underlying JPanel and add this to another JPanel which has the OverlayLayout layout manager. Add a non–opaque JPanel to the bottom–most (OverlayLayout) JPanel which will function as a clear overlay on top of the two JPanels. When you drag your images from their home JPanel toward the target JPanel you transfer the rendering of it to the overlay panel, for smooth boundry crossing, until you are ready to drop it on the target. This involves some mouse coordinate transform shenanigans but the SwingUtilities class has useful methods for this. The GlassPane demo in the swing tutorial shows how to use (one of) them.

As far as drawing the images on any of the JPanels I suggest using Rectangles. Keep them in an array that correlates with an array for the images. Each Rectangle will have the same width and height of its corresponding image. You can select and move the Rectangles with 'mousePressed'/'mouseDragged' and draw the image at the current r.x, r.y origin of its Rectangle.

About using a Component as a graphics component, ie, with no child components: The Components layout manager has no information to report to parent components for its size requirements so you either have to supply size hints or put them in a parent layout manager that will expand them to fill all the available space, eg, the center section of BorderLayout or, esp. in this case, GridLayout. In swing, and now in AWT as of j2se 1.5, you can use 'setPreferredSize' to provide a size hint. An alternative especially useful for graphic components with dynamic size changes such as changeing images or adding/moving graphics primatives, is to override 'getPreferredSize' and return the current desired Dimension.

[ May 09, 2005: Message edited by: Craig Wood ]
 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your reply:
"As far as drawing the images on any of the JPanels I suggest using Rectangles. Keep them in an array that correlates with an array for the images. Each Rectangle will have the same width and height of its corresponding image. You can select and move the Rectangles with 'mousePressed'/'mouseDragged' and draw the image at the current r.x, r.y origin of its Rectangle."

I'm not sure what you are referring to here. Are you saying put the images in a rectangle?? If so, how??

From your reply:
"About using a Component as a graphics component, ie, with no child components: The Components layout manager has no information to report to parent components for its size requirements so you either have to supply size hints or put them in a parent layout manager that will expand them to fill all the available space, eg, the center section of BorderLayout or, esp. in this case, GridLayout. In swing, and now in AWT as of j2se 1.5, you can use 'setPreferredSize' to provide a size hint. An alternative especially useful for graphic components with dynamic size changes such as changeing images or adding/moving graphics primatives, is to override 'getPreferredSize' and return the current desired Dimension."

I am really not sure what any of this is referring to. Is it possible to implement all these ideas in Swing?? Also, I thought that you were not to use void paint(Graphics g) in Swing.

The rest of your post was very helpful, but I am kind of confused by these 2 paragraphs.

Thanks for the help,
James
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Rectangle 'r' stores the x, y location and the w, h size of the image. The width and height is useful for boundry checking, eg, if you want to limit where it can be dragged or to keep it from being moved off screen.
 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, I have it now. When I run this applet in a browser, which class file do I include in the
<APPLET CODE="myClass.class" WIDTH=200 HEIGHT=300>
 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting an "Applet not Initialized message" Why is this???
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the plug–in console for the exceptions that caused the init failure. If the icon isn't in the systray (win os) you can find it in the control panel. Open it, select the advanced tab, java console, show console.
 
auden james
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that supposed to do. I made the changes, but still nothing.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing an applet for the first time in a browser window is a little more difficult than testing from the command prompt. Here's some options:
1 – put a main method inside the JApplet class and run the applet from the command line, just like an app. Load the applet into a JFrame and call its 'init' method before the call to 'setVisible'. All errors are reported in the (command prompt) comsole.

2 – put a comment at the top of the JApplet file

and run it in appletviewer with
prompt>appletviewer AnimationApplet.java
All errors are reported in the command prompt console.

3 – use an html file in a browser window. The only way to get the errors is to look in the java (plug–in) console. For ease, you might consider getting it running in appletviewer before you try it in the browser.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!