Dren Skywalker
Dren Skywalker

Reputation: 129

Displaying a JLabel text on top of a drawing

i can't figure out how to make this work.

I created a main class, extended in JFrame. If I run the program with the draw() and paint() methods to draw something, my label is not visible. If I just run the Main() class without the draw() and paint() methods, it works.

I'm trying to playing around with Swing components but this is really hard (I'm such a noob T.T).

Would you help me, please?

I'd like to see the label on top of my rectangle.

public class Main extends JFrame {

    JLabel label;

    public Main() {
         setSize(400, 300);
         setTitle("Title");
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setVisible(true);

         label = new JLabel("Text inside");
         add(label, BorderLayout.NORTH);
   }

    public void paint(Graphics g){
        Image img = createImage(getWidth(), getHeight());
        Graphics gr = img.getGraphics();
        draw(gr);
        g.drawImage(img, 0, 0, this);
    }

    public void draw(Graphics g){
        g.drawRect(100, 100, 200, 100);
    }

    public static void main(String[] args) {
        Main start = new Main();
    }
}

Upvotes: 2

Views: 4345

Answers (2)

A Cat
A Cat

Reputation: 629

The resolution of your problem is pretty simple. First, you don't need to create an Image and use createImage(getWidth(), getHeight()) on it to create the Graphics. Apart of that, when you override a method, you "invalidate" it and "replace" it with your code. Swing uses the paint(Graphics g) method to draw the components (like JButtons and JLabels). Override it will cause that your method is called not swing's. To fix the error, in the draw(Graphics g) you simply can write super.paint(g) to call the swing's method. So your class will be like this:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    JLabel label;

    public Main() {
         setSize(400, 300);
         setTitle("Title");
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setVisible(true);

         label = new JLabel("Text inside");
         add(label, BorderLayout.NORTH);
   }

    public void paint(Graphics g){
        Image img = createImage(getWidth(), getHeight());
        Graphics gr = img.getGraphics();
        draw(gr);
        g.drawImage(img, 0, 0, this);
    }

    public void draw(Graphics g){
        super.paint(g);
        g.drawRect(100, 100, 200, 100);
    }

    public static void main(String[] args) {
        Main start = new Main();
    }
}

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

Couple of things:

  1. move your addition of label up before showing your JFrame and add your label to contentPane instead of JFrame directly.

  2. Currently your Image is Overlaying everything because the width and height of image is equals to your frame. I am not sure what you are trying to achieve with drawing blank image with a rectangle. you can directly draw your rectangle on JFrame itself.

  3. If you intend to set your Image as Background and over it you want to show your label then you need to do it differently. You need to create a JLabel with desired image and add it as contentPane.

Hope these pointers will help you.

Upvotes: 1

Related Questions