New to Java, Starting a Project
Hi everyone, I'm quite new to Java and I am trying to make an app that can take an image from a source and relocate it to the app for display. So far, I have made a frame of " slots " for where the picture should go to, but I can't seem to put anything on top of my slot icons, not even a checkbox, let alone get another image to sit on top of it. I'm using Netbeans to try to create my app.
If anybody could give me some tips of how to " layer " functions over each other, or any other advice at all, that would be just super !
Thanks a bunch
Re: New to Java, Starting a Project
Please go through the Forum Rules, particularly the third paragraph.
As your question stands, we have no idea which GUI toolkit you are using, what kind of "source" the image is obtained from, or where exactly you're stuck. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
Note: if you happen to be trying out the NetBeans visual designer, drop it. It's categorically not a beginners' tool. Whatever some tutorials would have you believe, coding a simple GUI manually is far, far easier.
db
Re: New to Java, Starting a Project
I worked on making an image program like you said, with images that could sit on top of images.
I came up with the following class to display an image:
Code:
package us.kaydell.graphics;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private ImageIcon imageIcon;
public ImagePanel(int x, int y, ImageIcon imageIcon) {
this.imageIcon = imageIcon;
int width = imageIcon.getIconWidth();
int height = imageIcon.getIconHeight();
setBounds(x, y, width, height);
}
@Override
public void paint(Graphics g) {
if (imageIcon != null) {
imageIcon.paintIcon(this, g, getX(), getY());
}
}
}
And, I came up with the following class to test it with. It partly works, but it is a work in progress. What makes the images able to sit on top of each other is setting the layout manger to null so that a layout manager isn't used.
Code:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import us.kaydell.graphics.ImagePanel;
public class ImagePanelTest extends JFrame {
public ImagePanelTest(String fileName1, String fileName2) {
// by using null as the layout manager, images can be on top of each other
setLayout(null);
// create two image pannel objects
ImagePanel image1 = new ImagePanel(0,0,new ImageIcon(fileName1));
ImagePanel image2 = new ImagePanel(0,0,new ImageIcon(fileName2));
/// add each image to this frame
add(image1);
add(image2);
// setup other attributes of this frame to make is ready to display images
setSize(500,500);
setLocationRelativeTo(null); // center this frame on the screen
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ImagePanelTest("picture1.jpg", "picture2.jpg");
}
});
}
}
If anybody has a better solution, please let me know.
Re: New to Java, Starting a Project
Wow thank you so much ! I'll give this a go and it should really help!!