Displaying image over image Java












-1















How do I display an image over another image on either applet or jframe?



I know for one image in applet is this:



Image Bendy;
Bendy = getImage(getDocumentBase(), "Bendy Icon.PNG");
g.drawImage(Bendy, 20, 20, this);


And for jframe:



ImageIcon kh = new ImageIcon("KH3.gif");
JLabel lab = new JLabel(kh);
add(lab);


So how do I add an image over the other for either one?










share|improve this question


















  • 2





    Why were applets deprecated in JDK 9?; Oracle reveals Java Applet API deprecation plan; Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets

    – MadProgrammer
    Nov 22 '18 at 4:29











  • In short, Applet's are dead, long live HTML 5 ... or something

    – MadProgrammer
    Nov 22 '18 at 4:29






  • 2





    You do custom painting by overriding the paintComponent() method of a JPanel. Read the section from the Swing tutorial on Custom Painting for more information and working example. Then you can use the Graphics.drawImage(...) method.

    – camickr
    Nov 22 '18 at 4:29













  • Hey @DigiLei , did my answer help you solve your problem?

    – PradyumanDixit
    Nov 22 '18 at 16:50











  • Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly.

    – DigiLei
    Nov 22 '18 at 17:50


















-1















How do I display an image over another image on either applet or jframe?



I know for one image in applet is this:



Image Bendy;
Bendy = getImage(getDocumentBase(), "Bendy Icon.PNG");
g.drawImage(Bendy, 20, 20, this);


And for jframe:



ImageIcon kh = new ImageIcon("KH3.gif");
JLabel lab = new JLabel(kh);
add(lab);


So how do I add an image over the other for either one?










share|improve this question


















  • 2





    Why were applets deprecated in JDK 9?; Oracle reveals Java Applet API deprecation plan; Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets

    – MadProgrammer
    Nov 22 '18 at 4:29











  • In short, Applet's are dead, long live HTML 5 ... or something

    – MadProgrammer
    Nov 22 '18 at 4:29






  • 2





    You do custom painting by overriding the paintComponent() method of a JPanel. Read the section from the Swing tutorial on Custom Painting for more information and working example. Then you can use the Graphics.drawImage(...) method.

    – camickr
    Nov 22 '18 at 4:29













  • Hey @DigiLei , did my answer help you solve your problem?

    – PradyumanDixit
    Nov 22 '18 at 16:50











  • Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly.

    – DigiLei
    Nov 22 '18 at 17:50
















-1












-1








-1








How do I display an image over another image on either applet or jframe?



I know for one image in applet is this:



Image Bendy;
Bendy = getImage(getDocumentBase(), "Bendy Icon.PNG");
g.drawImage(Bendy, 20, 20, this);


And for jframe:



ImageIcon kh = new ImageIcon("KH3.gif");
JLabel lab = new JLabel(kh);
add(lab);


So how do I add an image over the other for either one?










share|improve this question














How do I display an image over another image on either applet or jframe?



I know for one image in applet is this:



Image Bendy;
Bendy = getImage(getDocumentBase(), "Bendy Icon.PNG");
g.drawImage(Bendy, 20, 20, this);


And for jframe:



ImageIcon kh = new ImageIcon("KH3.gif");
JLabel lab = new JLabel(kh);
add(lab);


So how do I add an image over the other for either one?







java image jframe applet






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 4:26









DigiLeiDigiLei

23




23








  • 2





    Why were applets deprecated in JDK 9?; Oracle reveals Java Applet API deprecation plan; Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets

    – MadProgrammer
    Nov 22 '18 at 4:29











  • In short, Applet's are dead, long live HTML 5 ... or something

    – MadProgrammer
    Nov 22 '18 at 4:29






  • 2





    You do custom painting by overriding the paintComponent() method of a JPanel. Read the section from the Swing tutorial on Custom Painting for more information and working example. Then you can use the Graphics.drawImage(...) method.

    – camickr
    Nov 22 '18 at 4:29













  • Hey @DigiLei , did my answer help you solve your problem?

    – PradyumanDixit
    Nov 22 '18 at 16:50











  • Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly.

    – DigiLei
    Nov 22 '18 at 17:50
















  • 2





    Why were applets deprecated in JDK 9?; Oracle reveals Java Applet API deprecation plan; Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets

    – MadProgrammer
    Nov 22 '18 at 4:29











  • In short, Applet's are dead, long live HTML 5 ... or something

    – MadProgrammer
    Nov 22 '18 at 4:29






  • 2





    You do custom painting by overriding the paintComponent() method of a JPanel. Read the section from the Swing tutorial on Custom Painting for more information and working example. Then you can use the Graphics.drawImage(...) method.

    – camickr
    Nov 22 '18 at 4:29













  • Hey @DigiLei , did my answer help you solve your problem?

    – PradyumanDixit
    Nov 22 '18 at 16:50











  • Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly.

    – DigiLei
    Nov 22 '18 at 17:50










2




2





Why were applets deprecated in JDK 9?; Oracle reveals Java Applet API deprecation plan; Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets

– MadProgrammer
Nov 22 '18 at 4:29





Why were applets deprecated in JDK 9?; Oracle reveals Java Applet API deprecation plan; Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets

– MadProgrammer
Nov 22 '18 at 4:29













In short, Applet's are dead, long live HTML 5 ... or something

– MadProgrammer
Nov 22 '18 at 4:29





In short, Applet's are dead, long live HTML 5 ... or something

– MadProgrammer
Nov 22 '18 at 4:29




2




2





You do custom painting by overriding the paintComponent() method of a JPanel. Read the section from the Swing tutorial on Custom Painting for more information and working example. Then you can use the Graphics.drawImage(...) method.

– camickr
Nov 22 '18 at 4:29







You do custom painting by overriding the paintComponent() method of a JPanel. Read the section from the Swing tutorial on Custom Painting for more information and working example. Then you can use the Graphics.drawImage(...) method.

– camickr
Nov 22 '18 at 4:29















Hey @DigiLei , did my answer help you solve your problem?

– PradyumanDixit
Nov 22 '18 at 16:50





Hey @DigiLei , did my answer help you solve your problem?

– PradyumanDixit
Nov 22 '18 at 16:50













Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly.

– DigiLei
Nov 22 '18 at 17:50







Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly.

– DigiLei
Nov 22 '18 at 17:50














2 Answers
2






active

oldest

votes


















0














As mentioned in comments, (Java) Applets are indeed dead and not worth pursuing. The ImagePanel in this answer extends JPanel and could theoretically be displayed in any Java Swing based top-level container (e.g. JFrame, JWindow, JOptionPane..) or in another container like a JPanel.



enter image description here



import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
import javax.imageio.ImageIO;
import java.net.URL;

public class StackedImages {

private JComponent ui = null;
String path1 = "https://i.stack.imgur.com/F0JHK.png";
String path2 = "https://i.stack.imgur.com/gJmeJ.png";

class ImagePanel extends JPanel {

private final BufferedImage images;
private final Random rand = new Random();
private final Dimension prefeSize = new Dimension(400, 400);

ImagePanel(BufferedImage images) {
this.images = images;
setBackground(Color.WHITE);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

for (int ii = 0; ii < 200; ii++) {
int x = rand.nextInt(prefeSize.width);
int y = rand.nextInt(prefeSize.height);
int ind = rand.nextInt(images.length);
g.drawImage(images[ind], x, y, this);
}
}

@Override
public Dimension getPreferredSize() {
return prefeSize;
}
}

StackedImages() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public final void initUI() throws Exception {
if (ui != null) {
return;
}

ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));

BufferedImage bi = new BufferedImage[2];
bi[0] = ImageIO.read(new URL(path1));
bi[1] = ImageIO.read(new URL(path2));
ui.add(new ImagePanel(bi));
}

public JComponent getUI() {
return ui;
}

public static void main(String args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
StackedImages o = new StackedImages();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}





share|improve this answer
























  • Please accept the answer if it helped solve the problem.

    – Andrew Thompson
    Nov 25 '18 at 6:31



















-1














As said by people in comments, Applets are dead, you don't actually need them to learn GUI programs.



But still, if you want there are some ways you can do so, like using drawImage() method. Maybe something like this:



try
{
BufferedImage background = ImageIO.read(new File("..."));
BufferedImage logo = ImageIO.read(new File("..."));

Graphics g = background.getGraphics();
g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
e.printStackTrace();
}





share|improve this answer
























  • 1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

    – Andrew Thompson
    Nov 23 '18 at 7:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423879%2fdisplaying-image-over-image-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As mentioned in comments, (Java) Applets are indeed dead and not worth pursuing. The ImagePanel in this answer extends JPanel and could theoretically be displayed in any Java Swing based top-level container (e.g. JFrame, JWindow, JOptionPane..) or in another container like a JPanel.



enter image description here



import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
import javax.imageio.ImageIO;
import java.net.URL;

public class StackedImages {

private JComponent ui = null;
String path1 = "https://i.stack.imgur.com/F0JHK.png";
String path2 = "https://i.stack.imgur.com/gJmeJ.png";

class ImagePanel extends JPanel {

private final BufferedImage images;
private final Random rand = new Random();
private final Dimension prefeSize = new Dimension(400, 400);

ImagePanel(BufferedImage images) {
this.images = images;
setBackground(Color.WHITE);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

for (int ii = 0; ii < 200; ii++) {
int x = rand.nextInt(prefeSize.width);
int y = rand.nextInt(prefeSize.height);
int ind = rand.nextInt(images.length);
g.drawImage(images[ind], x, y, this);
}
}

@Override
public Dimension getPreferredSize() {
return prefeSize;
}
}

StackedImages() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public final void initUI() throws Exception {
if (ui != null) {
return;
}

ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));

BufferedImage bi = new BufferedImage[2];
bi[0] = ImageIO.read(new URL(path1));
bi[1] = ImageIO.read(new URL(path2));
ui.add(new ImagePanel(bi));
}

public JComponent getUI() {
return ui;
}

public static void main(String args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
StackedImages o = new StackedImages();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}





share|improve this answer
























  • Please accept the answer if it helped solve the problem.

    – Andrew Thompson
    Nov 25 '18 at 6:31
















0














As mentioned in comments, (Java) Applets are indeed dead and not worth pursuing. The ImagePanel in this answer extends JPanel and could theoretically be displayed in any Java Swing based top-level container (e.g. JFrame, JWindow, JOptionPane..) or in another container like a JPanel.



enter image description here



import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
import javax.imageio.ImageIO;
import java.net.URL;

public class StackedImages {

private JComponent ui = null;
String path1 = "https://i.stack.imgur.com/F0JHK.png";
String path2 = "https://i.stack.imgur.com/gJmeJ.png";

class ImagePanel extends JPanel {

private final BufferedImage images;
private final Random rand = new Random();
private final Dimension prefeSize = new Dimension(400, 400);

ImagePanel(BufferedImage images) {
this.images = images;
setBackground(Color.WHITE);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

for (int ii = 0; ii < 200; ii++) {
int x = rand.nextInt(prefeSize.width);
int y = rand.nextInt(prefeSize.height);
int ind = rand.nextInt(images.length);
g.drawImage(images[ind], x, y, this);
}
}

@Override
public Dimension getPreferredSize() {
return prefeSize;
}
}

StackedImages() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public final void initUI() throws Exception {
if (ui != null) {
return;
}

ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));

BufferedImage bi = new BufferedImage[2];
bi[0] = ImageIO.read(new URL(path1));
bi[1] = ImageIO.read(new URL(path2));
ui.add(new ImagePanel(bi));
}

public JComponent getUI() {
return ui;
}

public static void main(String args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
StackedImages o = new StackedImages();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}





share|improve this answer
























  • Please accept the answer if it helped solve the problem.

    – Andrew Thompson
    Nov 25 '18 at 6:31














0












0








0







As mentioned in comments, (Java) Applets are indeed dead and not worth pursuing. The ImagePanel in this answer extends JPanel and could theoretically be displayed in any Java Swing based top-level container (e.g. JFrame, JWindow, JOptionPane..) or in another container like a JPanel.



enter image description here



import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
import javax.imageio.ImageIO;
import java.net.URL;

public class StackedImages {

private JComponent ui = null;
String path1 = "https://i.stack.imgur.com/F0JHK.png";
String path2 = "https://i.stack.imgur.com/gJmeJ.png";

class ImagePanel extends JPanel {

private final BufferedImage images;
private final Random rand = new Random();
private final Dimension prefeSize = new Dimension(400, 400);

ImagePanel(BufferedImage images) {
this.images = images;
setBackground(Color.WHITE);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

for (int ii = 0; ii < 200; ii++) {
int x = rand.nextInt(prefeSize.width);
int y = rand.nextInt(prefeSize.height);
int ind = rand.nextInt(images.length);
g.drawImage(images[ind], x, y, this);
}
}

@Override
public Dimension getPreferredSize() {
return prefeSize;
}
}

StackedImages() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public final void initUI() throws Exception {
if (ui != null) {
return;
}

ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));

BufferedImage bi = new BufferedImage[2];
bi[0] = ImageIO.read(new URL(path1));
bi[1] = ImageIO.read(new URL(path2));
ui.add(new ImagePanel(bi));
}

public JComponent getUI() {
return ui;
}

public static void main(String args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
StackedImages o = new StackedImages();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}





share|improve this answer













As mentioned in comments, (Java) Applets are indeed dead and not worth pursuing. The ImagePanel in this answer extends JPanel and could theoretically be displayed in any Java Swing based top-level container (e.g. JFrame, JWindow, JOptionPane..) or in another container like a JPanel.



enter image description here



import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
import javax.imageio.ImageIO;
import java.net.URL;

public class StackedImages {

private JComponent ui = null;
String path1 = "https://i.stack.imgur.com/F0JHK.png";
String path2 = "https://i.stack.imgur.com/gJmeJ.png";

class ImagePanel extends JPanel {

private final BufferedImage images;
private final Random rand = new Random();
private final Dimension prefeSize = new Dimension(400, 400);

ImagePanel(BufferedImage images) {
this.images = images;
setBackground(Color.WHITE);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

for (int ii = 0; ii < 200; ii++) {
int x = rand.nextInt(prefeSize.width);
int y = rand.nextInt(prefeSize.height);
int ind = rand.nextInt(images.length);
g.drawImage(images[ind], x, y, this);
}
}

@Override
public Dimension getPreferredSize() {
return prefeSize;
}
}

StackedImages() {
try {
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public final void initUI() throws Exception {
if (ui != null) {
return;
}

ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));

BufferedImage bi = new BufferedImage[2];
bi[0] = ImageIO.read(new URL(path1));
bi[1] = ImageIO.read(new URL(path2));
ui.add(new ImagePanel(bi));
}

public JComponent getUI() {
return ui;
}

public static void main(String args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
StackedImages o = new StackedImages();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 8:54









Andrew ThompsonAndrew Thompson

153k28162344




153k28162344













  • Please accept the answer if it helped solve the problem.

    – Andrew Thompson
    Nov 25 '18 at 6:31



















  • Please accept the answer if it helped solve the problem.

    – Andrew Thompson
    Nov 25 '18 at 6:31

















Please accept the answer if it helped solve the problem.

– Andrew Thompson
Nov 25 '18 at 6:31





Please accept the answer if it helped solve the problem.

– Andrew Thompson
Nov 25 '18 at 6:31













-1














As said by people in comments, Applets are dead, you don't actually need them to learn GUI programs.



But still, if you want there are some ways you can do so, like using drawImage() method. Maybe something like this:



try
{
BufferedImage background = ImageIO.read(new File("..."));
BufferedImage logo = ImageIO.read(new File("..."));

Graphics g = background.getGraphics();
g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
e.printStackTrace();
}





share|improve this answer
























  • 1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

    – Andrew Thompson
    Nov 23 '18 at 7:30
















-1














As said by people in comments, Applets are dead, you don't actually need them to learn GUI programs.



But still, if you want there are some ways you can do so, like using drawImage() method. Maybe something like this:



try
{
BufferedImage background = ImageIO.read(new File("..."));
BufferedImage logo = ImageIO.read(new File("..."));

Graphics g = background.getGraphics();
g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
e.printStackTrace();
}





share|improve this answer
























  • 1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

    – Andrew Thompson
    Nov 23 '18 at 7:30














-1












-1








-1







As said by people in comments, Applets are dead, you don't actually need them to learn GUI programs.



But still, if you want there are some ways you can do so, like using drawImage() method. Maybe something like this:



try
{
BufferedImage background = ImageIO.read(new File("..."));
BufferedImage logo = ImageIO.read(new File("..."));

Graphics g = background.getGraphics();
g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
e.printStackTrace();
}





share|improve this answer













As said by people in comments, Applets are dead, you don't actually need them to learn GUI programs.



But still, if you want there are some ways you can do so, like using drawImage() method. Maybe something like this:



try
{
BufferedImage background = ImageIO.read(new File("..."));
BufferedImage logo = ImageIO.read(new File("..."));

Graphics g = background.getGraphics();
g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
e.printStackTrace();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 4:32









PradyumanDixitPradyumanDixit

2,1772819




2,1772819













  • 1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

    – Andrew Thompson
    Nov 23 '18 at 7:30



















  • 1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

    – Andrew Thompson
    Nov 23 '18 at 7:30

















1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

– Andrew Thompson
Nov 23 '18 at 7:30





1) BufferedImage background = ImageIO.read(new File("...")); Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) Graphics g = background.getGraphics(); This is not the way to approach custom painting. See camickr's comment for the correct way. 3) g.drawImage(logo, 0, 0, null); Every JComponent is an image observer. So that should be g.drawImage(logo, 0, 0, this); 4) "Maybe something like this:" Except .. completely different.

– Andrew Thompson
Nov 23 '18 at 7:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423879%2fdisplaying-image-over-image-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory