how can i put a JButton on an image? - java

I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. I try to do it without layout because i want to put small buttons in some specific locations on the JFrame but every time i do it, the background image comes to the front or the JFrame has size equal to the JFrame size. With the following code, the JButton has the same size to JFrame. I have tried to change the size and location of the JButton but nothing. Can you help me please?
here is the code
public final class Test extends JComponent
{
private Image background;
private JFrame frame;
private Dimension dimension;
public Test()
{
dimension = new Dimension(15, 15);
frame = new JFrame("Iphone");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setBounds(641, 0, 344, 655);
frame.setVisible(true);
test = displayButton("tigka");
frame.getContentPane().add(test);
}
public void update(Graphics g)
{
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(background, 0, 25, null); // draw background
// label();
test = displayButton("test");
}
public JButton displayButton(String name)
{
JButton button = new JButton(name);
button.setSize(100, 100);
button.setPreferredSize(dimension);
return button;
}

You need to change the content pane to get a background for your Frame.
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Test");
frame.setContentPane(new JPanel() {
BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 300, 300, this);
}
});
frame.add(new JButton("Test Button"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
Output:

Have you tried using a JLabel with HTML in the label? Something like this:
import javax.swing.*;
public class SwingImage1
{
public static void main( String args[] )
{
JFrame frm = new JFrame( "Swing Image 1" );
JLabel lbl = new JLabel( "<html><body><img src=\"http://liv.liviutudor.com/images/liv.gif\"></body></html>" );
frm.getContentPane().add( lbl );
frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frm.pack();
frm.setVisible( true );
}
}
then on top of your label you can add your button?

You should swap those two lines:
super.paintComponents(g); //paints the children, like the button
g.drawImage(background, 0, 25, null); // draw background later possibly overwriting the button
Thus it should be this order:
g.drawImage(background, 0, 25, null);
super.paintComponents(g);
Additionally, note that the content pane's default layout is BorderLayout. Thus you'd set the layout of your content pane to null explicitly.

/*it is simple to put button on image first set image by making object then make button object & add the button object direct to image object rather then add to frame.*/
package frame;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class frame
{
public frame()
{
JFrame obj = new JFrame("Banking Software");
JButton b1 = new JButton("Opening Account");
JLabel image = new JLabel(new ImageIcon("money.jpg"));
image.setBounds(0,0, 1600, 1400);
obj.setExtendedState(JFrame.MAXIMIZED_BOTH);
obj.add(image);
b1.setBounds(500,400, 100, 40);
image.add(b1);
obj.setVisible(true);
}
public static void main(String args[])
{
new frame();
}
}

Related

How to add a background image in JPanel?

I did exactly according to the excellent guide but it does not work, I want to click a button that will change the background of the program. I would love to make the picture change
Code Guide https://stackhowto.com/how-to-set-background-image-in-java-swing/
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Display an image in the background");
final ImageIcon icon = new ImageIcon("background.png");
JTextArea text = new JTextArea()
{
Image img = icon.getImage();
// instance initializer
{setOpaque(false);}
public void paintComponent(Graphics graphics)
{
graphics.drawImage(img, 0, 0, this);
super.paintComponent(graphics);
}
};
JScrollPane pane = new JScrollPane(text);
Container content = frame.getContentPane();
content.add(pane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(3);
frame.setSize(400, 300);
frame.setVisible(true);
}
}

How to paint on JFrame

I have created this class which create a JFrame with a background picture. I am trying to paint a circle on that picture. But I can only show the picture or the figure, the circle will not show on the picture. I call the class from my main.
Sorry if this i a newbie question :)
package worldofzuul;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
/**
*
* #author JesperJørgensen
*/
public class GraphicsFrame extends JFrame {
private JPanel man = new JPanel();
void setupframe() {
// Here we create the Frame
JFrame frame = new JFrame(); // create the frame
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)
//Here we set the background image (the map which we walk in)
ImageIcon icon = new ImageIcon("src/Image/Kort.png");
frame.add(new JLabel(icon));
frame.setContentPane(new DrawPane());
frame.pack(); // sets the size of the frame to fit all objects inside.
frame.setVisible(true); // show the frame
}
class DrawPane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(20, 20, 100, 200);
}
}
}
See comments:
//always post an MCVE
//see http://stackoverflow.com/help/mcve
//include imports
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* #author JesperJørgensen
*/
public class GraphicsFrame extends JFrame {/*JFrame subclassing is never used*/
//This JPanel is never used
private JPanel man = new JPanel();
Image image;
void setupframe() {
// Here we create the Frame
JFrame frame = new JFrame(); // create the frame
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)
//initialize image
image = new ImageIcon("src/Image/Kort.png").getImage();
//frame.add(new JLabel(image));
frame.setContentPane(new DrawPane());
//if you don't use preferred sizes pack() will set frame to size 0.
//frame.pack(); // sets the size of the frame to fit all objects inside.
frame.setVisible(true); // show the frame
}
class DrawPane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
//add draw image to paint
g.drawImage(image,0, 0, null);
//this draws a rectangle. change to circle if desired
g.setColor(Color.red);
g.fillRect(20, 20, 100, 200);
}
}
//include main to make it an MCVE
public static void main(String args[]) {
new GraphicsFrame().setupframe();
}
}
To use the fact that this class is extending JFrame you may want to implement it like this:
public class GraphicsFrame extends JFrame {
Image image;
//introduce constructor
public GraphicsFrame() {
setupframe();
}
void setupframe() {
// no need to create a frame. This class is a JFrame
//JFrame frame = new JFrame(); // create the frame
setSize(500,500);
setLayout(new BorderLayout());
setResizable(false);
setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)
//initialize image
image = new ImageIcon("src/Image/Kort.png").getImage();
setContentPane(new DrawPane());
setVisible(true); // show the frame
}
class DrawPane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
//add draw image to paint
g.drawImage(image,0, 0, null);
//this draws a circle
g.setColor(Color.red);
g.drawOval(100, 100, 40, 40);
}
}
public static void main(String args[]) {
new GraphicsFrame();
}
}
Don't hesitate to ask for clarifications as needed.
setContentPane removes ImageIcon that so only visible element will be DrawPane

Text and JLabel

I have a JLabel which contains a picture and I set a text to this JLabel using the setText method. My problem is that I want to choose the position of the text I have just set. Do you have any ideas to perform that ?
//This the code i have
this.label = new JLabel ();
this.label.setText("My text");
this.label.setForeground(Color.BLACK);
this.add(label);
Can you try
public class LabelTextPos extends JLabel {
public static void main(String args[]) {
LabelTextPos label = new LabelTextPos();
JFrame frame = new JFrame();
frame.add(label);
frame.pack();
frame.setVisible(true);
}
#Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawString("Sample", 100, 100);
}
}

Putting a transparent text field over an image

I'm trying to write a program that will allow me to put text over an image, and save the edited image. Right now I'm getting an error that says:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
When I run the code it shows the text box, and a white background without my image. Any help with this would be appreciated. Right now i'm just focused on getting the text field over the image. Thank you in advance!
Here's the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.TreeSet;
public class Try1 extends JFrame {
public Try1() {
initializeUI();
}
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public void LoadImage() {
try {
img = ImageIO.read(new File("savedimage.jpg"));
}
catch (IOException e){}
}
private void initializeUI() {
JPanel panel = new JPanel(null);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField = new JTextField(20);
textField.setBounds(50, 50, 100, 20);
panel.add(textField);
setContentPane(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Try1().setVisible(true);
}
});
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new Try1());
f.pack();
f.setVisible(true);
}
}
A better approach in general can be seen in LabelRenderTest.
You only need to use HTML formatting in the label if multi-line text is required. Use plain text for a single line message.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class LabelRenderTest {
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
String title = "<html><body style='width: 200px; padding: 5px;'>"
+ "<h1>Do U C Me?</h1>"
+ "Here is a long string that will wrap. "
+ "The effect we want is a multi-line label.";
JFrame f = new JFrame("Label Render Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage image = new BufferedImage(
400,
300,
BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = image.createGraphics();
GradientPaint gp = new GradientPaint(
20f,
20f,
Color.red,
380f,
280f,
Color.orange);
imageGraphics.setPaint(gp);
imageGraphics.fillRect(0, 0, 400, 300);
JLabel textLabel = new JLabel(title);
textLabel.setSize(textLabel.getPreferredSize());
Dimension d = textLabel.getPreferredSize();
BufferedImage bi = new BufferedImage(
d.width,
d.height,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.setColor(new Color(255, 255, 255, 128));
g.fillRoundRect(
0,
0,
bi.getWidth(f),
bi.getHeight(f),
15,
10);
g.setColor(Color.black);
textLabel.paint(g);
Graphics g2 = image.getGraphics();
g2.drawImage(bi, 20, 20, f);
ImageIcon ii = new ImageIcon(image);
JLabel imageLabel = new JLabel(ii);
f.getContentPane().add(imageLabel);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
Following JFrame is useless. Because Try1 is itself a JFrame.
JFrame f = new JFrame("Load Image Sample");
basically just use Try1 instead of other Jframe.
f = new Try1();
f.pack();
f.setVisible(true);
But more importantly, you should not override paint, overide paintComponent instead. See Difference between paint() and paintcomponent()?.
This is your problem
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
This is not a window. It is a JFrame, so it is unwise to put a WindowAdapter on it
Your class is extending a JFrame, so take out the JFrame, and just do your
new Try1();
f.pack();
f.setVisible(true);

Adding a JPanel to a JLayeredPane causes paints and resizes to have no effect

I am trying to add a JPanel (well, several) to a JLayeredPane. However, when I do so, the paint component method of the JPanel seems to have no effect. An example is included below:
import javax.swing.*;
import java.awt.*;
public class Example {
public static void main(String[] args) {
// This Works as expected
JFrame usingPanel = new JFrame();
JPanel p = new JPanel();
p.add(new BluePanel());
usingPanel.setContentPane(p);
usingPanel.pack();
usingPanel.setVisible(true);
// This makes the frame but does not paint the BluePanel
JFrame usingLayer = new JFrame();
JLayeredPane l = new JLayeredPane();
l.setPreferredSize(new Dimension(200,200));
l.add(new BluePanel(), JLayeredPane.DEFAULT_LAYER);
JPanel p2 = new JPanel();
p2.add(l);
usingLayer.setContentPane(p2);
usingLayer.pack();
usingLayer.setVisible(true);
}
static class BluePanel extends JPanel{
public BluePanel(){
setPreferredSize(new Dimension(200,200));
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 200, 200);
}
}
}
Why is this? and what are the possible solutions?
JLayeredPane does not have a LayoutManager, so you need to set the location and size of your panels yourself.
See the tutorial
you hardcoded the size on the screen and have to change from
g.fillRect(0, 0, 200, 200);
to
g.fillRect(0, 0, getWidth(), getHeight());
(a minor change) add the method
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
and then remove of code line setPreferredSize(new Dimension(200,200));

Categories

Resources