1

I want to paint Graphics on JPanel. Right now, what I am doing is, I use the paintComponent method to define the drawing:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(10,10,100,100);
}

Then I call repaint() wherever I want to put the graphics.

But I wonder if there is a way to add Graphics to JPanel just like adding components without using this paintComponent method: panel.add(myComponent). I saw that Graphics type cannot be initiated, but maybe there might be another type to let me do that.

I'm pretty sure lots of GUIs such as FANG Engine have this option, but all the examples I saw with Swing was using this method. Any suggestions to do without this? Because it is messing up with the overall design of my program sometimes.

Thanks in advance.

Emir
  • 762
  • 2
  • 8
  • 22
  • http://download.oracle.com/javase/tutorial/uiswing/components/panel.html – mKorbel Jul 13 '11 at 16:00
  • @mKorbel, I went through all those APIs and tutorials before I came here. Did you even read my question? – Emir Jul 13 '11 at 16:19
  • @Emir: *"it is messing up with the overall design of my program sometimes."* Unless you figure *why* it is messing up, I suspect the problems will persist using the alternate methods that have been suggested. – Andrew Thompson Jul 13 '11 at 16:21
  • 1
    @Emir: *"Did you even read my question?"* -1 **I** did not have time to carefully read your question. If you want, I'll delete my comment rather than trawl through the details. (And it was good advice that we cannot know you checked, until you tell us. We are not mind readers.) – Andrew Thompson Jul 13 '11 at 16:24
  • @Andew Thompson: Please do. I have been dealing with this problem for a week and I read everything before I came here. It would do nothing but make it complex for commentators if I shared why it messes up with the overall design of my program. And that mess is not the question, if you read the title it says everything I want to know. I will check an answer as soon as I will be able to do what I stated in title, it has nothing to do with my program in overall. I made my design choices and am not going back. My question is simple in structure; I don't ask for a programmers' manual. – Emir Jul 13 '11 at 16:36
  • 1) be sure that manual is better in most of cases as wrong habits, used by majorities of Programer's Guru :-) 2) please what did you talking about, see my post, I didn'd use any `paintComponent() or your original question has already wore off? – mKorbel Jul 13 '11 at 17:11
  • @mKorbel: No success my friend, don't play with words. My original question says "Painting Graphics on JPanel", and if you go forward and read the description, anyone from the Earth can understand that the Graphics relates to java.awt.Graphics. Even if you did not use the paintComponent() in your post that aimed to make fun of me, you did not write a single line about Graphics. Convinced that you should listen to what people say before answering them? – Emir Jul 13 '11 at 18:24

4 Answers4

4

The easiest way would be to create a custom Icon object, which is in charge of your painting. You can then add your icon to your panel via a JLabel (or put it on a button or any other component that takes icons), while abstracting out the painting work.

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
  • 2
    +1 for `implements Icon`, for [example](http://stackoverflow.com/questions/2833482/how-to-represent-double-values-as-circles-in-a-2d-matrix-in-java/2834484#2834484). – trashgod Jul 13 '11 at 16:08
  • I want to draw graphics though. Like g.drawLine etc. Will I be able to do that within icon class? – Emir Jul 13 '11 at 16:12
  • 2
    @Emir, You can create a `BufferedImage`, obtain its `Graphics` object, perform your drawing tasks there, and then set the `BufferedImage` as the icon of the `JLabel`. – mre Jul 13 '11 at 16:15
  • @little bunny foo foo, That sounds good. Will I be able to change the coordinates of the icon? – Emir Jul 13 '11 at 16:18
  • 1
    @Emir: If you look at the [example](http://stackoverflow.com/questions/2833482/how-to-represent-double-values-as-circles-in-a-2d-matrix-in-java/2834484#2834484), you'll see that the coordinates and graphics context are supplied to `paintIcon()`. – trashgod Jul 13 '11 at 16:38
3

You can take a look at the image and buffered image apis. You can generate and handle buffered images in a lot of ways other than the paintcomponent and graphics g. I guess if you generate buffered images and then load them on your JPanel you will be able to get a similar effect. You can use the setRGB method to paint what you want. It's not as versatile as the paint method but it is an alternative.

Eternal_Light
  • 676
  • 1
  • 7
  • 21
  • BufferedImage does not have paint method. Can you be more specific? – Emir Jul 13 '11 at 16:14
  • It has a setRGB function though that is pretty useful. Just not as easy to make it suit your needs as the paint method. You will, most likely, have to work pixel by pixel. I added this to my main post. – Eternal_Light Jul 13 '11 at 16:20
1

An idea would be to put every custom painting inside an individual JPanel, then add this.

public class MyFrame extends JFrame {
    //initialize frame...
    //put your components

    //now put your jpanel
    MyPanel custom = new MyPanel();
    custom.setBounds(10, 10, 100, 100);

    this.add(custom);
}

public class MyPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //draw your stuff...
    }
}

Of course, this would make it a lot less dynamic...

EDIT:

Talking about separation, if you want to separate your screen in e.g. two parts: One for the custom painting and one for the components, for example for a game, then this solution should fit your needs...

Tom S.
  • 243
  • 3
  • 9
  • Thank you for the reply. But why would I do this? I could do all this in one class. More importantly, this still uses the paintComponent method. Thank you for trying though. – Emir Jul 14 '11 at 17:17
0
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JPanel viewportPanelTop = new JPanel();
        JPanel viewportPanelBottom = new JPanel();
        JPanel viewportPanelCenter = new JPanel();
        viewportPanelCenter.setBackground(Color.black);
        viewportPanelCenter.setPreferredSize(new Dimension(600, 400));
        JPanel viewportPanelWest = new JPanel();
        JPanel viewportPanelEast = new JPanel();

        JFrame frame = new JFrame();
        frame.setTitle("JFrame");
        frame.setLocation(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(viewportPanelTop, BorderLayout.NORTH);
        frame.add(viewportPanelWest, BorderLayout.WEST);
        frame.add(viewportPanelCenter, BorderLayout.CENTER);
        frame.add(viewportPanelEast, BorderLayout.EAST);
        frame.add(viewportPanelBottom, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    private Main() {
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1) that's your title and code that you posted here, 2) for other painting is required use of JLabel (as mentioned here), 3) you didn't told us exactly what you really wanted to paint, then some answer looks like (for you) as shots to the dark... because you can paint to the Image or Icon and put that to the JLabel or... or... 4) if you want to use `java.awt.Graphics` then look here http://www.java2s.com/Code/Java/2D-Graphics-GUI/Catalog2D-Graphics-GUI.htm – mKorbel Jul 13 '11 at 20:41