Passing graphics to other methods
Hello, I am trying to pass Graphics g to the update method so that I can use drawstring in the update method. I have only included part of my code below. img,square,index and str have all been declared.
public void paint(Graphics g)
super.paint(g);
g.setFont(font);
g.setColor(Color.blue);
g.drawImage(img,0,100,this);
g.drawString("ready",200,200);
update(g);
public update(Graphics g)
String myArr[] = new String[15];
for (square=0; square<=14;)
for (index=0; index<=14;)
myArr[square] = str.substring(0,index);
square++;
index++;
g.drawString(myArr[5],150,150);
repaint();
When I run the code, I recieve this error
C:\Program Files\JCreator\MyProjects\WORK\WORK\WORK.java:71: invalid method declaration; return type required
public update(Graphics g)
Then when I add return g; just before repaint in update(), I recieve the above error plus this error;-
C:\Program Files\JCreator\MyProjects\WORK\WORK\WORK.java:84: cannot return a value from method whose result type is void
return g;
^
Please tell me how I can correct these errors. I know I could just add the code from the update() method to the paint() method but I would like to keep them seperate.
Thank you
Andrew
Your update method doesn't have a return type specified. Use
public void update(Graphics g)instead of
public update(Graphics g)That should get things compiling.
However, the logic of the painting looks wrong.
Traditionally repaint calls update and update calls paint. You've got update calling repaint (which will then call update) - it'll probably loop around forever (or until it runs out of stack).
Don't get paint to call update. Get update to call paint(g) directly.
Hope this helps.
Similar Messages
-
Is there any other method other than se78 to upload a graphic into SAP R/3?
Hi,
Is there any other method other than se78 to upload a graphic into SAP R/3?
Anita Jeyanhi ,
just use OAER or RSTXLDMC program
it will definately help you
regards
rahul -
How to pass Method Inner Class reference to other method?
Hi All,
I am trying to pass the reference of "method inner class".
Can any one explain me how to pass the reference and where other method will sit in the class, I mean either in Outer Class or in Inner Class ?
Thanks in advance for ur reply :)
package methosInnerClass;
public class MethodLocalInnerClass {
private String outerName;
private static String statOuterName;
public MethodLocalInnerClass(String name, String statName) {
outerName = name;
statOuterName = statName;
public void methodWithLocallClass() {
class MethodInnerClass {
String innerName;
MethodInnerClass(String name) {
innerName = name;
public void displayOuterInner() {
System.out.println("Outer Name: " + outerName + "\nOuter StatName: " + MethodLocalInnerClass.statOuterName + "\nInner Name: " + innerName);
MethodInnerClass methodInner = new MethodInnerClass("Harish");
methodInner.displayOuterInner();
*Pass above reference to other method*
public static void staticMethodWithLocallClass() {
class MethodInnerClass {
String innerName;
MethodInnerClass(String name) {
innerName = name;
public void displayOuterInner() {
// We can not access the non-static instance variable since this method is a static method
//System.out.println("Outer Name: " + outerName + "\nOuter StatName: " + MethodLocalInnerClass.statOuterName + "\nInner Name: " + innerName);
System.out.println("Outer StatName: " + MethodLocalInnerClass.statOuterName + "\nInner Name: " + innerName);
new MethodInnerClass("Shakshi").displayOuterInner();
public static void main(String[] args) {
new MethodLocalInnerClass("Abhishek","Neeshu").methodWithLocallClass();
System.out.println("Calling innerClass within static method !!!");
staticMethodWithLocallClass();
}package donald.test.inner_class;
public class OutterClass {
private String outerName;
private final OutterClass outterClass;
public OutterClass() {
outterClass = this;
public void methodWithInnerClass(final String strValueToPassToInnerClass) {
class InnerClass {
private InnerClass innerClass;
private String innerName;
InnerClass(String name) {
innerName = name;
public void displayOuterInner() {
System.out.println("Non-Static:\tOuter Name: " + outerName + "\tInner Name: " + innerName);
System.out.println("");
System.out.println("final String strValueToPassToInnerClass = " + strValueToPassToInnerClass);
* @return the innerClass
public InnerClass getInnerClass() {
return innerClass;
* @param innerClass the innerClass to set
public void setInnerClass(InnerClass innerClass) {
this.innerClass = innerClass;
* @return the innerName
public String getInnerName() {
return innerName;
* @param innerName the innerName to set
public void setInnerName(String innerName) {
this.innerName = innerName;
InnerClass methodInner = new InnerClass("Inner.Donald");
methodInner.displayOuterInner();
System.out.println("My OutterClass " + outterClass.getOuterName());
// Unknown "MethodInnerClass "
public void passReferenceOfInnerClassToOtherMethod( ) {
* @param args
public static void main(String[] args) {
OutterClass outterClass = new OutterClass();
outterClass.setOuterName("Outter.Donald");
outterClass.methodWithInnerClass("This Donald is so very cool...!!! Yeah...!!!");
* @return the outerName
public String getOuterName() {
return outerName;
* @param outerName the outerName to set
public void setOuterName(String outerName) {
this.outerName = outerName;
* @return the outterClass
public OutterClass getOutterClass() {
return outterClass;
} -
Passing Parameters via Post Method from Webdynpro Java to a web application
Hello Experts,
I want to pass few parameters from a web dynpro application to an external web application.
In order to achieve this, I am referring to the below thread:
HTTP Post
As mentioned in the thread, I am trying to create an additional Suspend Plug parameter (besides 'Url' of type String) with name 'postParams' and of type Map.
But when I build my DC, I am getting the same error which most of the people in the thread have mentioned:
Controller XXXCompInterfaceView [Suspend]: Outbound plug (of type 'Suspend') 'Suspend' may have at most two parameters: 'Url' of type 'string' and 'postParams' of type 'Map'.
I am using SAP NetWeaver Developer Studio Version: 7.01.00
Kindly suggest if this is the NWDS version issue or is it something else that I am missing out.
Also, if it is the NWDS version issue please let me know the NWDS version that I can use to avoid this error.
Any other suggestion/alternative approach to pass the parameters via POST method from webdynpro java to an external web application apart from the one which is mentioned in the above thread is most welcome.
Thanks & Regards,
AnuragHi,
This is purely a java approach, even you can try this for your requirement.
There are two types of http calls synchronous call or Asynchronous call. So you have to choose the way to pass parameters in post method based on the http call.
if it is synchronous means, collect all the values from users/parameters using UI element eg: form and pass all the values via form to the next page is nothing but your web application url.
If it is Asynchronous means, write a http client in java and integrate the same with your custom code and you can find an option for sending parameters in post method.
here you go and find the way to implement Asynchronous scenario,
http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload
http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
http://digiassn.blogspot.com/2008/10/java-simple-httpurlconnection-example.html
Thanks & Regards
Rajesh A -
hi i have a situation where i have to pass parameter to my method i don't what to pass the parameter define from the viewO because am not using parameter to query from the view,i just what to pass it to my procedure,my method is
public void submit_agr(String par_id,String dref_id,String tas_id,String agr_id){
ViewObject sub = this.findViewObject("AGR1");
i don't what to use this-> sub.setNamedWhereClauseParam("tas_id", tas_id); the tas_id is not in AGR1 VIEWO
// sub.
sub.executeQuery();
Row row = sub.first();
par_id = (String)row.getAttribute("par_id");
agr_id = (String)row.getAttribute("id");
callPerformSdmsLogon("SMS_FORM_TO_ADf.delete_agr(?)", new Object[] {par_id,dref_id,tas_id,agr_id});
}i try this AM IN jDEVELOPER 11.1.2.1.0
public void submit_agr(String par_id,String dref_id,String tas_id,String agr_id){
ViewObject sub = this.findViewObject("AGR1");
Row row = sub.first();
sub.setNamedWhereClauseParam("tas_id", new Number(10));-how will i pass this to my procedure
sub.setNamedWhereClauseParam("dref_id", new Number(10));-how will i pass this to my procedure
par_id = (String)row.getAttribute("par_id");
agr_id = (String)row.getAttribute("id");
sub.executeQuery();
callPerformSdmsLogon("SMS_FORM_TO_ADf.delete_agr(?)", new Object[] {par_id,dref_id,tas_id,agr_id});
}how will i pass the two prameter to my procedure
Edited by: Tshifhiwa on 2012/07/01 3:14 PM -
Passing object references to methods
i got the Foo class a few minutes ago from the other forum, despite passing the object reference to the baz method, it prints "2" and not "3" at the end.
but the Passer3 code seems to be different, "p" is passed to test() , and the changes to "pass" in the test method prints "silver 7".
so i'm totally confused about the whole call by reference thing, can anyone explain?
class Foo {
public int bar = 0;
public static void main(String[] args) {
Foo foo = new Foo();
foo.bar = 1;
System.out.println(foo.bar);
baz(foo);
// if this was pass by reference the following line would print 3
// but it doesn't, it prints 2
System.out.println(foo.bar);
private static void baz(Foo foo) {
foo.bar = 2;
foo = new Foo();
foo.bar = 3;
class Passer2 {
String str;
int i;
Passer2(String str, int i) {
this.str = str;
this.i = i;
void test(Passer2 pass) {
pass.str = "silver";
pass.i = 7;
class Passer3 {
public static void main(String[] args) {
Passer2 p = new Passer2("gold", 5);
System.out.println(p.str+" "+p.i); //prints gold 5
p.test(p);
System.out.println(p.str+" "+p.i); //prints silver 7
}private static void baz(Foo foo) {
foo.bar = 2;
foo = new Foo();
foo.bar = 3;This sets the bar variable in the object reference by
foo to 2.
It then creates a new Foo and references it by foo
(foo is a copy of the passed reference and cannot be
seen outside the method).
It sets the bar variable of the newly created Foo to
3 and then exits the method.
The method's foo variable now no longer exists and
now there is no longer any reference to the Foo
created in the method.thanks, i think i followed what you said.
so when i pass the object reference to the method, the parameter is a copy of that reference, and it can be pointed to a different object.
is that correct? -
Passing Array to Another Method
Hello, I created a program with an array in one of the methods. I have been trying to figure out how to correctly pass the array to another method in the same class. I know my problem is in my method delcaration statements. Could someone please show me what I am doing wrong? Please let me know if you have any questions. Thanks for your help.
import javax.swing.*;
import java.util.*;
class Bank1 {
public static void main(String[] args) {
Bank1 bank = new Bank1();
bank.menu();
//Main Menu that initializes other methods
public void menu( ) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the bank. Please choose from the following options:");
System.out.println("O - Open new account");
System.out.println("T - Perform transaction on an account");
System.out.println("Q - Quit program");
String initial = scanner.next();
char uInitial = initial.toUpperCase().charAt(0);
while (uInitial != 'O' && uInitial != 'T' && uInitial != 'Q') {
System.out.println("That was an invalid input. Please try again.");
System.out.println();
initial = scanner.next();
uInitial = initial.toUpperCase().charAt(0);
if (uInitial == 'O') newAccount();
if (uInitial == 'T') transaction();
//Method that creates new bank account
public Person[] newAccount( ) {
Person[] userData = new Person[1];
for (int i = 0; i < userData.length; i++) {
Scanner scanner1 = new Scanner(System.in);
System.out.println("Enter your first and last name:");
String name = scanner1.next();
Scanner scanner2 = new Scanner(System.in);
System.out.println("Enter your address:");
String address = scanner2.next();
Scanner scanner3 = new Scanner(System.in);
System.out.println("Enter your telephone number:");
int telephone = scanner3.nextInt();
Scanner scanner4 = new Scanner(System.in);
System.out.println("Enter an initial balance:");
int balance = scanner4.nextInt();
int account = i + 578;
userData[i] = new Person( );
userData.setName ( name );
userData[i].setAddress ( address );
userData[i].setTelephone ( telephone );
userData[i].setBalance ( balance );
userData[i].setAccount ( account );
System.out.println();
System.out.println("Your bank account number is: " + userData[i].getAccount());
return userData;
menu();
//Method that gives transaction options
public void transaction(Person userData[] ) {
System.out.println(userData[0].getBalance());Thank you jverd, I was able to get that to work for me.
I have another basic question about arrarys now. In all of the arrary examples I have seen, the array is populated all at once using a for statement like in my program.
userData = new Person[50];
for (int i = 0; i < userData.length; i++) In my program though, I want it to only fill the first array parameter and then go up to the main menu. If the user chooses to add another account, the next spot in the array will be used. Can someone point me in the right direction for doing this? -
Passing output to a method, I'm lost!
I want to put all of my "output" including the returns on the three Temp functions in my method DisplayTemperatures, but I'm stuck and don't know how to do this. Please help
import javax.swing.*;
import java.text.DecimalFormat;
public class Temp {
public static void main(String[] args) {
int Hourly_Temperatures[];
String output;
Hourly_Temperatures = new int[24];
DecimalFormat twoDigits = new DecimalFormat ("00");
for (int i = 0;i < Hourly_Temperatures.length;i++) { //inputs temps
Hourly_Temperatures[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter temperature between -50 & 130\nHour: " + twoDigits.format(i) + ":00"));
while (Hourly_Temperatures[i] < -50|| Hourly_Temperatures[i] > 130) {
Hourly_Temperatures[i] = Integer.parseInt(JOptionPane.showInputDialog(null,"Invalid Entry!\nEnter temperature"));
output = "Hour\tTemperature\n"; // build output string
for (int counter = 0; counter < Hourly_Temperatures.length; counter++)
output += twoDigits.format(counter) + ":00" + "\t" + Hourly_Temperatures[counter] + "\n";
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null,outputArea,"24 hour Temperature",JOptionPane.INFORMATION_MESSAGE);
//System.exit(0);
} //end main
public int High_Temp(int Temperatures[]){
int highTemp = Temperatures[0];
for (int counter =0; counter < Temperatures.length; counter++)
if (Temperatures[counter]>highTemp)
highTemp = Temperatures[counter];
return highTemp;
}//end method High_Temp
public int Low_Temp(int Temperatures[]){
int lowTemp = Temperatures[0];
for (int counter =0; counter < Temperatures.length; counter++)
if (Temperatures[counter]<lowTemp)
lowTemp = Temperatures[counter];
return lowTemp;
}//end method Low_temp
public int Average_Temp(int Temperatures[]){
int total = 0;
for (int counter =0; counter < Temperatures.length; counter++)
total += Temperatures[counter];
return total / Temperatures.length;
}// end method Average_Temp
public void Display_Temperatures(int Temperatues[], int Average_Temp){
}// end method Display_Temperatures
} //end classI am not sure I understand what problem you are having in passing output to the method. However, I'll guess:
If you want to pass output to Display_Temperatures() then:
1. change Display_Temperatures to accept a String
2. move display code from main to Display_Temperatures.
3. pass output to Display_Temperatures()
Display_Temperatures(output)If you want to do all the calculations also in Display_Temperatures:
1. change Display_Temperatures to accept only 1 arg - int[]
2. move all the calculations & display code from main to Display_Temperatures
3. pass Hourly_Temperatures to Display_Temperatures()
Display_Temperatures(Hourly_Temperatures)If you make these changes then you will encounter some compilation errors. Post them here only after you've first tried to understand and fix them.
If it is something else you want to do, then you will have to explain it better; at least I've not understood your problem.
The naming standards that almost all jave professionals follow is:
1. not to use underscore char ( _ ) in method names; so Display_Temperatures shd be DisplayTemperatures
2. var names don't have _ char within the name and begin with a lower case; so Hourly_Temperatures shd be hourlyTemperatures
It is generally a good practice to create an instance of the class in the main() method and write the process code in the instance methods instead of putting all that code in main() itself. Read the [url http://java.sun.com/docs/books/tutorial/]tutorials for this starting from the "Your First Cup of Java". -
Passing objects to a method.
Hi,
I was reading this from a book and puzzled by this suggestion:
for example:
public Employee findEmployee(String employeeID);
This method takes a String specifying a unique employee id and returns the Employee object with that id, null otherwise. Don't pass un-necessary objects to methods, and return the appropriate results.
Why not passing objects to methods, I found it is convenient to pass objects to methods, why ?
Thanks in advance !
TeeOf course you can pass objects to methods. A String is also an object.
The author doesn't say to not pass any objects to methods, only no unnecessary objects. He propably means that you don't need a method likepublic Employee findEmployee(String employeeID, String country)to retrieve the Employee if the employee ID by itself is unique or you don't use the country in the method, to give you an example. -
Passing paramaters to a method
I am passing objects to another method.
I do some changes to them and expect the changed objects to be available in the calling method ( since they are supposedely passed by reference) when the execution is returned to it. However, I don't get the changed objects but rather the old ones, before the method execution.
Can someone explain?
Thanks,
Boris.This is an extremely common question. A search through this forum would be very useful to you.
Java does not pass-by-reference in the same way C/C++ do. When you pass an object to a method, you can change the contents of the object using its methods and see the changes after the method completes in the calling method. In this way Java is similar to C/C++.
However, you cannot change an object to equal another object or a new object inside a method and expect the changes to be visible after the method completes in the calling method.
Here is an example.
This will show changes and the line "String" would be printed.
StringBuffer sb = new StringBuffer();
method(sb);
System.out.println(sb);
public void method(StringBuffer buf) {
buf.append("String");
}However, this would not show changes and the line "Old String would be printed:
String string = "Old String";
method(string);
System.out.println(string);
public void method(String string) {
string = "New String";
// string.concat("New String) would also fail to work -
Passing parameters to a method
I am passing objects to another method.
I do some changes to them and expect the changed objects to be available in the calling method ( since they are supposedely passed by reference) when the execution is returned to it. However, I don't get the changed objects but rather the old ones, before the method execution.
Can someone explain?
Thanks,
Boris.I do some changes to them and expect the changed
objects to be available in the calling methodIf you modify an object passed to a method, then the changes made to that object will be visible as soon as they are made.
If you change the parameter so that it references some other object, this will NOT be seen in the calling method.
You can "fake" it by putting the object in question in an array and passing the array. Better though is to just not expect methods to be able to change references they are passed to point at some other object. -
Can anyone show me the syntax to pass Hashtable to a method???
c.buildHT(cal, ht); //where ht is the hashtable...
buildHT(Calendar cal, Hashtable ht) {
Is this OK? I tried....but I am getting errors...Can anyone clarify me about this?????I guess, instead of passing the entire HashTable, why dont you try passing only the keys of the hashtable, and accessing the values of the hashtable, thro' their respective keys
-
Passing information to a method
Im going through the java tutorial about passing information to a method
and i am presented with this code.
class RGBColor {
public int red, green, blue;
class Pen {
int redValue, greenValue, blueValue;
void getRGBColor(RGBColor aColor) {// is aColor a reference for RGBColor ?
aColor.red = redValue; //is aColor then used to access the variables of RGBColor ?
aColor.green = greenValue;// Are these values being initialised here
aColor.blue = blueValue;
RGBColor penColor = new RGBColor();// we are creating an instance of RGBColor called penColor
pen.getRGBColor(penColor);// this is the bit i dont understand where the heck did pen.getRGBColor come from, i cant see anywhere where pen has been declared ..... or instantiated
System.out.println("red = " + penColor.red +
", green = " + penColor.green +
", blue = " + penColor.blue);The follwoing is what it says in the java tutorial about the code
The modifications made to the RGBColor object within the getRGBColor method affect the object created in the calling sequence because the names penColor (in the calling sequence) and aColor (in the getRGBColor method) refer to the same object.
any guidance would be greatDirectly above the code that you don't understand is
o A definition for a class Pen.
o something called "pen" is probably declared somewhere like this:
Pen pen;
or
Pen pen = new Pen(....);
or
Pen pen = something.getPen();
So pen is an instance of Pen, and you can invoke the methods defined in class Pen, thus
pen.getRGBColor(....); -
Hi,
can some one tell me how to call the dsn from other method within the same cfc.
this getDSN method return me the correct dsn that i declared from application.cfm.
<cffunction name="GetDSN" access="public" output="false" hint="Gets the DSN">
<cfargument name="dsn" required="yes">
<cfreturn this />
</cffunction>
how can i get the dsn from above method. What i have is not working..........
<cffunction name="findDupicate" output="no" access="public" returntype="query">
<cfargument name="form" required="yes" type="struct">
<cfquery name="check_duplicate" datasource="#GetDSN#">
SELECT voucherNo
FROM voucher
WHERE voucherNo= <cfqueryparam value="#arguments.form.voucher#" cfsqltype="cf_sql_varchar" />
</cfquery>
<cfreturn check_duplicate>
</cffunction>
ThanksTwo things that immediately spring to mind.
getDsn() is misnamed, or does the wrong thing. It doesn't return a DSN, it returns the entire object (return this).
I your CFQUERY tag you're not calling getDsn(), you're just using it as a value, eg: "getDsn" is a reference to the method itself, but "getDsn()" is actually CALLING the method.
Oh a third thing: something called getDsn() would not normally take an argument that is the very thing it is supposedly getting. IE: why is getDsn() taking an argument of dsn? It should be RETURNING the DSN name, not having it passed into it.
Adam -
Concatenating VARCHAR2 to pass to a Java method
Hi, I've imported in Oracle 10g a Java method which processes a String. I've mapped the String to a varchar2 as follow:
create or replace function PARSE(input in varchar2) RETURN varchar2
as language java
name 'Base64.decodeToString(java.lang.String) return String';
I then built a simple PL/SQL program to build the string to pass to the Java method as follows:
create or replace procedure TEST_PARSE(input_tid in number) is
begin
declare
result varchar2(32767);
cursor object_cur is select TEXT from OBJECTSTORE where TID=input_tid order by rnumber;
object_row object_cur%ROWTYPE;
begin
open object_cur;
loop
fetch object_cur into object_row;
exit when object_cur%NOTFOUND;
result := result || object_cur.TEXT;
end loop;
close object_cur;
result := PARSE(result);
end
The PL/SQL program just concatenates the TEXT column from a bunch of records in the table OBJECTSTORE. The TEXT column is defined as a VARCHAR2(4000).
Now if the SELECT in the TEST_PARSE program returns only 1 record, then everything works and the PARSE Java function returns the processed String.
If the SELECT returns 2 or more records then I get the following warning and the Java method doesn't return anything:
"Warning: PL/SQL procedure successfully completed with compilation errors"
Since I know that the Java method works fine (it has been tested within java programs successfully ) I think the problem is something to do with data types or maybe with the size of the concatenated string.
Any help really appreciated. Thank you!Thanks guys, it still doesn't work, but at least now I can see some error messages:
Error code -24345: ORA-24345: A Truncation or null fetch error occurred
1) The error occurs when I call the Java method. As before this happens only when the select returns more than 1 record and I concatenate two or more TEXT (each one is a VARCHAR2(4000)). However the concatenation works fine so I guess the problem is that the resulting string is too big for the Java method to process.
Or maybe the string returned by the Java method is too big for the PL/SQL varchar2?
2) Also why do I get a compilation error if I try to add the size of the varchar2 in the mapping below?
create or replace function PARSE(input in varchar2) RETURN varchar2
as language java
name 'Base64.decodeToString(java.lang.String) return String';
Maybe you are looking for
-
Problem with primaryClientAction for choice controls
I have a large UIX based application based on templates. Now what I need to do is support partial page rendering using the primaryClientAction tag for any type of control e.g. textInput, checkBox, datePickeer and also choice. So what we are going to
-
InDesign CC has Stopped Working when I try to edit a particular document
Hi, I am working on a large price list document of 200 pages. Basically it's a very long table which spans the whole 200 pages and is formated to show in two columns on each page. My InDesign is working fine normally, and it allowed me to create this
-
OS X Yosemite or Mavericks-Which is better?
Hello, everyone, I want a professional, personal, and meaningful review of OS X Yosemite's new design/UI and the new features. Thank you!
-
Photo Sweeper and Aperture Duplicate Detection
Hi all just a quick question. I have tons of duplicates coming into my Aperture library, basically due to my imports coming from my backup disks which have multiple backups of multiple time points and who knows what is in any of them. I have looked a
-
How do I refund the iCloud storage that I just purchased?
Hi, I recently purchase the iCloud storage of 10gb but I realize that I do not need that much... And the receipt says that I can't get a refund within 15 days, so I would like to know the solution, as I have been searching online for answers but it's