Results 1 to 11 of 11
  1. #1
    billycro is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Question drawing only the most recently added component?


    so heres the code that isnt working
    Java Code:
    public class Runner
    {
       public static void main(String[] args){
           Unit basic= new Unit();
           basic.setX(20);
           basic.setY(20);
           Worker work= new Worker();
           work.setX(50);
           work.setY(30);       
           Grid g=new Grid();       
           g.setSize(1000,1000);       
           g.add(work);
           g.add(basic);
           g.show();
        }
    }
    Java Code:
    public class Grid extends JFrame
    {
       private ArrayList<Unit> units;
       private ArrayList<Integer> terrain;
       private JPanel panel;
       public Grid(){
           units=new ArrayList<Unit>();
           terrain=new ArrayList<Integer>();
           panel=(JPanel)getContentPane();
          
       }
       public void add(Unit u){
           units.add(u);
           panel.add(u);
        } 
    }
    Java Code:
    public class Unit extends JComponent
    {
        private int x=0;
        private int y=0;
        private int width=20;
        private int height=20;
        private Color drawColor;
        public Unit(){
            drawColor=Color.BLACK;
        }
        public Unit(Color dColor){
            drawColor=dColor;
        }
        public void doActivity(){
            
        }
        public void setX(int a){
            x=a;
        }
        public void setY(int b){
            y=b;
        }
        public void paintComponent(Graphics g){
            g.setColor(drawColor);
            g.fillOval(x-width/2,y-height/2,width,height);
        }
    }
    Java Code:
    public class Worker extends Unit
    {    
        public Worker(){
            super(Color.GREEN);   
        }
        public Worker(int x, int y){
            super(Color.GREEN);
            setX(x);
            setY(y);
        }
        public void doActivity(){
            
        }
    }
    these are all the classes included, i've excluded the import statements and irrelevant methods,

    the exact problem is that whichever Unit obbject i add second in the runner is the Unit being drawn, why does this happen? i cant think of a solution
    Last edited by billycro; 07-03-2011 at 08:03 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    You forgot to mark in your code where the problem is. Add some comments to say where:
    // **** HERE IS THE PROBLEM .... because.... or when .... etc

    Please wrap your code in code tags to preserver the formatting. Use the # icon above the input area or see: BB Code List - Java Forums

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

    Default

    You're adding components to a JFrame (actually to its contentPane) which uses a BorderLayout by default. Adding components to a BorderLayout as you're doing adds them BorderLayout.CENTER and will cover anything added before. I think that you want to give the contentPane a GridLayout or something similar.

  4. #4
    billycro is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    panel.setLayout(null);
    i added this below where i put a value into panel, because this generally works with when i do graphical things, but now nothing gets drawn at all, and with the grid layout the same happened

    also the components are drawn in the correct placeswhen there is the default layout, theyre just not visible like i said

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

    Default

    Quote Originally Posted by billycro View Post
    Java Code:
    panel.setLayout(null);
    i added this below where i put a value into panel, because this generally works with when i do graphical things, but now nothing gets drawn at all, and with the grid layout the same happened

    also the components are drawn in the correct placeswhen there is the default layout, theyre just not visible like i said
    I don't recall recommending that you use null layout. Please read the tutorials on layout managers if you want to get this right: Layout Tutorial

  6. #6
    billycro is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    i tried that after a GridLayout didnt work

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

    Default

    Quote Originally Posted by billycro View Post
    i tried that after a GridLayout didnt work
    OK, it's good that you tried, but if you still need our help, you may wish to show your attempt and elaborate further on how it didn't work. Myself, I do poorly if I try to guess what's wrong since I'm terrible at reading minds. :)

  8. #8
    billycro is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    lol, im working on that one too, well with any layout so far other than the default, nothing will appear, and then with the default layout the last thing overlaps everything else, so in conclusion, im not entirely sure where to go with it, gridlayout didnt seem to work, and also i want exact positioning, and in the layout tutorial thats what the layout null is supposed to be right? also with null shouldnt i have no worries of overlap?

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

    Default

    Again, please show your attempt with using GridLayout, and please post any error messages it may cause.

  10. #10
    billycro is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    im just rewriting it so the Grid is responsible for all drawing with its own paint method, thanks anyways guys as i progress through my project i'll come back to making everything its' own component

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    well with any layout so far other than the default, nothing will appear,
    When doing custom painting you need to override the getPreferredSize() method of your component otherwise the default size is (0, 0).

Similar Threads

  1. Weird problem with doubles not being added to sum
    By kiregad in forum New To Java
    Replies: 3
    Last Post: 03-24-2010, 10:08 PM
  2. how to save added items in jcombobox
    By kwink in forum AWT / Swing
    Replies: 2
    Last Post: 03-22-2009, 07:03 PM
  3. Help-would like JFRAME added to program
    By tigerfan2 in forum New To Java
    Replies: 1
    Last Post: 05-13-2008, 04:03 AM
  4. No datas in added to arraylist
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 12:07 PM
  5. Tree with Listener not working when added to ScrollPane
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2008, 08:06 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
  •