Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 47
Like Tree2Likes

Thread: random numbers with no duplicate

  1. #21
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    sorry for misunderstanding.

    i read your post of 14. as another solution but it was another problem sorry for that :D

    anyways. if i put pictures to jar files how can i upload it as?with all jar files?

    is there a another solution for this ?

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    Put everything the applet needs in one jar file.

  3. #23
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    now i feel im really newbie on java :( after following questions

    where is the jar file ?

    on eclipse or on my projects files?

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    You should create one. Put all the class files, image files and any other files the applet needs in it.
    Change your <APPLET tag to have an archive= attribute that refers to your jar file.

  5. #25
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Hey again.

    Im recoding my project PicturePuzzle.

    i try to lower line of code.

    i did create new class.

    Java Code:
    public class MyButton {
    	private double field;
    	private JButton btn;
    	public MyButton(int field, String imageFile) {
    		this.field = field;
    		btn = new JButton(new ImageIcon(imageFile));
    	}
    	
    	public double getField() {
    		return field;
    	}
    	public void setField(int field) {
    		this.field = field;
    	}
    	public JButton getBtn() {
    		return btn;
    	}
    	public void setBtn(JButton btn) {
    		this.btn = btn;
    	}
    
    	
    }
    This class gives to button field number so i can operate operation on it(Before create this class i put String number on JButton like this JButton("i"). "i" was int here. So Button had picture which complete buttons.Unfortunely under the picture there was int i which increase along buttons.So i try to prevent this situation.So i did create to class for it.)
    Here is how to Jbutton is created with picture and field.
    Java Code:
    btn[i] =new MyButton(i, rsm[list[i]]);
    Here is actionlistener for all buttons.
    Java Code:
    for (int i = 0; i < btn.length; i++) { 
    		
    		btn[i].getBtn().addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent e) {
    				String actionCommand = e.getActionCommand();//old code was understand with this.With class MyButton need new code here
    Before i create MyButton class i was reaching actioncommand because all butons had integer but now i dont have them i only have fields for buttons with MyButtonClass
    now here is the question.

    how can programs understand which button pressed with using class MyButton??

    before the class MyButton code for it was like this
    Java Code:
    else if(Double.parseDouble(actionCommand)==17.0){
    			setPicture(17,18);//Double.parseDouble(actionCommand) need to change this code,need change
    thank you
    Last edited by KSBeyaz; 12-08-2011 at 06:43 PM.

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    how can programs understand which button pressed with using class MyButton?
    The ActionEvent object has lots of information in it you could use to determine which component/button caused the event.
    If the source of the event was a MyButton object you could cast the source to your class and then call any of themethods in your class. If the action listener is only added to your MyButton class objects, then you could safely cast the source object to a MyButton object.

  7. #27
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    i dont know right code for it
    i did try like this
    MyButton dnm =(MyButton)e.getSource()
    dont work

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    dont work
    Please explain.
    You must post the error message if you want help.
    Add a println at the start of the actionPerformed method to print out the value of the ActionEvent passed to the method.

  9. #29
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Here is the code
    for (int i = 0; i < btn.length; i++) { //her button için action listener açar

    btn[i].getBtn().addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
    MyButton dnm = (MyButton)e.getSource();//btn a verilen ismi döndürür
    String actionCommand = e.getActionCommand();

    System.out.println(dnm.getField());
    here is the Error msg:

    Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: javax.swing.JButton cannot be cast to MyButton
    at DenemePicturePuzzle$1.actionPerformed(DenemePictur ePuzzle.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    You do not show what the object is: btn[i].getBtn().addActionListener
    that you are adding a listener to.
    Is it a MyButton object? It must be when you try to cast it.

    The source of the event is a JButton object contained in your MyButton class.
    If MyButton extended JButton then you could add it to the container and it would be the source of the event.

    Another solution is to use the JComponent class's ClientProperties. It is a hashtable contained inside of JComponents that you can put objects into and get them back out again.

  11. #31
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Quote Originally Posted by Norm View Post
    You do not show what the object is: btn[i].getBtn().addActionListener
    that you are adding a listener to.
    Is it a MyButton object? It must be when you try to cast it.

    The source of the event is a JButton object contained in your MyButton class.
    If MyButton extended JButton then you could add it to the container and it would be the source of the event.

    Another solution is to use the JComponent class's ClientProperties. It is a hashtable contained inside of JComponents that you can put objects into and get them back out again.
    Norm you are the best.

    finally i have get working my project PicturePuzzle on my webside.

    but still im on proggress on recoding thank you for that.

    You can look at it with ksbeyaz.webuda.com with clicking picturepuzzle

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    Good luck with the recoding.

  13. #33
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Quote Originally Posted by Norm View Post
    You do not show what the object is: btn[i].getBtn().addActionListener
    that you are adding a listener to.
    Is it a MyButton object? It must be when you try to cast it.

    The source of the event is a JButton object contained in your MyButton class.
    If MyButton extended JButton then you could add it to the container and it would be the source of the event.

    Another solution is to use the JComponent class's ClientProperties. It is a hashtable contained inside of JComponents that you can put objects into and get them back out again.
    hey again Norm i did show object of btn[i] on post 25.
    i did exten Mybutton with JPanel but still dont work on eclipse with this class.

    How can i add to it container can you give same example plz?

  14. #34
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: random numbers with no duplicate

    Shouldn't you be extending MyButton with JButton not JPanel, or is that a typo?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    still dont work
    What happens?

  16. #36
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: javax.swing.JButton cannot be cast to MyButton
    at DenemePicturePuzzle$1.actionPerformed(DenemePictur ePuzzle.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    and so on
    Last edited by KSBeyaz; 12-09-2011 at 08:22 PM.

  17. #37
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    if i remove all btn variables how can i reach Mybutton from Main class?

    iwas reaching it with getButton() method.

    Moreover,i have already used container with this code
    add(btn[0].getBtn());
    add(btn[1].getBtn());
    add(btn[2].getBtn());
    add(btn[3].getBtn());
    add(btn[4].getBtn());
    add(btn[5].getBtn());
    add(btn[6].getBtn());
    add(btn[7].getBtn());
    add(btn[8].getBtn());
    add(btn[9].getBtn());
    add(btn[10].getBtn());
    add(btn[11].getBtn());
    add(btn[12].getBtn());
    add(btn[13].getBtn());
    add(btn[14].getBtn());
    add(btn[15].getBtn());
    add(btn[16].getBtn());
    add(btn[17].getBtn());
    add(btn[18].getBtn());
    add(btn[19].getBtn());

    My problem is that how to program aware of which button is used with MyButton class need code for that and i lost here cant think anythink for that.

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    The array btn should contain MyButton objects. You do not need to call the getBtn() method. MyButton objects ARE JButton objects because of the extends.
    how to program aware of which button is used
    The getSource() method should return a reference to your MyButton object. You can cast it to a MyButton and then call any of the MyButton class's methods.

  19. #39
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Quote Originally Posted by Norm View Post
    The array btn should contain MyButton objects. You do not need to call the getBtn() method. MyButton objects ARE JButton objects because of the extends.

    The getSource() method should return a reference to your MyButton object. You can cast it to a MyButton and then call any of the MyButton class's methods.
    now i can understand what you mean. i did remove all btn variables except this on Class MyButton

    btn = new JButton(new ImageIcon(imageFile));

    this code was creating JButton with imagefiles how can i do it with your style ?im thinking on it atm

  20. #40
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: random numbers with no duplicate

    Look at the API doc for the JButton class for a method to set its icon

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. removing duplicate numbers from an array
    By ozzyman in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 08:22 PM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  4. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 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
  •