How to paint on JFrame - java

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

Related

Cannot change the background color of JFrame ContentPane

So I have a snake program in java, works perfectly, however in my Frame class I cannot change the background color of my JFrame's content pane, I use getContentPane().setBackground(Color.DARK_GRAY); but it is not working, any help ?
Here is my Frame class:
package mainpackage;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
public Frame() {
getContentPane().setBackground(Color.BLACK); \\NOT WORKING !!
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Snake by Sarp~");
setResizable(false);
init();
}
public void init() {
setLayout(new GridLayout(1, 1, 0, 0));
Screen s = new Screen();
add(s);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Frame();
}
}
setLayout(new GridLayout(1, 1, 0, 0));
With the above layout manager, any component(s) you add to the frame will completely cover the content pane.
Screen s = new Screen();
add(s);
You may set the background of the content pane, but then you add a component to the content pane. So you will see the color of the Screen component over top of the content pane.
Set the color of your Screen object to be whatever you want:
s.setBackground( Color.BLACK );

Set an image as background to JFrame

I am making a small app, but i want to set an image as background at the whole window. I tried to make this like below but nothing happen. The image is in the folder where the class is so as a path I put only the name...Can you help me please? what can I do?
Container c = getContentPane();
setContentPane(c);
setContentPane(new JLabel(new ImageIcon("Chrysanthemum.jpg")));
One possibility is to add a BorderLayout to the JFrame, which should fill the JFrame with the JLabel, then set the background, adding the JLabel to the frame and then add components to it, like this:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Foo extends JFrame {
public Foo() {
setLayout(new BorderLayout());
JLabel background = new JLabel(new ImageIcon("Untitled.png"));
add(background);
background.setLayout(new FlowLayout());
background.add(new JButton("foo"));
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
Foo foo = new Foo();
}
}
The above works for me, with the JButton at the top center of the 500 by 500 JFrame with the specified background.
What I would do is create a JPanel with a background image, and add it to the JFrame. I already have a BackgroundPanel class right in one of my projects, and this is the setup I have for it.
public class MyFrame extends JFrame {
private BackgroundPanel bgPanel;
public MyFrame() {
bgPanel = new BackgroundPanel("Chrysanthemum.jpg");
setTitle("MyFrame");
setResizable(false);
setContentPane(bgPanel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
// -- BackgroundPanel class
public class BackgroundPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Image bg;
public BackgroundPanel(String path) {
this(Images.load(path).getImage());
}
public BackgroundPanel(Image img) {
this.bg = img;
setPreferredSize(new Dimension(bg.getWidth(null), bg.getHeight(null)));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
}
}

JFrame and Rectangle

Can anyone tell me why the rectangle is not showing up on the frame?
I only see a button on the frame. Please help.
I tried to using the paint method for drawing the rectangle.
Should I use paintComponent() or just paint()?
public class GUI2 {
public static void main(String[] args) {
JFrame frame = new JFrame("Game");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(500, 200);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("YO");
panel.add(button);
button.addActionListener(new Action());
frame.paint(null);
}
public void paint(Graphics g) {
g.drawRect(250, 250, 200, 100);
}
static class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
}
You shouldn't have to explicitly call paint
get rid of the paint method
Make an Inner JPanel class
Yes override paintComponent in the JPanel class
Call super.paintComponent in the paintComponent method.
Add the class JPanel to the JFrame
Don't do everything inside the main, as you'll find out, that static will cause a problem for you. Do everything inside a constructor
Run the program from the EDT SwingUtilitites.invokeLater().
Make the button a global variable so it can be accessed from the ActionListener
setVisible should be the last thing you do, after adding all the component.
When adding multiple components the JFrame you will want to use the BorderLayout positions, or set the layout to the JFrame to something else besides BorderLayout
Override getPrefferedSize in your JPanel when painting, so the JPanel has a respected preferred size.
Don't set the size of the JFrame just call pack();
Here is a refactor of your code
Also see Creating GUI with Swing | Graphics2D | Performing Custom Paintin for further details.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GUI2 {
JButton button = new JButton("YO");
public GUI2() {
button.addActionListener(new Action());
JFrame frame = new JFrame("Game");
frame.add(new DrawPanel(), BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(250, 250, 200, 100);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GUI2();
}
});
}
static class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
}
I can tell you why you'll get a NullPointerException
You've not overridden any paint method of any displayable component
You've not passed a valid Graphics context to you paint method, but I would discourage this any way
You should make use of the #Override annotation which would have prevented the class from compiling. Use it when you think you're overriding a method of a parent class, it will tell you when you're wrong
Start by taking a look at Performing Custom Painting and then take a look at Painting in AWT and Swing for more details about how painting is actually done in Swing
Here is working example of your code:
package test;
import javax.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.awt.*;
import java.text.AttributedCharacterIterator;
public class GUI2 extends JPanel{
JButton button;
JFrame frame;
public GUI2() {
button = new JButton("YO");
//panel = new JPanel();
frame = new JFrame();
//panel = new JPanel();
this.add(button);
frame.add(this);
//button.addActionListener(new Action());
// this.paint(null);
frame.setSize(500, 500);
frame.setLocation(500, 200);
frame.setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect(250, 250, 200, 100);
}
public static void main(String[] args) {
GUI2 test = new GUI2();
}
}
I have removed some statements but you can add them later

Java - Graphics - adding another shape on JPanel

I've got a class that makes a JFrame and adds a panel on it
and the second one extends the JPanel and paints on it
The first one (JFrame)
class MyWindow {
void qwe() {
JFrame frame = new JFrame("qwe");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
panel.setLayout(null);
frame.add(panel);}}
and the second one (JPanel)
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
g.drawRect(50,50,90,70);
}
public void addShape() {
Graphics g = this.getGraphics();
Graphics2D gg = (Graphics2D) g;
gg.drawString("qwe",20,20);}}
how can i add a String on the JPanel by using the addShape() method ?
As a concrete example of #camickr's point, note that MyPanel already override's paintComponent(), so you can pass a reference to the Graphics context to addShape(). Additionally,
Be sure to invoke super.paintComponent(g).
Override getPreferredSize() to establish the component's preferred size.
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyWindow {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MyWindow().qwe();
}
});
}
void qwe() {
JFrame frame = new JFrame("qwe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
panel.setLayout(null);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static class MyPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(50, 50, 90, 70);
addShape(g);
}
public void addShape(Graphics g) {
g.drawString("qwe", 20, 20);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
}
}
Don't use the getGraphics() method of your component to do custom painting. This type of painting is only temporary and will be lost the next time Swing determines the component needs to be painted.
Custom painting should always be done in the paintComponent() method of your component.
See Custom Painting Approaches for the two commons was to do what you want.

how can i put a JButton on an image?

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();
}
}

Categories

Resources