Results 1 to 20 of 20
  1. #1
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default Coloring an image

    Hello everyone, my JFrame has two panels, top and bottom, and the top contains two items, my map and the information(JTextArea). I'm currently using the "image in a JLabel" method to display the map, but now I know that isn't going to work for my purposes. I want to color the area on the map to whatever they select, but I can't use 2D graphics because then my information JTextArea will be moved to the center, and the image will cover it up. So how would I go about doing this?
    If you need any more information just say so.
    Thank you
    ~Twitch
    The one who laughs last is teh stupid.
    :3

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Twitchy5 View Post
    ... but I can't use 2D graphics because then my information JTextArea will be moved to the center, and the image will cover it up. ...
    Why is the JTextArea being moved to a position where it becomes covered by the JLabel? This should not happen if your layout is correct.

  3. #3
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Why is the JTextArea being moved to a position where it becomes covered by the JLabel? This should not happen if your layout is correct.
    I said that I can't use "2D Graphics" which isn't a JLabel.
    The one who laughs last is teh stupid.
    :3

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Still you should be able to display both the 2D graphic and the textarea at the same time, right?

  5. #5
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    I can, but like I said, when I draw the image it covers up the textarea because you can't put 2D graphics in a JPanel, or can you?
    The one who laughs last is teh stupid.
    :3

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Where are you currently drawing the image on?
    Did you try putting it on the panel?

  7. #7
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    I just tried to draw the image, can you put the image on a JPanel, and if you can I'm going to wonder why I was to stupid not to see that :p.
    The one who laughs last is teh stupid.
    :3

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why don't you post the code where you have tried and failed by getting the overlap.

  9. #9
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Taking code out because it's sloved

    But if you take out the JLabel, and try to draw the image it will overlap it.
    Last edited by Twitchy5; 09-04-2009 at 08:25 PM. Reason: taking out code
    The one who laughs last is teh stupid.
    :3

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Let's see the code where to tried to draw the image and got the overlapping behavior.

  11. #11
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Well, it seems I've ran into a bit of bad luck, I lost the code with the paint method and I cannot find it on google, so what way would you draw an image on a Jpanel without being a background?
    The one who laughs last is teh stupid.
    :3

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You lost your code on google? No wonder some people seem to be avoiding google when you'd think that they need it badly.

  13. #13
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Don't get me wrong now, I code my own stuff, but to learn certain things I look on google like any other thing, and since this is my first time doing something like this, I don't know the code, and I tried looking on the API and seeing what methods do what, but sometimes you need help.
    The one who laughs last is teh stupid.
    :3

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

  15. #15
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post

    db
    Thank you, sir, that was exactly what I was looking for :).

    Anyways, when I do the paintComponent it is in the background, so my information box is covering up my map because there is nothing in the panel, it's just a background.
    The one who laughs last is teh stupid.
    :3

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

  17. #17
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    Ok guys I got it, what I did was this:
    Java Code:
    BufferedImage image = ImageIO.read(new File(filename));
    ImageIcon img = new ImageIcon(image);
    JLabel map = new JLabel(img);
    
    //to paint:
    private void paintTerritories(int ter)
    {
       //do whatever with the BufferedImage
       frame.update();
    }
    The one who laughs last is teh stupid.
    :3

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default

    1. Perform custom painting in a JComponent or JPanel, not is a top-level container like JFrame (or JDialog/JApplet).

    2. Call repaint(), not update() to update your GUI.

    Looks like you aren't interested in going through helpful links, so you'll probably ignore this too.

    db

  19. #19
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    No, I went to the links, and they said little about how to paint a picture, and I know my layouts. And repaint() just calls the update() usually.
    The one who laughs last is teh stupid.
    :3

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello Twitchy. This article goes into more depth on the painting of heavy weight and light weight components, gives general guidelines on painting in Swing, and gives reasons not to directly invoke update with Swing:

    Painting in AWT and Swing

    Much luck.

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM
  4. Replies: 0
    Last Post: 07-25-2008, 02:31 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •