Images and Panels - java

I'm having a problem adding a JPanel on top of an Image. This is what I'm trying to do:
Image bgImage = loadImage(filename);
JPanel jp = new JPanel();
jp.setBounds(100,100,100,100);
jp.setOpaque(true);
jp.setBackgroudColor(Color.red);
bgImage.add(jp);
After doing this, I only see the bgImage. I tried everything but I still can't show the panel. Can somebody help me?

You cannot place a component inside an Image. What you want to do is paint the Image onto the background of a swing component (like JPanel). All swing components have a paint() method that calls these three methods (perhaps not quite this order): paintComponent(), paintChildren(), paintBorder(). So, you want to override the paintComponent() method to paint your background image over the panel. When this runs, your custom method will be called, and then the paintChildren() method will be called, which will paint all "child" components over the top of your background image:
class BackgroundImagePanel extends JPanel {
public void setBackgroundImage(Image backgroundImage) {
this.backgroundImage = backgroundImage;
}
#Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
graphics.drawImage(backgroundImage, 0, 0, this);
}
private Image backgroundImage;
}
BackgroundImagePanel panel = new BackgroundImagePanel();
panel.setBackgroundImage(image);
panel.add(new JTextField("Enter text here..."));
panel.add(new JButton("Press Me"));

The "BackgroundImagePanel" solution paints the image at its actual size. If this is a requirement, then you can just use a JLabel instead of creating a custom component.
The BackgroundPanel entry shows how you can do this. It also provides a background panel with more custom image painting solutions, that will allow you to scale and tile the image, if this is part of your requirement.

Related

How add a jtextfield to a jpanel that has paint component?

I'm making a game and I want to add a JTextField to a JPanel that has Paint Component. I repaint the JPanel every 16 milliseconds.
I add() the textfield to the panel but it show up only for a single frame when i click on it.
Then I tried to repaint() the textfield but now it is flashing.
public class Screen extends JPanel {
public Screen() {
JTextField txt = new JTextField();
txt.setBounds(10, 10, 300, 50);
this.add(txt);
}
#Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, this.getWidth(), 20);
txt.repaint();
}
}
I want to show the textfield on the top of the panel
JTextField txt = new JTextField();
When you create a JTextField you should use code like:
JTextField txt = new JTextField(10);
Now the text field can calculate its own preferred size.
//txt.setBounds(10, 10, 300, 50);
Don't use setBounds() to give a component a size. Again each Swing component is responsible for determining its own preferred size. Then the layout manager will set the size/location of the component on the panel.
//public void paint(Graphics g) {
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// add custom painting here
}
Don't override paint(). Custom painting is done by overriding the paintComponent() method. And the first statement in the method should be super.paintComopnent(g)
//g2D.setColor(Color.BLACK);
//g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
Don't paint the background of the panel. That is the job of the panel and that is why you need to super.paintComponent(), to make sure the background is painted.
Then in the constructor of your JPanel class you simply use setBackground( Color.BLACK )
//txt.repaint();
Don't ever invoke repaint() on any component in a painting method.
Read the section from the Swing tutorial on Custom Painting for working examples to get you started. Use the demo code as the starting point for you program. Then you simply add a JTextField to the panel, so it will be a single line of code that is needed to display the text field.
It seems like you want to have a JTextField on a black panel. You don't need to set the colour of the panel every time in paint() method. Instead add this to the constructor:
public Screen() {
setOpaque(true);
setBackground(Color.BLACK);
//...
}
and remove paint() method.
Also, if you want to use absolute positioning with setBounds() method then you should set the layout to null setLayout(null) in constructor. If you use absolute positioning you will also need to specify the size of the panel explicitly. However, I would still suggest you use a layout manager that takes care of panel sizing as well.
See this post for more information about absolute positioning.

Java Swing - Buffered Image [duplicate]

I am designing a program that contains two JPanels within a JFrame, one is for holding an image, the other for holding GUI components(Searchfields etc). I am wondering how do I draw the Image to the first JPanel within the JFrame?
Here is a sample code from my constructor :
public UITester() {
this.setTitle("Airplane");
Container container = getContentPane();
container.setLayout(new FlowLayout());
searchText = new JLabel("Enter Search Text Here");
container.add(searchText);
imagepanel = new JPanel(new FlowLayout());
imagepanel.paintComponents(null);
//other constructor code
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawImage(img[0], -50, 100, null);
}
I was trying to override the paintComponent method of JPanel to paint the image, but this causes a problem in my constructor when I try to write :
imagepanel.paintComponents(null);
As it will only allow me to pass the method null, and not Graphics g, anybody know of a fix to this method or another method i can use to draw the image in the JPanel? Help is appreciated! :)
All the best and thanks in advance!
Matt
I'd like to suggest a more simple way,
image = ImageIO.read(new File(path));
JLabel picLabel = new JLabel(new ImageIcon(image));
Yayy! Now your image is a swing component ! add it to a frame or panel or anything like you usually do! Probably need a repainting too , like
jpanel.add(picLabel);
jpanel.repaint();
You can use the JLabel.setIcon() to place an image on the JPanel as shown here.
On the other hand, if you want to have a panel with a background, you can take a look at this tutorial.
There is no need to manually invoke paintComponent() from a constructor. The problem is that you passing null for a Graphics object. Instead, override paintComponent() and you use the Graphics object passed in to the method you will be using for painting. Check this tutorial. Here is an example of JPanel with image:
class MyImagePanel extends JPanel{
BufferedImage image;
public void paintComponent(Graphics g){
super.paintComponent(g);
if(image != null){
g.drawImage(image, 0, 0, this);
}
}
}

How to draw rectangle in JPanel from button click event

I am relatively new to Java Graphics. I want to draw 20 x 80 rectangle at (X,Y) coordinates in JPanel when user clicks a JButton. (where 'X' and 'Y' are coming from 2 JTextFields) .
I have read many questions and tutorial, but could not solve a problem. In some cases, I can draw rectangle but cannot draw new rectangle without emptying JPanel.
Here is my code :
public class CustomPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // first draw a clear/empty panel
g.draw3DRect(Integer.parseInt(x.getText()),Integer.parseInt(y.getText()), 20, 80, true);
// then draw using your custom logic.
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//Code for frame
//Code for JTextfields x and y
JButton btnDraw = new JButton("Draw");
btnDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel= new CustomPanel();
panel.setBounds(406, 59, 407, 297);
frame.getContentPane().add(panel);
frame.revalidate();
}
});
btnDraw.setBounds(286, 339, 89, 23);
frame.getContentPane().add(btnDraw);
}
You ActionListener code is wrong. You don't want to create a new panel, you want to add a Rectangle to the existing panel.
When you create the GUI you should add two panels to the GUI:
The first panel will be an empty panel that will do your custom painting. You would generally add this to the CENTER of the frame
The second panel will contain the "Draw" button. You would generally add this panel to the PAGE_END. Then when you click the Draw button you invoke a method like addRectangle(...) in your custom painting panel so the panel can paint the Rectangle.
Check out Custom Painting Approaches for the two common ways to do custom painting:
Keep a List of Object to paint and then in the paintComponent() method you iterate the LIst an paint each object.
Create a BufferedImage and then just paint the Rectangle onto the BufferedImage, then you can just paint the BufferedImage either in a JLabel or in your paintComponent() method.

Strange repositioning using repaint();

I have a JSlider in a JPanel that return me a value of R-G-B .
I create it, in the Costructor of JPanel. I draw in same Panel (using paintComponent) a little circle, and I change his color using the Slider. I want that the color change in contemporany of slider shift.
So, i use the method repaint.. Next to Panel there is another Panel, with two button.. If I use method repaint in first panel , the buttons of second panel duplicated in the topLeft of First Panel. Why? Thank's you.
First Panel:
public class OptionsPanel extends JPanel {
static JSlider RBG = new JSlider(0,255);
OptionsPanel(){
this.setVisible(false);
this.setSize(350,1000);
this.setLayout(null);
this.setBackground(new Color(200,200,0));
Main.f1.add(this);
RBG.setVisible(true);
RBG.setSize(255,50);
RBG.setLocation(30,240);
this.add(RBG);
LotL lotl = new LotL();
Button save = new Button("Save");
save.setVisible(true);
save.setSize(100,40);
save.setLayout(null);
save.setLocation(60,300);
save.addActionListener(lotl);
save.setBackground(Color.yellow);
save.identificatore=3;
this.add(save);
}
boolean draw=false;
#Override
public void paintComponent(Graphics g){
g.drawOval(50,100,70,70);
g.setColor(new Color(RBG.getValue(),180,200));
g.fillOval(50,100,70,70);
repaint();
}
}
Second Panel:
public class FirstPanel extends JPanel{
FirstPanel(){
this.setVisible(true);
this.setSize(1000,1000);
this.setLayout(null);
this.setBackground(new Color(255,200,180));
Main.f1.add(this);
Button start = new Button("Start Game!");
Button options = new Button("Options");
LotL LotL = new LotL();
start.setVisible(true);
start.setSize(200,80);
start.setLayout(null);
start.setLocation(400,450);
start.addActionListener(LotL);
start.setBackground(Color.green);
start.identificatore=1;
this.add(start);
options.setVisible(true);
options.setSize(200,70);
options.setLayout(null);
options.setLocation(400,550);
options.addActionListener(LotL);
options.setBackground(Color.green);
options.identificatore=2;
this.add(options);
}
}
You've broken the paint chain...
#Override
public void paintComponent(Graphics g){
g.drawOval(50,100,70,70);
g.setColor(new Color(RBG.getValue(),180,200));
g.fillOval(50,100,70,70);
repaint();
}
Graphics is a shared resource, which gets passed to ALL the components that are painted during a given paint cycle.
One of the jobs of paintComponent is to prepare the Graphics context for painting, but filling with the components background color.
You MUST call super.paintComponent before performing any custom painting.
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(50,100,70,70);
g.setColor(new Color(RBG.getValue(),180,200));
g.fillOval(50,100,70,70);
}
Also, there is never any need for paintComponent to be public, no one should ever be calling directly and NEVER modify the state of a component from within any paint method which may trigger a repaint, you will get yourself into a infinite loop which will eventually consume your CPU and make you computer unusable.
Take a look at Painting in AWT and Swing and Performing Custom Painting for more details
You should also avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

BG image with Swing without overriding paintComponent

I'm currently designing a menu with several screens with multiple buttons on each screen. To use buttons on top of the background image, which is in a jLabel (by default, I can't put buttons on TOP of the jLabel), I used GridBagLayout with two panels/menu screen, one panel containing the buttons (opaque = false) and one panel with the background image, or jLabel. In order to switch the current panels being displayed, depending on where the user is in the menu, I made each menu screen (aka. every 2 panels) in separate methods, not classes.
Now, I've come to the point where I'm working on parts of the interface that are unnecessarily complicated, and I don't feel GridBag will serve my purposes, so I was wondering if there was a different way to draw my background image, still being able to use my buttons on top of the image.
The most popular way I looked up was overriding the paintComponent method, but I can't do that, since I've made my JPanels in separate methods, not classes. They're all contained in my original JFrame.
Help would be greatly appreciated, thank you!
Just added this code, but my background remains white for some reason? Trying the other suggestion right now, thanks guys!
private void mainPanel() {
icon = new ImageIcon(getClass().getResource("/phantasma/menuv1.png"));
mainContainer1 = new javax.swing.JPanel() {
#Override
protected void paintComponent(Graphics g) {
g.drawImage(icon.getImage(), 0,0, null);
super.paintComponent(g);
}
};
BG image with Swing without overriding paintComponent
I have no idea why all the postings suggest doing custom painting for this. You would only do custom painting if you need to automatically scale the background image.
If you want the image painted at its real size then use a JLabel.
I can't put buttons on TOP of the jLabel),
Sure you can. Just set a LayoutManager for the JLabel and then you can add any component to it the same way you add components to a panel.
In my comment above I state:
You can always create an anonymous inner JPanel-derived class and override the paintComponent method there if need be.
As an example of what I mean, you can override paintComponent in any JPanel that you create whether it's derived from a stand-alone class or created within a method. For e.g.,
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class AnonInnerPanel {
private static void createAndShowGui() {
JPanel mainPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
};
JFrame frame = new JFrame("AnonInnerPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Categories

Resources