Finding Methods In a class
Is there anyway I can find all the methods that are available to
in a class with the help of java code. For example: the Math class. Is there a
programming technique to find all the methods available in the
math class or any class.
Thanx
-Reds
Hi raja17 :
try this example and you'll understand how to find all method of class :
//this code print all the return type and the names of a class :
Class l_class = Class.forName("javax.swing.JFrame");
java.lang.reflect.Method[] l_method = l_class.getMethods();
for (int i =0; i< l_method.length ; i++) {
System.out.println(l_method.getReturnType() +" "+ l_method[i].getName());
Similar Messages
-
We do not implement finder method in Bean class for CMP. As such, where do we implement finder methods such as findInRange() method? How does the container implement my custom "find" methods.
ThanksUsing CMP 2.x, your custom finder and select methods are implemented by the container based on the EJB QL query you provide in the ejb-jar.xml. In CMP 1.1, there was no portable way to express the semantics of the query, so most vendors had a vendor-specific syntax for describing the query.
See chapter 27 of the J2EE 1.4 Tutorial for some examples of using EJB QL :
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
--ken
Kenneth Saks
J2EE SDK Team
SUN Microsystems -
Class cast exception using Finder method
Hello. I'm new to J2EE. I have set up one entity bean but am having trouble
with my current one.
Basically, I have two finder methods:
public ShareHistory findByPrimaryKey(Integer historyId)
throws FinderException, RemoteException;
public Collection findByShare(String shareId)
throws FinderException, RemoteException; findByPrimaryKey works fine, but findByShare causes a class cast exception in java.lang.String.
The stack trace in the server logs shows that it is my ejbActivate method in my entity bean causing the problem:
public void ejbActivate() {
//String numberString = (String) context.getPrimaryKey();
//historyId = new Integer(numberString);
historyId = (Integer) context.getPrimaryKey();
}The stack trace from my client shows that the class cast exception occurs
in the client at the System.out.println("shareid" + ": " + sh.getShareId());
line:
Collection c = sharesHistoryHome.findByShare("DCAN");
Iterator i = c.iterator();
while (i.hasNext()) {
ShareHistory sh = (ShareHistory) i.next();
System.out.println("shareid" + ": " + sh.getShareId());
System.out.println("value" + ": " + sh.getValue());
System.out.println("time" + ": " + sh.getTime());
System.out.println("date" + ": " + sh.getDate());
}//whileAs you can see I tried casting to a string in ejbactivate, but that simply causes an Integer class cast exception during findByprimaryKey instead. How do I allow both Integer and String objects to be used?
Also I am a bit confused as to why the String passed to findByShare(String) is being used in context.getPrimaryKey() in the first place (if that is actually what's happening).Oops my FindByShare method was returning a collection of shareId's (strings) instead of a collection of Integer primary keys, which would explain the class cast exception.
-
Moved to correct forum by moderator
Hi everyone,
where can I find the detail document about certain method of a class?????
e.g. the class CL_GUI_ALV_GRID , when I was going through the class and looking
at the methods, sometimes the method description is just like the method name,
and I cannot know what the method does.
so, I am wondering where I can find the detail information about the class???
Edited by: Matt on Dec 4, 2008 11:55 AMHi,
Most of the times the SAP itself provides the documentaion of the CLASS. when you click on the METHOD name the METHOD DOCUMENTATION button you can see on the application tool bar.
more over the names of the methods suggest what it is going to do.
SET_ATTRIBUTE( sets the attribute)
GET_ATTRIBUTE( gets the attribute value that is provided to the method)
GET_CHILDNODE
BIND_TABLE
etc
like this
regards
Ramchander Rao.K -
Reg - how to find the purpose of methods in a class
hi everyone,
can u plz help me with how to find the purpose of methods in a class???????
a description abt the methods in a class??????
thx in advance,
regards,
balaji.sHi Balaji
Pls find some stuff.
reward pts if help.
The following statements define the structure of a class:
· A class contains components
· Each component is assigned to a visibility section
· Classes implement methods
The following sections describe the structure of classes in more detail.
Class Components
The components of a class make up its contents. All components are declared in the declaration part of the class. The components define the attributes of the objects in a class. When you define the class, each component is assigned to one of the three visibility sections, which define the external interface of the class. All of the components of a class are visible within the class. All components are in the same namespace. This means that all components of the class must have names that are unique within the class.
There are two kinds of components in a class - those that exist separately for each object in the class, and those that exist only once for the whole class, regardless of the number of instances. Instance-specific components are known as instance components. Components that are not instance-specific are called static components.
In ABAP Objects, classes can define the following components. Since all components that you can declare in classes can also be declared in interfaces, the following descriptions apply equally to interfaces.
Attributes
Attributes are internal data fields within a class that can have any ABAP data type. The state of an object is determined by the contents of its attributes. One kind of attribute is the reference variable. Reference variables allow you to create and address objects. Reference variables can be defined in classes, allowing you to access objects from within a class.
Instance Attributes
The contents of instance attributes define the instance-specific state of an object. You declare them using the DATAstatement.
Static Attributes
The contents of static attributes define the state of the class that is valid for all instances of the class. Static attributes exist once for each class. You declare them using the CLASS-DATA statement. They are accessible for the entire runtime of the class.
All of the objects in a class can access its static attributes. If you change a static attribute in an object, the change is visible in all other objects in the class.
The technical properties of instance attributes belong to the static properties of a class. It is therefore possible to refer in a LIKE addition to the visible attributes of a class through the class component selector or through reference variables, without prior creation of an object.
Methods
Methods are internal procedures in a class that define the behavior of an object. They can access all of the attributes of a class. This allows them to change the data content of an object. They also have a parameter interface, with which users can supply them with values when calling them, and receive values back from them The private attributes of a class can only be changed by methods in the same class.
The definition and parameter interface of a method is similar to that of function modules. You define a method meth in the definition part of a class and implement it in the implementation part using the following processing block:
METHOD meth.
ENDMETHOD.
You can declare local data types and objects in methods in the same way as in other ABAP procedures (subroutines and function modules). You call methods using the CALL METHOD statement.
Instance Methods
You declare instance methods using the METHODSstatement. They can access all of the attributes of a class, and can trigger all of the events of the class.
Static Methods
You declare static methods using the CLASS-METHODSstatement. They can only access static attributes and trigger static events.
Special Methods
As well as normal methods, which you call using CALL METHOD, there are two special methods called constructor and class_constructor that are automatically called when you create an object or when you first access the components of a class.
reward pts if help.
deepanker -
How to find the arguments of a static method from the class file
Hi,all !
How to find the arguments of a static method from the class file? for example, when we meet a bytecode "invokestatic", how can I know the arguments of this static method?Hi,all !
How to find the arguments of a static method from the
class file? for example, when we meet a bytecode
"invokestatic", how can I know the arguments of this
static method?You mean
1. The values?
2. Argument names?
3. Argument signatures.
I would suppose for the last that the easiest way would be to parse the signature string.
The first is not possible - not from the class file.
The second is only in the debug information stored in the optional part of the class file. And figuring out the format for that is going to be a problem. -
Not able to reload the data from DB using finder methods
Hi all,
<p>
I am facing a weird problem while using finder methods.
I am using weblogic 8.1 SP3 and entity beans are CMP with DB concurrency.DB is oracle
</p>
<h4>Problem Description</h4>
<p>
I am having one session bean which internally interacts with my entity beans,
Now say my transaction is getting initiated in one of the session bean and I use some finder in it.
</p>
<p>
To make the problem more clear lets say my entity bean is loanBean with loanId as primary key.
Now say method A of session bean initiates the transaction and I use something like
<br>
LoanLocal loanLocal =loanLocalHome.findByLoanId(loanId);
<br>
<b>Note that I am not using findByPrimaryKey method</b>
<br>
now this method A calls some other method B on some session bean which is having Required as its transaction attribute.
<br>
But before the call of B some other thread or background process updates the DB for this loanId and commits,
<br>
now when I fire the same finder in method B I am still getting the old data, ie I am not getting the data which has been modified in DB and committed by some other thread, I still get the old data and when I tried to generate the SQL queries which weblogic is firing, I see
it fires the SQL for every finder other that findByPrimaryKey.
<br>
<b>
Now my problem is I am getting the old data only and I need the new updated data of DB. isolation-level of DB and beans is READCOMMITTED.
Note:: I cant use new transaction to read the data.
</b>
<br>
And I couldn't understand that when weblogic is firing query for every finder then why it should not refresh the data in its cache. Is there any way to disable this kind of caching and say that everytime when i use finder just go to DB and get me the last committed data.
</p>
<br>
Any help in this regard would be very helpful to me.
<br>
Thanks and Regards
<br>
Manish Garg.
</p>Hi,
In my understanding, cache is not involved in this scenario. As you
observed, the container fires sql every time when you invoke this finder.
So, it should just give the result that it got from the DB. Is there a
possibility that the DB is using repeatable_read or serializable for
isolation level?
You can debug further by doing couple of things -
1. Instrument the code in the generated RDBMS java file for the entity bean
(if you use -keepgenerated option for weblogic.ejbc, u can get the source of
this file). This class will have the implementation for ejbFindByLoanId. You
can just print the result set data after the query is fired.
2. Try the same scenario without the ejb container. Like, write a jsp which
will start a user tx and fire the query twice such that there is an update
between the two queries. Note that, you need to use a TxDataSource to get
the JDBC connection so that it will be tx aware.
--Sathish
<Manish Garg> wrote in message news:[email protected]...
Hi all,
<p>
I am facing a weird problem while using finder methods.
I am using weblogic 8.1 SP3 and entity beans are CMP with DB
concurrency.DB is oracle
</p>
<h4>Problem Description</h4>
<p>
I am having one session bean which internally interacts with my entity
beans,
Now say my transaction is getting initiated in one of the session bean and
I use some finder in it.
</p>
<p>
To make the problem more clear lets say my entity bean is loanBean with
loanId as primary key.
Now say method A of session bean initiates the transaction and I use
something like
<br>
LoanLocal loanLocal =loanLocalHome.findByLoanId(loanId);
<br>
<b>Note that I am not using findByPrimaryKey method</b>
<br>
now this method A calls some other method B on some session bean which is
having Required as its transaction attribute.
<br>
But before the call of B some other thread or background process updates
the DB for this loanId and commits,
<br>
now when I fire the same finder in method B I am still getting the old
data, ie I am not getting the data which has been modified in DB and
committed by some other thread, I still get the old data and when I tried
to generate the SQL queries which weblogic is firing, I see
it fires the SQL for every finder other that findByPrimaryKey.
<br>
<b>
Now my problem is I am getting the old data only and I need the new
updated data of DB. isolation-level of DB and beans is READCOMMITTED.
Note:: I cant use new transaction to read the data.
</b>
<br>
And I couldn't understand that when weblogic is firing query for every
finder then why it should not refresh the data in its cache. Is there any
way to disable this kind of caching and say that everytime when i use
finder just go to DB and get me the last committed data.
</p>
<br>
Any help in this regard would be very helpful to me.
<br>
Thanks and Regards
<br>
Manish Garg.
</p> -
CMP Entity Bean with ejb-ql finder methods and INFORMIX database
Hi,
I have some CMP Entity Beans with finder methods defined in ejb-ql. In my ejb-jar, within <entity> definitions I have something like:
<entity>
<abstract-schema-name>BeanName</abstract-schema-name>
<cmp-field><field-name>fieldOne</field-name></cmp-field>
<cmp-field><field-name>fieldTwo</field-name></cmp-field>
<query>
<query-method>
<method-name>findAll</method-name>
<method-params></method-params>
</query-method>
<ejb-ql>SELECT OBJECT(o) FROM BeanName o</ejb-ql>
</query>
<entity>
And in persistent.xml:
<db-properties>
<data-source-name>datasource_name</data-source-name>
</db-properties>
<entity-bean>
<ejb-name>BeanName</ejb-name>
<table-name>table_name</table-name>
<field-map key-type="NoKey">
<field-name>fieldOne</field-name>
<column><column-name>column_one</column-name></column>
</field-map>
<field-map key-type="NoKey">
<field-name>fieldTwo</field-name>
<column><column-name>column_two</column-name></column>
</field-map>
<finder-descriptor>
<method-name>findAll</method-name>
<method-params/>
</finder-descriptor>
Once deployed, on server side, I can found a java source file (with corresponding compiled class file) in path:
j2ee/cluster/server0/apps/companyName/MyEARApp/EJBContainer/temp/temp38837373733/route/to/package/
with names:
BeanName0_0pm.java
BeanName0_0PM.class
and the generated java file contains this code:
public java.util.Enumeration ejbFindAll() throws javax.ejb.FinderException, javax.ejb.EJBException {
TransactionContext tc = pm.getTransactionContext();
Connection conn = null;
PreparedStatement pSt = null;
ResultSet ejb_rs = null;
int status = javax.transaction.xa.XAResource.TMSUCCESS;
try {
conn = pm.getConnectionForFindMethod();
pSt = conn.prepareStatement("SELECT \"O\".\"COLUMN_ONE\",\"O\".\"COLUMN_TWO\", FROM \"TABLE_NAME\" \"O\"");
ejb_rs = pSt.executeQuery();
I'm trying to call this method but it throws a SQLException when preparing the statement.
It seems that Informix does not like this SQL syntax because of upper case names, doble quotes on table alias, or something else.
When editing persistent.xml in netweaver, I can define the element <datasource-vendor> as ORACLE, SAPDB, MS_SQL_SERVER, DB2_UDB_AS400 or DB2_UDB_OS390 but INFORMIX is not an accepted value.
Is there any way to define how this SQL query is build?
Thanks in advance.The return type of the finder method defined in the remote home interface is either the entity bean's remote interface or a collection of objects implementing the entity bean's remote interface. The return type of the finder method defined in the local home interface is either the entity bean's local interface or a collection of objects implementing the entity bean's local interface
-
Calling a method from abstarct class
Hi Experts,
Am working on ABAP Objects.
I have created an Abstract class - with method m1.
I have implemented m1.
As we can not instantiate an abstract class, i tried to call the method m1 directly with class name.
But it it giving error.
Please find the code below.
CLASS c1 DEFINITION ABSTRACT.
PUBLIC SECTION.
DATA: v1 TYPE i.
METHODS: m1.
ENDCLASS. "c1 DEFINITION
CLASS c1 IMPLEMENTATION.
METHOD m1.
WRITE: 'You called method m1 in class c1'.
ENDMETHOD. "m1
ENDCLASS. "c1 IMPLEMENTATION
CALL METHOD c1=>m1.
Please tell me what is wrong and how to solve this problem.
Thanks in Advance.Micky is right, abstract means not to be instantiated. It is just a "template" which you can use for all subsequent classes. I.e you have general abstract class vehicle . For all vehicles you will have the same attributes like speed , engine type , strearing , gears etc and methods like start , move etc.
In all subsequent classes (which inherit from vehicle) you will have more specific attributes for each. But all of these classes have some common things (like the ones mentioned above), so they use abstract class to define these things for all of them.
Moreover there is no sense in creating instance (real object) of class vehicle . What kind of physical object would vehicle be? there is no such object in real world, right? For this we need to be more precise, so we create classes which use this "plan" for real vehicles. So the abstract class here is only to have this common properties and behaviour defined in one place for all objects which will have these. Abstract object however cannot be created per se. You can only create objects which are lower in hierarchy (which are specific like car , ship, bike etc).
Hope this claryfies what we need abstract classes for.
Regards
Marcin -
Calling results of a method from another class
Very very new to Java, so apologies for the lack of basic knowledge. I am making a programme with 3 classes. One class gathers details about a module. Another about the results of this module (which requires some of the information inputted for the first class). For some reason I cannot find how to use results created in the first class in this second class. How do you call the results of a method from one class in another class?
Thanks.Thank you.
I am given the following information:
'_ModuleRecord_
This class is used to record information about a module taken by a single student. It has a constructor that takes three parameters:
a Module,
an int representing the examination mark achieved by a student, and
an int representing the coursework mark achieved by a student.
The class has another constructor that takes a single Module parameter.'
and the code looks like this:
public class ModuleRecord
public ModuleRecord(Module m, int eMark, int cMark)
public ModuleRecord(Module m)
} I am a bit confused by the whole thing to be honest. I assume that the Module is referring to the other class, but how do I forge the link between them here? -
Calling a method from another class or accessing a component from another
Hi all
im trying to make a find/replace dialog box
my main application form has a jtextpane and when i open up the find and replace dialog box it has two textboxes (find and replace)
now i have the code to do the finding on my jtextpane but how do i call that code to do the find method?
I have tried having the code in my main application class but then how do i call that method from my dialog box class?
ive also tried having the code in my dialog box class, but then how to i tell it to work on my jtextpane which is in my main ap class?well if someone had been nice enough to provide me
with a tutorial i wouldnt have gotten into this
muddle, no need to be rude is there!I'm not rude. And you also wouldn't have gotten into the muddle if you searched yourself. This site provides many very good tutorials about all kinds of stuff.
http://java.sun.com/docs/books/tutorial/java/javaOO/classes.htmlAmong other things, it mentions that "static" defines everything that belongs to a class, as opposed to an object. -
Cant find method? and i cant figure out how to round a decimal....
when i compile this it says "getAverage not found in class AverageGradeUser" - look at actionPeformed() to find where its used at....how do i fix this, i had it working but somehow messed it up and when the new average is figured and put in the results textfield, how can u round it off to the nearest tenth (0.1)..ive tried using NumberFormat and related things but couldnt get it to work
please help me
thanks
p.s. keep in mind i am a rookie-rookie-rookie in java programming
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AverageGradeUser extends Applet implements ActionListener
/****InstanceVariables****/
public TextField input;
/*****InitMethod****/
public void init()
Label directions = new Label("Enter a grade percent: ");
input = new TextField(10);
Label results = new Label("Your new average is: ");
TextField output = new TextField(10);
add(directions);
add(input);
add(results);
add(output);
output.setEditable(false);
input.addActionListener(this);
} // init()
public void actionPerformed(ActionEvent e)
double grade = Double.parseDouble(input.getText());
if (e.getSource() == input)
getAverage(grade);
} // actionPerformed()
} // AverageGradeUser class
============================================================
(the classes are in separate files but both are in the project)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AverageGrade
/****InstanceVariables****/
private double total = 0;
private double counter = 0;
private double average = 0;
private TextField input;
private TextField output;
/****ConstructorMethod****/
public AverageGrade()
/****AccessMethod****/
public void getAverage(double g)
total += g;
counter ++;
average = total / counter;
output.setText(average + " " + getLetterGrade(average));
input.setText("");
} // getAverage()
private String getLetterGrade(double avg)
if (avg >= 90)
return "A";
else if (avg >= 80)
return "B";
else if (avg >= 70)
return "C";
else if (avg >= 60)
return "D";
else if (avg < 60)
return "F";
else
return "error";
} // getLetterGrade()
} // AverageGrade classAccording to the code you have posted, method "getAverage()" is defined in class "AverageGrade" -- and not in class "AverageGradeUser". Therefore, you have two choices (in my opinion):
1. Add the definition of method "getAverage()" to class "AverageGradeUser"
2. Change the "actionPerformed()" method:
Replace the line:
getAverage(grade)with
new AverageGrade().getAverage(grade)Hope this helps you.
Good Luck,
Avi. -
Finder method in container-managed entity bean
Finder method of a container entity bean is taking the primary key class object of another entity bean. How to map this in the xml descriptor file weblogic-cmp-rdbms.xml for this type of beans.
I am trying to deploy the jasmine computer store of Mastering EJB by EdRoman.
I couldn't write the deployment file weblogic-cmp-rdbms.xml for Order Entity bean which uses CustomerPK as an argument in its finder method.
Please tell me how to write this. I am trying on weblogic 5.1
Thanx in advance
Srivatsahello,
in container managed persistence, you specify on your xml file the name of your table. so the primary key you want to use have to be a foreign key in your table. otherwise i don't think that you can use a container managed persistence.
Najib. -
Field-symbols as parameters to the method of a class
Hi All,
I am having an doubt regarding the field-symbols.Can we pass the field-symbols as a parameter to the method of a class.If yes can anyone tell me how to do this. Before posting I have searched regarding it in google but I did not find any better solution.Though I have seen some examples regarding the passing of field symbols as a parameter those scenarios does not match with my report as my report varies dynamically based on selection criteria.
Below is the snippet of my code regarding the passing of field-symbols as a parameter.
methods: final_data importing <fs_h_line>TYPE any
<fs_h> TYPE STANDARD TABLE
exporting <fs_f_line> TYPE any
<fs_f> TYPE STANDARD TABLE,
CALL METHOD l_obj->final_data exporting <fs_h_line> = <fs_header_line>
<fs_h> = <fs_header>
importing <fs_f_line> = <fs_final_line>
<fs_f> = <fs_final>.
With the above code I am getting an error.Check whether it is correct or not.If not suggest the solution to resolve the issue.
Regards,
Chakradhar.Hi
Maybe if you change this code below to field-symbol, it can work:
DATA: tl_header_csv TYPE STANDARD TABLE OF yol_header_arquivo,
tl_csv_aux TYPE textline_t .
DATA: wl_header_csv LIKE LINE OF tl_header_csv.
converter_csv_al11_itab( EXPORTING im_t_csv = tl_csv_aux
IMPORTING ex_w_sap = wl_header_csv
CHANGING ex_t_sap = tl_header_csv ).
METHOD converter_csv_al11_itab.
IM_T_CSV Importing Type TEXTLINE_T
EX_W_SAP Exporting Type ANY
EX_T_SAP Changing Type STANDARD TABLE -
Problem using repaint() method from another class
I am trying to make tower of hanoi...but unable to transfer rings from a tower to another...i had made three classes....layout21 where all componentents of frame assembled and provided suitable actionlistener.....second is mainPanel which is used to draw the rods n rings in paintComponent.....and third is tower in which code for hanoi is available...i had made an object of mainPanel at layoout21 n tower but i m not able to call repaint from tower..gives an error : cannot find the symbol....method repaint in tower.
code fragments od three classes are:
LAYOUT21
class layout21 extends JFrame implements ActionListener
{ private Vector rod1 = new Vector();
private Vector rod2 = new Vector();
private Vector rod3 = new Vector();
private String elem; //comment
public String r22;
public boolean in=false;
public int count=0; //no of times the transfer to other rods performed
private int r3,rings; // current no of rings
private JComboBox nor,col;
private JLabel no;
private JLabel moved;
private JLabel no1;
private JButton start;
private JButton ref;
private AboutDialog dialog;
private JMenuItem aboutItem;
private JMenuItem exitItem;
private tower t;
final mainPanel2 p =new mainPanel2();
public layout21()
{ t = new tower();
Toolkit kit =Toolkit.getDefaultToolkit();
Image img = kit.getImage("java.gif");
setIconImage(img);
setTitle("Tower Of Hanoi");
setSize(615,615);
setResizable(false);
setBackground(Color.CYAN);
JMenuBar mbar = new JMenuBar();
setJMenuBar(mbar);
JMenu fileMenu = new JMenu("File");
mbar.add(fileMenu);
aboutItem = new JMenuItem("About");
aboutItem.addActionListener(this);
fileMenu.add(aboutItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
Container contentPane =getContentPane();
JPanel bspanel = new JPanel();
JPanel bnpanel = new JPanel();
setBackground(Color.CYAN);
//JComboBox
nor = new JComboBox();
nor.setEditable(false);
nor.addItem("3");
nor.addItem("4");
nor.addItem("5");
nor.addItem("6");
nor.addItem("7");
nor.addItem("8");
nor.addItem("9");
bspanel.add(nor);
col = new JComboBox();
col.setEditable(false);
col.addItem("BLACK");
col.addItem("GREEN");
col.addItem("CYAN");
bspanel.add(col);
JLabel tl = new JLabel("Time");
tl.setFont(new Font("Serif",Font.BOLD,12));
bspanel.add(tl);
JTextField tlag = new JTextField("0",4);
bspanel.add(tlag);
start =new JButton("Start");
bspanel.add(start);
ref =new JButton("Refresh");
bspanel.add(ref);
JButton end =new JButton("End");
bspanel.add(end);
start.addActionListener(this);
nor.addActionListener(this);
col.addActionListener(this);
ref.addActionListener(this);
end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose(); // Closes the dialog
contentPane.add(bspanel,BorderLayout.SOUTH);
JLabel count = new JLabel("No of Transfer reguired:");
count.setFont(new Font("Serif",Font.BOLD,16));
bnpanel.add(count);
no = new JLabel("7");
no.setFont(new Font("Serif",Font.BOLD,16));
bnpanel.add(no);
JLabel moved = new JLabel("Moved:");
moved.setFont(new Font("Serif",Font.BOLD,16));
bnpanel.add(moved);
no1 = new JLabel("0");
no1.setFont(new Font("Serif",Font.BOLD,16));
bnpanel.add(no1);
contentPane.add(bnpanel,BorderLayout.NORTH);
contentPane.add(p,BorderLayout.CENTER);
String r = (String)nor.getSelectedItem();
rings = Integer.valueOf(r).intValue();
p.draw(rings,1) ;
public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
if(source == start)
r3 = Integer.valueOf((String)nor.getSelectedItem()).intValue();
p.transfer(false);
t.initialise(rod1,rod2,rod3,0);
t.towerOfHanoi(r3);
//repaint();
if(source == ref)
{ rod1.removeAllElements() ;
rod2.removeAllElements() ;
rod3.removeAllElements() ;
count=0;
r3 = Integer.valueOf((String)nor.getSelectedItem()).intValue();
p.draw(r3,1);
p.transfer(true);
no1.setText(""+0);
p.trans_vec(rod1,rod2,rod3);
t.initialise(rod1,rod2,rod3,0);
System.out.println("");
repaint();
if(source == nor)
{ JComboBox j = (JComboBox)source;
String item = (String)j.getSelectedItem();
int ring1 = Integer.valueOf(item).intValue();
int a=1;
for(int i=1;i<=ring1;i++)
{ a = a*2;
a=a-1;
no.setText(""+a);
p.draw(ring1,1);
repaint();
if(source == aboutItem)
{ if (dialog == null) // first time
dialog = new AboutDialog(this);
dialog.setVisible(true);
if(source == exitItem)
{ System.exit(0);
if (source==col)
{ JComboBox j = (JComboBox)source;
String item = (String)j.getSelectedItem();
repaint();
TOWER
class tower extends Thread
{ private Vector rod1 = new Vector();
private Vector rod2 = new Vector();
private Vector rod3 = new Vector();
private int count ;
private String elem;
final mainPanel2 z =new mainPanel2();
public void initialise(Vector r1,Vector r2,Vector r3,int c)
{ rod1 = r1;
rod2 = r2;
rod3 = r3;
count =c;
public void towerOfHanoi(int rings)
for(int i=0;i<rings;i++)
rod1.add(" "+(i+1));
System.out.println("rod1:"+rod1.toString());
hanoi(rings,1,2);
public void hanoi(int m,int i, int j)
if(m>0)
{ hanoi(m-1,i,6-i-j);
if(i==1 && j==2 && rod1.isEmpty()==false)
{ count++;
//no1.setText(""+count);
elem = (String)rod1.remove(0);
rod2.add(0,elem);
//z.trans_vec(rod1,rod2,rod3);
repaint(); //NOT ABLE TO USE METHOD HERE...WHY??
//z.hanoi_paint();
try
this.sleep(2000);
catch (Exception e) { e.printStackTrace() ; }
System.out.println(count+". ROD 2:"+rod2.toString());
if(i==1 && j==3 && rod1.isEmpty()==false)
{ count++;
//no1.setText(""+count);
elem = (String)rod1.remove(0);
rod3.add(0,elem);
//z.trans_vec(rod1,rod2,rod3);
repaint();//
// z.hanoi_paint();
try
this.sleep(2000);
catch (Exception e) { e.printStackTrace() ; }
System.out.println(count+". ROD 3:"+rod3.toString());
if(i==2 && j==1 && rod2.isEmpty()==false)
{ count++;
//no1.setText(""+count);
elem = (String)rod2.remove(0);
rod1.add(0,elem);
//z.trans_vec(rod1,rod2,rod3);
repaint();
//z.hanoi_paint();
try
this.sleep(2000);
catch (Exception e) { e.printStackTrace() ; }
System.out.println(count+". ROD 1:"+rod1.toString());
if(i==2 && j==3 && rod2.isEmpty()==false)
{ count++;
//no1.setText(""+count);
elem = (String)rod2.remove(0);
rod3.add(0,elem);
//z.trans_vec(rod1,rod2,rod3);
repaint();
//z.hanoi_paint();
try
this.sleep(2000);
catch (Exception e) { e.printStackTrace() ; }
System.out.println(count+". ROD 3:"+rod3.toString());
if(i==3 && j==1 && rod3.isEmpty()==false)
{ count++;
//no1.setText(""+count);
elem = (String)rod3.remove(0);
rod1.add(0,elem);
//z.trans_vec(rod1,rod2,rod3);
repaint();
//z.hanoi_paint();
try
this.sleep(2000);
catch (Exception e) { e.printStackTrace() ; }
System.out.println(count+". ROD 1:"+rod1.toString());
if(i==3 && j==2 && rod3.isEmpty()==false)
{ count++;
//no1.setText(""+count);
elem = (String)rod3.remove(0);
rod2.add(0,elem);
//z.trans_vec(rod1,rod2,rod3);
repaint();
//z.hanoi_paint();
try
this.sleep(2000);
catch (Exception e) { e.printStackTrace() ; }
System.out.println(count+". ROD 2:"+rod2.toString());
hanoi(m-1,6-i-j,j);
MAINPANEL
class mainPanel2 extends JPanel //throws IOException
public Vector line = new Vector();
public Vector rod11= new Vector();
public Vector rod22= new Vector();
public Vector rod33= new Vector();
public int no_ring;
public int rod_no;
String pixel;
StringTokenizer st,st1;
int x,y;
public boolean initial =true;
public void paintComponent(Graphics g)
{ System.out.println("repaint test");
bresenham(100,60,100,360);
bresenham(101,60,101,360);
bresenham(102,60,102,360);
bresenham(103,60,103,360);
bresenham(104,60,104,360);
g.setColor(Color.BLUE);
while(line.size()>0)
{ pixel = (String)line.remove(0);
st = new StringTokenizer(pixel);
x = Integer.valueOf(st.nextToken()).intValue();
y = Integer.valueOf(st.nextToken()).intValue();
g.drawLine(x,y,x,y);
bresenham(300,60,300,360);
bresenham(301,60,301,360);
bresenham(302,60,302,360);
bresenham(303,60,303,360);
bresenham(304,60,304,360);
while(line.size()>0)
{ pixel = (String)line.remove(0);
st = new StringTokenizer(pixel);
x = Integer.valueOf(st.nextToken()).intValue();
y = Integer.valueOf(st.nextToken()).intValue();
g.drawLine(x,y,x,y);
bresenham(500,60,500,360);
bresenham(501,60,501,360);
bresenham(502,60,502,360);
bresenham(503,60,503,360);
bresenham(504,60,504,360);
while(line.size()>0)
{ pixel = (String)line.remove(0);
st = new StringTokenizer(pixel);
x = Integer.valueOf(st.nextToken()).intValue();
y = Integer.valueOf(st.nextToken()).intValue();
g.drawLine(x,y,x,y);
bresenham(0,361,615,361);//used to get a pixel according to algo.. . func not provided
bresenham(0,362,615,362);
bresenham(0,363,615,363);
bresenham(0,364,615,364);
bresenham(0,365,615,365);
while(line.size()>0)
{ pixel = (String)line.remove(0);
st = new StringTokenizer(pixel);
x = Integer.valueOf(st.nextToken()).intValue();
y = Integer.valueOf(st.nextToken()).intValue();
g.drawLine(x,y,x,y);
if(initial==true)
g.setColor(Color.RED);
for(int i = no_ring;i>0;i--)
{ g.drawLine(100-(i*8),360-(no_ring - i)*10,100+(i*8)+5,360-(no_ring - i)*10);
g.drawLine(100-(i*8),359-(no_ring - i)*10,100+(i*8)+5,359-(no_ring - i)*10);
g.drawLine(100-(i*8),358-(no_ring - i)*10,100+(i*8)+5,358-(no_ring - i)*10);
g.drawLine(100-(i*8),357-(no_ring - i)*10,100+(i*8)+5,357-(no_ring - i)*10);
g.drawLine(100-(i*8),356-(no_ring - i)*10,100+(i*8)+5,356-(no_ring - i)*10);
// draw for each rod
//System.out.println("rod11:"+rod11);
//System.out.println("rod22:"+rod22);
//System.out.println("rod33:"+rod33);
int r1 = rod11.size();
int r2 = rod22.size();
int r3 = rod33.size();
String rd1,rd2,rd3;
int r11,r12,r21,r22,r31,r32;
if(initial == false)
{ g.setColor(Color.RED);
while(rod11.size()>0)
{ r12 = rod11.size()-1;
rd1 = (String)rod11.remove(r12);
r11 = Integer.valueOf(rd1).intValue();
g.drawLine(100-((r11+1)*8),360-(r1 - (r11+1))*10,100+((r11+1)*8)+5,360-(r1 - (r11+1))*10);
g.drawLine(100-((r11+1)*8),359-(r1 - (r11+1))*10,100+((r11+1)*8)+5,359-(r1 - (r11+1))*10);
g.drawLine(100-((r11+1)*8),358-(r1 - (r11+1))*10,100+((r11+1)*8)+5,358-(r1 - (r11+1))*10);
g.drawLine(100-((r11+1)*8),357-(r1 - (r11+1))*10,100+((r11+1)*8)+5,357-(r1 - (r11+1))*10);
g.drawLine(100-((r11+1)*8),356-(r1 - (r11+1))*10,100+((r11+1)*8)+5,356-(r1 - (r11+1))*10);
while(rod22.size()>0)
{ g.setColor(Color.RED);
r22 = rod22.size()-1;
System.out.println("TEST *************************:"+r22);
try
// e.printStackTrace();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr) ;
br.readLine() ;
}catch(Exception f) {}
rd2 = ((String)rod22.remove(r22)).trim();
r21 = Integer.valueOf(rd2).intValue();
g.drawLine(300-((r22+1)*8),360-(r2 - (r22+1))*10,300+((r22+1)*8)+5,360-(r2 - (r22+1))*10);
g.drawLine(300-((r22+1)*8),359-(r2 - (r22+1))*10,300+((r22+1)*8)+5,359-(r2 - (r22+1))*10);
g.drawLine(300-((r22+1)*8),358-(r2 - (r22+1))*10,300+((r22+1)*8)+5,358-(r2 - (r22+1))*10);
g.drawLine(300-((r22+1)*8),357-(r2 - (r22+1))*10,300+((r22+1)*8)+5,357-(r2 - (r22+1))*10);
g.drawLine(300-((r22+1)*8),356-(r2 - (r22+1))*10,300+((r22+1)*8)+5,356-(r2 - (r22+1))*10);
while(rod33.size()>0)
{ g.setColor(Color.RED);
r32 = rod33.size()-1;
rd3 = (String)rod33.remove(r32);
r31 = Integer.valueOf(rd3).intValue();
g.drawLine(500-((r32+1)*8),360-(r3 - (r32+1))*10,500+((r32+1)*8)+5,360-(r3 - (r32+1))*10);
g.drawLine(500-((r32+1)*8),359-(r3 - (r32+1))*10,500+((r32+1)*8)+5,359-(r3 - (r32+1))*10);
g.drawLine(500-((r32+1)*8),358-(r3 - (r32+1))*10,500+((r32+1)*8)+5,358-(r3 - (r32+1))*10);
g.drawLine(500-((r32+1)*8),357-(r3 - (r32+1))*10,500+((r32+1)*8)+5,357-(r3 - (r32+1))*10);
g.drawLine(500-((r32+1)*8),356-(r3 - (r32+1))*10,500+((r32+1)*8)+5,356-(r3 - (r32+1))*10);
why i m not able to use repaint() method in tower class? from where i can use repaint() methodi can't read your code - not formatted with code tags
I have no chance of getting it to compile (AboutDialog class?? p.draw() ??)
here's a basic routine - add a couple of things to this to demonstrate what is not
being redrawn
(compare the readability of below code (using tags) to yours)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame
public Testing()
setSize(400,300);
setLocation(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
final DrawPanel dp = new DrawPanel();
JButton btn = new JButton("Change Text Location/Repaint");
getContentPane().add(dp,BorderLayout.CENTER);
getContentPane().add(btn,BorderLayout.SOUTH);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
dp.x = (int)(Math.random()*300);
dp.y = (int)(Math.random()*150)+50;
repaint();}});
public static void main(String[] args){new Testing().setVisible(true);}
class DrawPanel extends JPanel
int x = 50, y = 50;
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawString("Hello World",x,y);
}
Maybe you are looking for
-
I MICHAEL WUESCHINSKI DONATED MONEY FROM PAYPAL AND I BETTER GET T SHIRT BECAUSE YOU KRRP ASKING ME LIKE I DIDN'T DONATE. WHAT IS THE PROBLEM ARE YOU GOING TO ASK EVERYTIME I SIGN ON THEN IF YOU ARE REFUND MY DONATION NOW/ MICHAEL WUESCHINSKI 228 VRU
-
How to use the Function keys as function keys instead of action keys?
When I press F1 or any of the other F keys it does what the symbol says on the key (volume/skip track etc). How do I actually use the F keys and not the symbols?
-
How can I export many short videos from one project?
We had an awards ceremony, and the entirety of the program was filmed and is now saved on my computer as one large, continuous file. I would like to edit it down to only the acceptance speechers (there are about 10 or so) and export them to MPEGs to
-
Join two images together in PS7.
Hi folks I am exasperated by PS 7 again. Someone has emailed me an old document in two sections (JPGs) and I want to "knit" them together again to make one document (JPG). I've spent ages in PS7 trying to do this, to no avail. Help, please?
-
Google calendar not syncing now
hi, im really hoping for some help. I have previously been syncing my calendar on my iphone 3g with google calendar. everything has been working fine however, I have had to use a new computer to upgrade my software on my phone. now i cant get my cale