Remove JFrame in Java - java

Fist of all, I knew that this question has been asked before (JFrame to image without showing the JFrame).
However, due to the poor coding of mine, I would like to ask help from others about how to remove the JFrame out from my coding as shown below:
package com.example.ImageScreen;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
//public class ImageScreen extends JPanel{
public class ImageScreen extends JPanel{
private static final long serialVersionUID = 1L;
private BufferedImage image;
public ImageScreen() {
setSize(600,600);
// setMinimumSize(new Dimension(250,250));
try {
//Load the image
image = ImageIO.read(new File("C:/Users/User/Downloads/Geoffs_Picture_Overlay_App/crosshair.gif"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
//Paint it on screen
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
g2d.dispose();
}
public static void main(String[] args) {
JFrame window = new JFrame("An Image On Screen");
window.add(new ImageScreen());
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
// window.setAlwaysOnTop(true);
window.setBackground(Color.CYAN);
window.setUndecorated(true);
// window.dispose();
}
}

Use - window.setVisible(false)

Use a JLabel to display an image. There is no need to do custom painting when you are painting the image at its actual size.
The setUndecorated(...) method should be invoked before you pack() and make the frame visible.

Related

paintComponent() not being called

Here is a little program that should (in theory) draw an image of a ball on screen.
The problem is that paintComponent seems to not get called. The program consists of two classes.
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ScreenSaver extends JPanel {
private static final long serialVersionUID = 001;
public static void main(String[] args) {
new ScreenSaver();
}
public ScreenSaver() {
new Window(1600, 900, "ScreenSaver", this);
}
//----------------------------------------------------------------------------------
private static BufferedImage ball;
public static BufferedImage getBallSprite() {
try {
File pathToBall = new File("ball.png");
ball = ImageIO.read(pathToBall);
} catch (IOException ex) {
ex.printStackTrace();
}
return ball;
}
}
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Window extends Canvas {
private static final long serialVersionUID = 002;
public Window(int width, int height, String title, ScreenSaver ScreenSaver) {
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
repaint();
}
public void paintComponent(Graphics g) {
System.out.println("Painting...");
BufferedImage ball = ScreenSaver.getBallSprite();
g.drawImage(ball, 0, 0, 100, 100, this);
}
}
As you can see, I tested if paintComponent was called using a console message. Sadly this was not the case. Can someone explain?
java.awt.Canvas does not inherit from JComponent so paintComponent won't be called automatically. You can create a new custom window instead to create a Swing-centric component
public class MyWindow extends JComponent {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
It's no wonder that paintComponent is not called, because Canvas has no implementation of paintComponent which you can override. With a canvas you have to overwrite paint for your purposes. In your code you use both a JPanel and a Canvas, which is not necessary at all. Use either of the two.
The following is an example with a Canvas:
import java.awt.*;
import javax.swing.*;
public class ScreenSaver extends Canvas{
public static void main(String[] args) {
JFrame window = new JFrame("Screensaver");
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setResizable(false);
ScreenSaver canvas = new ScreenSaver();
canvas.setPreferredSize(new Dimension(1600, 900));
canvas.setBackground(Color.BLACK);
window.add(canvas);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
#Override
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillOval(100, 100, 100, 100);
}
}
The annotation Override above the method to be overwritten ensures that the compiler can issue a warning message if the overwritten method does not exist or there is a typo. I hope this helps you further.

Image is not being confined to a direct area

Image is not being confined to a direct area in this java code below. I want the java image to be displayed in its entirety in a 400 width by 400 height image. I tried to do that by frame.setSize(400, 400); and it is not working. My code includes drawImage which is what I was told I had to put in the code for this to work. I don't what to do next.
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SwingSandbox {
public static void main(String[] args) throws IOException {
JFrame frame = buildFrame();
final BufferedImage image = ImageIO.read(new File("/Users/johnzalubski/Desktop/c.jpg"));
JPanel pane = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
frame.add(pane);
}
private static JFrame buildFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
return frame;
}
}
I reworked your Swing sandbox code a bit. Here's what I came up with.
The image is distorted because I made it fit a 400 x 400 drawing panel. You should reduce the image and maintain the aspect ratio.
Here are the changes I made.
I added the call to the SwingUtilities invokeLater method to put the Swing components on the Event Dispatch Thread.
I made the drawing panel a class, so I could set the preferred size. You set the size of the drawing panel, not the JFrame. Who cares how large or small the JFrame is?
I put the JFrame method calls in the correct order.
And here's the code.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class SwingSandbox implements Runnable {
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new SwingSandbox());
}
private BufferedImage image;
public SwingSandbox() {
try {
image = ImageIO.read(new File("C:\\Users\\Owner\\OneDrive\\Pictures\\Saved Pictures\\StockMarketGame.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
JFrame frame = new JFrame("Image Display");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel(image);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage image;
public DrawingPanel(BufferedImage image) {
this.image = image;
this.setPreferredSize(new Dimension(400, 400));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 400, 400, null);
}
}
}
You are changing how big is frame. You should use something like this: g.drawImage(image, 0, 0, 400, 400, null);
If this help you please vote me up. I’am here new and want to have some basic reputation. Thanks! 😀

Displaying an image over another

Currently I am having an issue whereby the Robot.png is replacing the image of my gameboard.png . I want to make it so that the robot .png is ontop of the board and be able to move the robot around the board.
Board.Java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Board extends JPanel {
private Image gameboard;
public Board(){
initBoard();
}
private void initBoard(){
loadImage();
int w = gameboard.getWidth(this);
int h = gameboard.getHeight(this);
setPreferredSize(new Dimension(w,h));
}
private void loadImage(){
ImageIcon i = new ImageIcon("res/gameboard.png");
gameboard = i.getImage();
}
#Override
public void paintComponent(Graphics g){
g.drawImage(gameboard,0,0,null);
}
}
Player.Java
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Player extends JPanel {
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
ImageIcon ic = new ImageIcon("res/Robot.png");
Image image1 = ic.getImage();
g2d.drawImage(image1, 100, 100, null);
}
}
GameGUI.Java
import javax.swing.JFrame;
public class GameGui extends JFrame {
public GameGui(){
initGui();
}
public void initGui(){
add(new Board());
add(new Player());
setTitle("11+ Game");
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(1240,620);
}
}
You don't need to reload the robot image every time. You should use ImageIO to load it, and save it in a member variable.
Otherwise, the way to not have the image overwrite the whole background image is to use height and width parameters with your drawImage call
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
ImageIcon ic = new ImageIcon("res/Robot.png");
Image image1 = ic.getImage();
int width = ..., height = ...;
g2d.drawImage(image1, 100, 100, width, height, null);
}

How do I draw an image to a JPanel or JFrame?

How do I draw an Image to a JPanel or JFrame, I have already read oracle's tutorial on this but I can't seem to get it right. I need the image "BeachRoad.png" to be displayed on a specific set of coordinates. Here is what I have so far.
public class Level1 extends JFrame implements ActionListener {
static JLayeredPane EverythingButPlayer;
static Level1 l1;
public Level1() {
EverythingButPlayer = new JLayeredPane();
BufferedImage img = null;
try {
img = ImageIO.read(new File("BeachRoad.png"));
} catch (IOException e) {
}
Graphics g = img.getGraphics();
g.drawImage(img,0, 0, EverythingButPlayer);
this.add(EverythingButPlayer);
}
And in the Main(),
l1 = new Level1();
l1.setTitle("poop");
l1.setSize(1920, 1080);
l1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1.setVisible(true);
Thanks in advance!
Try this:
package com.sandbox;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SwingSandbox {
public static void main(String[] args) throws IOException {
JFrame frame = buildFrame();
final BufferedImage image = ImageIO.read(new File("C:\\Projects\\MavenSandbox\\src\\main\\resources\\img.jpg"));
JPanel pane = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
frame.add(pane);
}
private static JFrame buildFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
return frame;
}
}
There are a lot of methods, but I always override the paint(Graphics g) of a JComponent and use g.drawImage(...)
edit: I was making a sample, but Daniel Kaplan did it perfectly, look at his answer :)

I need to set a background image for a java DesktopApplication

I didn't know how, and there is no background image property. I researched the answer but all I could find was to set a labels icon inside a panel with a null layout. This worked and my image is there, but it is covering all but a single text field. Can I change the Z value of this label? I do not see a 'move to back' option, if there is one.
This should solve your problem:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestImage {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ContentPane panel = new ContentPane();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
private static class ContentPane extends JPanel {
BufferedImage image = null;
public ContentPane() {
try {
String pathToImage = "C:/Users/Branislav/Pictures/sun.jpg";
image = ImageIO.read(new File(pathToImage));
} catch (IOException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
};
#Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
}
}
Basically, I set image on JPanel (ContentPane). Also, size of your JPanel depends on size of image (at least in this case).
Regards.

Categories