Results 1 to 10 of 10
  1. #1
    simprepol is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default painting whole label

    Hi,

    Ive programmed an app that basicly consists of a frame that contains a label. This label contains some icons. The problem Im having is, that the label (on purpose) is bigger than the frame so that the frame only displays a selected part of the label. When I start the app everything appears fine, but as soon as I reposition the label via ActionListener so that the frame now displays an area that wasnt displayed before the frame only displays the part that was painted when the app was started and the rest of the frame appears blank. As soon as I resize the frame, the painted area becomes larger but downsizing makes the newpainted area disappear again.
    Ive so far tried every possibility of revalidate, repaint, pack, invalidate, setSize that I could imagine. I have also tried putting the label in a panel that didnt help either. I have also tried changing the layoutmanager to null but as every other thing I have tried that didnt help.
    So basicly I want to know how I can force the frame to draw the entire label? (So that as soon as I reposition the label, it doesnt show me a blank space but the icons that are actually at that position.)

    I hope I have made my self clear and I apologise for any bad english. Its not my first language. I have attached som picture to illustrate the problem:



    painting whole label-start.jpg

    painting whole label-pic2.jpg

    painting whole label-resized.jpg

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    I suggest you post an SSCCE that demonstrates what you're doing. Otherwise we're just guessing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    simprepol is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    So I tried to trim it as much as possible.
    The World-file where the GUI is implemented:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class World
    {
        JFrame frame;
        JLabel label;
        Car car;
        GameComponent components;
        Level level;
        int fwidth = 200, fheight = 200, lwidth = 1000, lheight = 600;
        
        public World()
        {
             level = new Level(lwidth, lheight);
             components = new GameComponent(lwidth, lheight);
             car = new Car(0,0,50,50);
             frame = new JFrame("Frame");
             components.addComponent(car);
             components.addComponent(level);
    
             label = new JLabel(components);
             
             frame.addKeyListener(new KeyAdapter()
             {
                 public void keyTyped(KeyEvent e)
                 {
                     if(e.getKeyChar() == 'x') car.translate(-10);
                     if(e.getKeyChar() == 'c') car.translate(10);
                 }             
             });
             
             final Timer controls = new Timer(150, new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                    moveLabel(label);
                    label.repaint();
                    frame.repaint();
                }
             });
             
             controls.start();
             frame.setLocation(100,100);
             frame.setVisible(true);         
             frame.setLayout(new BorderLayout());
             frame.add(label, BorderLayout.CENTER);
    
             frame.setPreferredSize(new Dimension(fwidth, fheight));
             frame.pack();
             frame.repaint();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
        public void moveLabel(JLabel label)
        {
            int x = -1*car.centerX() + fwidth/2;
            int y = -1*car.centerY() + fheight/2; 
            label.setLocation(x,y);
        }
        
        public static void main(String[] args)
        {
            World w = new World();
        }
    }
    The Level-file where a line-icon is added:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class Level implements Icon
    {
        private int h, w, fh, fw;
        Line2D.Double line;
        
        public Level(int lheight, int lwidth)
        {
            h = lheight;
            w = lwidth;
            line =new Line2D.Double(0,100,lwidth,100);
        }
        
        public int getIconHeight()
        {
            return w;
        }
        
        public int getIconWidth()
        {
            return h;
        }
        
        public void paintIcon(Component c, Graphics g, int x1, int y1)
        {        
            Graphics2D g2 = (Graphics2D) g;
            g2.draw(line);
        }
    }
    The GameComponent-file where the icons are stored
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.util.*;
    
    public class GameComponent extends JComponent implements Icon
    {
        private ArrayList<Icon> things;
        private int height, width;
        
        public GameComponent(int width, int height)
        {
            this.height = height;
            this.width = width;
            things = new ArrayList<Icon>();
        }
        
        public void addComponent(Icon i)
        {
            things.add(i);
        }
        
        public int getIconHeight()
        {
            return height;
        }
        
        public int getIconWidth()
        {
            return width;
        }
        
        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            Graphics2D g2 = (Graphics2D) g;
            for(Icon i : things) i.paintIcon(c,g,x,y);
        }
    }
    And finally the Car-File where a moveable circle is added
    Java Code:
    import java.awt.*;
    import java.util.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class Car implements Icon
    {
        Ellipse2D.Double circle;
        int x, y, w, h;
        
        public Car(int x, int y, int w, int h)
        {
            this.x = x;
            this.y = y;
            this.w = w;
            this. h = h;
            circle = new Ellipse2D.Double(x, y, w , h);
        }
    
        public int centerX()
        {
            return x + w/2;
        }
        
        public int centerY()
        {
            return y + h/2;
        }
        
        public int getIconHeight()
        {
            return h;
        }
        
        public int getIconWidth()
        {
            return w;
        }
    
        public void translate(int dx)
        {
            x += dx;
        }
        
        public void paintIcon(Component c, Graphics g, int x1, int y1)
        {
            circle = new Ellipse2D.Double(x, y, w , h);
            Graphics2D g2 = (Graphics2D) g;
            g2.draw(circle);
        }
    }
    If you execute the main method in World a frame will appear you can move the circle by typing x and c. As soon as you reach the border the circle will start disappearing. Resizing the frame, will make it visible again. But if you downsize the frame again, it will disappear again.

    I hope everything is clear. Ill appriciate any help.
    Last edited by simprepol; 01-25-2011 at 07:07 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    You've got way too much going on in that code. For example, why are you trying to move around a component (label) that's been added to a JFrame with a BorderLayout? Then it seems on top of that, you're trying to move things around on the thing you're trying to move around? It doesn't make a ton of sense.

    I would rethink your painting logic: why not use a regular old JPanel, override its paintComponent, and draw your shapes onto that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    simprepol is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    The reason,why I move both the label and the circle, is that I want the circle to always be in the center of the frame. Its like the old super mario jump and run games. If you move mario he actually doesnt change position on the screen, because as he makes a step forwards the screen moves along with him.
    But As you can see in my app the new available content is not painted and so after a few steps mario disappears.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    I'm not convinced that the content is not painted- it looks more like it's just being painted in the wrong place.

    Again, I'd rethink the logic here if I were you- Instead of trying to coordinate the movement of different things, why don't you just use a stationary JPanel, a stationary circle (or whatever you want in the center), then simply change the coordinates of the thing that moves and paint that to the JPanel as well?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    simprepol is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    yeah thats what I meant. Im quite sure its beeing painted because you can manually resize the frame and the new content becomes visible.
    About your suggestion: It sounds like you dont want me to move the cirlce but the environment around the circle. For example: If the circle is supposed to go one step to the right all of the surroundings should move one step to the left? I can see that this will help me with the disapearing content, but it seems to me quite complicated. But I will give it a try if thats the only possibility?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    It's not the only possibility, but it actually sounds like the simplest to me. You're going to have to translate the environment anyway (which is what I think you're trying to do by moving your label), so that actually sounds more complicated to me.

    That being said, do whatever fits into your brain the best. I don't have to work with this code; you do. So if one way seems simpler to you, then that's the way to go. However, I will warn you that my way is probably the more "standard" way to do it, including using a JPanel to do your painting instead of a label and Icons.

    If I didn't already give it to you, here's a tutorial on painting: http://java.sun.com/products/jfc/tsc/articles/painting/
    Last edited by KevinWorkman; 01-26-2011 at 04:42 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    simprepol is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    So if one way seems simpler to you, then that's the way to go
    The problem right now is that it wont work my way. The app simply does only paint/show the part of the label (I actually even tried to put the label into a panel and move the panel) that is visible right at the beginning. As soon as the label/panel moves the new visible part will not be redrawn and appears blank.
    I can see that I propably should get rid of the label and the icons and use panels instead. I will eventually, right now I'm trying to get "my way" to work but I cannot see how.
    But thanks for the advice and time you have given me so far.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

Similar Threads

  1. Painting Problem!
    By Jcbconway in forum Advanced Java
    Replies: 3
    Last Post: 11-17-2010, 04:14 AM
  2. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  3. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  4. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:44 AM
  5. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 11:24 AM

Tags for this Thread

Posting Permissions

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