Page 1 of 2 12 LastLast
Results 1 to 20 of 32
Like Tree1Likes

Thread: Duplicate doesn't work?

  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Duplicate doesn't work?

    Okay,so i'm having problems. Firstly: I'm making duplicates. I can do that,yes.

    Java Code:
    Main(){
    		Bee newb = new Bee(200, 100, 100, 100, Color.yellow);
    		Bee newb2 = new Bee(200, 200, 100, 100, Color.black);
    		Background back = new Background(0, 0, 800, 600,Color.cyan);
            
            fps.addMouseListener(listener);
            fps.addMouseMotionListener(listener);
            fps.animate();
            fps.addRect(newb);
            fps.addRect(newb2);
            fps.addBackground(back);
            fps.setPreferredSize(new Dimension(800,600));
            
            frame.add(fps);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.getContentPane().add(fps);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setPreferredSize(windSize);
            System.out.println("Init");
        }
    Basic code that makes the duplicate. My problem is that i put this anywhere else outside of the initialization,and nothing happens.

    Now,i know that it's not adding it because 1: there's something i set up in the loop to show the amount of "bees" in the list.

    How i add the bees is exactly the same as the init:

    Java Code:
    	static DrawingComponent fps = new DrawingComponent();
    	static Bee newb2 = new Bee(150, 150, 10, 10, Color.red);
    
    public void mousePressed(MouseEvent e) {
    		if(e.getButton() == MouseEvent.BUTTON1){
    			Main.isMouseLeft = true;
    			Main.isTargActive = true;
    			Main.targ.setLocation(e.getX()-5, e.getY()-5);
    			System.out.println("TargActive");
    		} else if (e.getButton() == MouseEvent.BUTTON3) {
    			Main.isMouseRight = true;
    			Main.isTargActive = false;
    			System.out.println("!TargActive");
    		}
    		if(Main.isMouseLeft){
      			fps.addRect(newb2);
            }
    	}
    Now,the problem is not it reaching the line or anything,but it's because it's like "Nope,not adding the object."

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Duplicate doesn't work?

    I'm making duplicates.
    What does that mean?

    anywhere else outside of the initialization,and nothing happens.
    Make a small, simple, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    Java Code:
    package Main;
     
    import java.awt.*;
    
    import javax.swing.*;
     
    class Main extends JPanel{
        private static final long serialVersionUID = 1L;
        
        
        static JFrame frame = new JFrame();
        static DrawingComponent fps = new DrawingComponent();
        public static Listener listener = new Listener();    
        public static Main main = new Main();  
        public static Point mse = new Point(0,0);
        public static Point targ = new Point(0,0);
        public static Dimension windSize = new Dimension(800,600); 
        
        public static int windNum = 0;
        
        public static boolean isMouseLeft, isMouseRight;
        public static boolean isTargActive;
        
    	Main(){
    		Bee newb = new Bee(200, 100, 100, 100, Color.yellow);
    		Bee newb2 = new Bee(200, 200, 100, 100, Color.black);
    		Background back = new Background(0, 0, 800, 600,Color.cyan);
            
            fps.addMouseListener(listener);
            fps.addMouseMotionListener(listener);
            fps.animate();
            fps.addRect(newb);
            fps.addRect(newb2);
            fps.addBackground(back);
            fps.setPreferredSize(new Dimension(800,600));
            
            frame.add(fps);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.getContentPane().add(fps);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setPreferredSize(windSize);
            System.out.println("Init");
        }
         
        public static void main(String[] args) {
    		
        }
           
     
        public void start(){
             
        }
        
        public static void windResize(){
        	if (windNum == 0){
        		fps.setPreferredSize(new Dimension(800, 600));
        		
        		System.out.println("Resized");
        		
        		windNum = 1;
        	} else if (windNum == 1){
        		fps.setPreferredSize(new Dimension(1200, 950));
        		
        		System.out.println("Resized");
        		
        		windNum = 2;
        	} else if (windNum == 2){
        		
        		windNum = 0;
        	}
        }
    }
    
    package Main;
    
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    class Bee {
    	  int x;
    	  int y;
    	  int height;
    	  int width;
    	  Color color;
    	  Bee(int x, int y, int width, int height, Color color) {
    		  	this.x = x;
    	    	this.y = y;
    	    	this.width = width;
    	    	this.height = height;
    	    	this.color = color;
    	  }
    	 
    	  public void draw(Graphics g) {
    		  g.setColor(color);
    		  g.fillRect(x, y, width, height);
    	  }
    	 
    	  public int getY() {
    		  return y;
    	  }
    	  
    	  public int getX(){
    		  return x;
    	  }
    	   
    	  public int getHeight() {
    		  return height;
    	  }
    	   
    	  public void setY(int y) {
    		  this.y = y;
    	  }
    	  
    	  public void setX(int x) {
    		  this.x = x;
    	  }
    }
    
    class DrawingComponent extends JPanel {
    	private static final int ANIMATION_DELAY = 10;
    	private List<Bee> beeList = new ArrayList<Bee>();
    	private List<BeeButton> butList = new ArrayList<BeeButton>();
    	private List<Background> BackList = new ArrayList<Background>();
    	private int dirY = 0;
    	private int dirX = 0;
    	   
    	  DrawingComponent() {
    		  
    	  }
    	   
    	  public void animate() {
    		    new Timer(ANIMATION_DELAY, new ActionListener() {
    			      public void actionPerformed(ActionEvent arg0) {
    			    	  	for (BeeButton button : butList){
    			    	  		int x = button.getX();
    			    	  		int y = button.getY();
    			    	  		int mousex = Main.mse.x;
    			    	  		int mousey = Main.mse.y;
    			    	  	}
    			    	  	
    				        for (Bee bee : beeList) {
    					          int y = bee.getY();
    					          int x = bee.getX();
    					          int targx = Main.targ.x;
    					          int targy = Main.targ.y;
    					          
    					          if(!Main.isTargActive){
    					    		  System.out.println("beezanim" + " " + beeList.size());
    					    		  try {
    									Thread.sleep(476);
    								} catch (InterruptedException e1) {
    									// TODO Auto-generated catch block
    									e1.printStackTrace();
    								}
    						          if(new Random().nextInt(5) == 1){
    						        	  dirY = 1;
    						          } else if(new Random().nextInt(5) == 2){
    						        	  dirY = -1;
    						          } else if(new Random().nextInt(5) == 3){
    						        	  dirX = 1;
    						          } else if(new Random().nextInt(5) == 4){
    						        	  dirX = -1;
    						          } else if(new Random().nextInt(5) == 5){
    						        	  dirX = 0;
    						        	  dirY = 0;
    						          } 
    						          try {
    									Thread.sleep(15);
    								} catch (InterruptedException e) {
    									e.printStackTrace();
    								}
    					          } else if(Main.isTargActive){
    					        	  dirX=0;
    					        	  dirY=0;
    					        	  if(x >= targx){
    					        		  x = x - 2;
    					        	  }
    					        	  if(x <= targx){
    					        		  x = x + 2;
    					        	  }
    					        	  if(y >= targy){
    					        		  y = y - 2;
    					        	  }
    					        	  if(y <= targy){
    					        		  y = y + 2;
    					        	  }
    					          }  
    					          bee.setY(y + (dirY * 2));
    					          bee.setX(x + (dirX * 2));
    					        }
    				        //End of calcs
    				        repaint();
    			      }
    		    }).start();
    	  }
    	 
    	  public void addRect(Bee bee) {
    		  beeList.add(bee);
    	  }
    	  public void addBackground(Background back){
    		  BackList.add(back);
    	  }
    	 
    	  @Override
    	  public void paintComponent(Graphics g) {
    		    super.paintComponent(g);
    		    	for (Background back : BackList){
    		    		back.draw(g);
    		    	}
    		    	for (BeeButton button : butList){
    		    		button.render(g);
    		    	}
    			    for (Bee bee : beeList) {
    			    	bee.draw(g);
    			    }
    		  }
    	}
    That's all i really have right there,i can't see any problems. I'm just confused as to how i can use fps.add(bee) in the init,but not anywhere else,even when i use the exact same refrences (static DrawingComponent fps = new DrawingComponent();). I want to know why it only works in the init,and how to fix it.( Note: I'm making something that works. Not something efficient or the best way. I wanna make it work,and optimize later.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Duplicate doesn't work?

    how i can use fps.add(bee)
    What is the definition for the add() method in the fps object?
    Does it take an object of type Bee. (I assume bee is an instance of the Bee class).
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    ... Why are you asking ME this? It's how the Lists work. I haven't fully understood them yet,but you just use [list].add([object]),that's how it adds a new object instance. I know it works,because I can add(and have added) multiple instances. But i want to be able to add a new instance whenever i click. I have listening working,i know i do. It reaches the fps.add(bee) in the listening class(I know because i used a system.out.println() right after the fps.add(bee)),but it doesn't add it.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Duplicate doesn't work?

    First, his question was rhetorical to get you to think. It is not a list. It appears to be a DrawingComponent that inherits from JPanel. Thus we know what the add method does and what kind of argument it takes. But do you? Do you think JPanel knows anything about a Bee class?

    Regards,
    Jim
    Last edited by jim829; 11-08-2013 at 07:46 PM.
    Norm likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    .... I'm still confused....

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Duplicate doesn't work?

    The code you've post could be getting compiler errors. Please copy the full text of the error messages and paste it here.

    The java compiler is very fussy. If a method is defined to have a parameter of type ClassX then you will get an error message if the code tries to call the method with a variable that is not type ClassX.
    The API doc defines the methods for classes and shows what is allowed.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Duplicate doesn't work?

    You want to add a Bee object to your fps instance. But you can't because the fps.add(), which is inherited from Container via JPanel, expects a type Component (and perhaps other types since add() is overloaded). So Bee must extend or implement something that add expects. But that does not guarantee it will behave as you wish. You may need to override things like paintComponent etc. It is important to become familiar with the inherited classes. This can be done easily with the JDK Javadoc.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    Quote Originally Posted by Norm View Post
    The code you've post could be getting compiler errors. Please copy the full text of the error messages and paste it here.

    The java compiler is very fussy. If a method is defined to have a parameter of type ClassX then you will get an error message if the code tries to call the method with a variable that is not type ClassX.
    The API doc defines the methods for classes and shows what is allowed.
    There's no error. I'm using Eclipse.

  11. #11
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    But what I don't get is how it works 100% perfectly,as long as the fps.add(bee) is put in the initialization. But if it's put ANYWHERE else it doesn't work.

    http://i.imgur.com/s44teWM.png This is what i want,and i have. But i want it to add a red rectangle whenever i click somewhere(it should spawn where the smaller black rectangle is),which it won't do.

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Duplicate doesn't work?

    In your first example, where you claim you are adding it in your initialization, you are using addRect, not add. And the addRect method is designed to accept an
    instance of Bee.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    Quote Originally Posted by jim829 View Post
    In your first example, where you claim you are adding it in your initialization, you are using addRect, not add. And the addRect method is designed to accept an
    instance of Bee.

    Regards,
    Jim
    Sorry,yes. This whole time i've been saying fps.add() when it's,in fact,fps.addRect().

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Duplicate doesn't work?

    OK. We got that resolved. So does it work or are there still problems?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Duplicate doesn't work?

    it's,in fact,fps.addRect().
    Good luck Jim. You'll need your crystal ball for this one.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    Quote Originally Posted by Norm View Post
    Good luck Jim. You'll need your crystal ball for this one.
    Well,the only thing IN fps.addRect(Bee bee) is beeList.add(bee). But yeah,I'm sorry,my mistake. But that still doesn't fix this. It's fps.addRect() everywhere i need it.

  17. #17
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    Quote Originally Posted by jim829 View Post
    OK. We got that resolved. So does it work or are there still problems?

    Regards,
    Jim
    As i've said,the problem is not making the multiple instances. It's calling multiple instances outside of the initialization.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Duplicate doesn't work?

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Duplicate doesn't work?

    As i've said,the problem is not making the multiple instances. It's calling multiple instances outside of the initialization.
    OK, first multiple instances of what? The Bee class? And let me explain something too you. You are asked for an SSCCE.org to demonstrate the problem. This allows folks to easily look over your code and possible run it to see the problem. In your case you started off with a snippet which then lead to more code and finally resolved part of the problem. However, it still cannot be run because you have not included all the essential classes (e.g. your button classes). I will try and run it to observe the problem but I need all the code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Duplicate doesn't work?

    I'll upload a .zip of the whole program.

    Beestuffz

    If you have any other tips for optimizing anything,I'd love to hear it. I slowed it down a TON to actually be able to read the console,I'm planning on putting test text in there( You can remove the thread.sleep(200) in Drawing Component). But yeah,that's the whole thing,with eclipse set up and everything,since that's what I use.

Page 1 of 2 12 LastLast

Similar Threads

  1. I wonder why this doesn't work...
    By louboulos in forum New To Java
    Replies: 3
    Last Post: 05-13-2012, 04:59 PM
  2. Why doesn't this work?
    By mailman in forum Java Applets
    Replies: 5
    Last Post: 01-10-2012, 01:01 PM
  3. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 05:22 PM
  4. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  5. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 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
  •