Results 1 to 4 of 4

Thread: A bit of help

  1. #1
    Wesnc is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default A bit of help

    So I have a custom JLabel, and I want to paint a circle around it. The JLabel paints fine, but the custom circle code does not.
    I figured I could do it all in one class, instead having another class that draws the circle separately, I'm not sure if I can do this or not.

    Once again, the Jlabel with an indicated icon shows properly, but the custom circle code does not.

    TowerMarker.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package maputilproject;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    
    /**
     *
     * @author WESNC
     */
    public final class TowerMarker extends JLabel
    {
        private int x, y, id;
        private ImageIcon ic;
        private String ql;
        
        public TowerMarker(int id, int x, int y, ImageIcon ic, String ql)
        {
            this.x = x;
            this.y = y;
            this.ic = ic;
            this.ql = ql;
            setBounds(x, y, 40, 85);
            setIcon(ic);
            setToolTipText("QL: "+ql);
    
        }
        
        @Override
        public int getX()
        {
                return x;
        }
        
        @Override
        public int getY()
        {
            return y;
        }
        
        public String getQL()
        {
            return ql;
        }
        
        public ImageIcon getImageIcon()
        {
            return ic;
        }
        
        public int getID()
        {
            return id;
        }
        
        @Override
        public String toString()
        {
            return x+":"+y+":"+ql;
        }
        
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            
            g.drawOval(x, y, 100, 100);
            
        }
        
    }
    My Main class, just a snippet to give an idea.
    Java Code:
    towers_mr.addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent e) 
                        {
                            //JLabel mrTower = new JLabel(new ImageIcon(getClass().getResource("/tower_mr.png")));
                            //map.add(mrTower);
                            //mrTower.setBounds(mX, mY, 40, 85);
                            
                            final int mXStore = mX-15;
                            final int mYStore = mY-42;
                            
    
                            Object[] test = {"Input Tower QL", "Example: 10, 20, 30, 40, etc"};
                            String s = (String)JOptionPane.showInputDialog(pmenu, test);
                            
                            Random r = new Random();
                            TowerMarker mrt = new TowerMarker(r.nextInt(10000), mXStore, mYStore, new ImageIcon(getClass().getResource("/tower_v2_mr.png")), s);
                            map.add(mrt);
                            map.repaint();
                            
                            mrt.addMouseListener(new MouseAdapter()               
                                        {
                                            @Override
                                            public void mouseReleased(MouseEvent evt)
                                            {
                                                if(evt.isPopupTrigger())
                                                  {
                                                      mX = evt.getX();
                                                      mY = evt.getY();
    
                                                      rclick.show(evt.getComponent(), evt.getX(), evt.getY());
                                                  }
                                            }
                                        }
                                    );
                        }
                    }
                    );
    Any help upon this matter?

    EDIT: Also, the map is just a jlabel WHICH may be some of my problem, but if so why does everything else work fine?
    Last edited by Wesnc; 09-13-2011 at 06:37 PM.

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

    Default Re: A bit of help

    Correct me if I'm wrong, but your circle seems bigger than the bounds of your JLabel. Why the absolute sizing anyway?

  3. #3
    Wesnc is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: A bit of help

    It just seemed easier at the time, would this be affecting the issue?

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

    Default Re: A bit of help

    Quote Originally Posted by Wesnc View Post
    It just seemed easier at the time, would this be affecting the issue?
    Which comment of mine do you mean?

    regarding drawing outsides the bounds of JLabel -- yes that can affect the issue. How would you draw a circle around a JLabel if you're doing the drawing in the JLabel?
    regarding use of absolute sizing -- this could affect the issue. Better to let layout managers decide the size needed.

Posting Permissions

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