This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ben&Sam
Ben&Sam wrote ...

2015/2/2

How to edit a GreenfootImage

Ben&Sam Ben&Sam

2015/2/2

#
I need to be able to add to a greenfootImage that has been read from a file. It has already been edited, so I cannot set the image again or the previous image is lost. I have a file, another file is put into this and it changes, then i need the user to be able to change the origial file while the primary changes are kept, can anyone help, see code below Regards, The silkiest team on the greenfoot scene }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if(Greenfoot.mouseClicked(l2))
            {
             String Label1 = ReadasString();
             String text21 = ReadasString2();
             GreenfootImage imageprint12 = new GreenfootImage (text21, 25, Color.BLACK, Color.ORANGE);
             l2.setImage(imageprint12);
             String[] partsa = text21.split(",");
             String myleetstring=Label.replace("#","A")
                                      .replace("*","M")
                                      .replace("%","N");
             GreenfootImage imagee = new GreenfootImage (""+myleetstring, 25, Color.BLACK, Color.ORANGE);
             l1.setImage(imagee);                            
               
            
            String Labela = ReadasString2();
            String[] partsa = Label.split("\n");
            String alla = partsa[0] + partsa[1] + partsa[2] + partsa[3];
            GreenfootImage textImageA = new GreenfootImage (Labela, 25, Color.BLACK, Color.ORANGE);
             
            if(Greenfoot.mouseClicked(l3))
            {
               String mystring1 = JOptionPane.showInputDialog("Enter a symbol to be replaced");
               String mystring2 = JOptionPane.showInputDialog("Enter a letter as replacement");
               String myleetstring=Label.replace(""+mystring1,""+mystring2);
               GreenfootImage imageeno = new GreenfootImage (""+myleetstring, 25, Color.BLACK, Color.ORANGE);
               l1.setImage(imageeno); 
                 
                 
            }
danpost danpost

2015/2/2

#
You can create a copy of the image set to the actor with:
1
GreenfootImage dupe = new GreenfootImage(l1.getImage());
Changes to 'dupe' will not be implemented on the image set to the actor.
Ben&Sam Ben&Sam

2015/2/2

#
We have programmed everything in the world we have one actor only used for reference, so where would this code fit into the world, also is the word dupe interchangeable?
danpost danpost

2015/2/2

#
To declare a variable of any kind (local to a method, a field in a class, whatever), the following format is used:
1
VariableType variableName = variableExpressioin;
where the expression on the right is evaluated and assigned to a new variable called 'variableName' that is declared to hold a value (or object reference) of type 'VariableType'. VariableType is either a primary data type (int, float, double, boolean, etc.) which means the variable holds a value or true/false, or it is the name of a class (String, GreenfootImage, Point, Player, Actor, World, etc) which means that it 'holds' an object instance created from the named class. variableExpression is any expression that produces a value or an object reference of VariableType type. variableName is a non-java keyword name that the programmer gives to the variable. The bottom of this page of the java tutorials explains the rules/conventions for naming variables.
Ben&Sam Ben&Sam

2015/2/2

#
Yeah obviously, but we want to know where to put that line of code? Also would that mean by whenever we type l1.getImage() will it go into dupe? This would make it so that all edits are saved and added into the primary class, correct?
danpost danpost

2015/2/2

#
Ben&Sam wrote...
would that mean by whenever we type l1.getImage() will it go into dupe?
No. I only used 'l1.getImage()' to show an image of an actor object being given to the Greenfootimage constructor. When you use 'new GreenfootImage(/** image */)', where 'image' is any expression that returns a GreenfootImage object, the GreenfootImage constructor makes a copy of that image object (hence -- 'new'). That new image is then assigned to a variable, which I gave the name 'dupe' -- a name that can henceforth be used to refer that new image (at least locally -- within the method). If the image, after being edited, is to be held for later use, it can be assigned to a field (variable) that is declared within the class (but not within the method). The line of code, as I gave it, is probably not exactly what you need. It is just giving you a general idea of how an image can be copied so that you can edit it without ruining the original.
This would make it so that all edits are saved and added into the primary class, correct?
I have no idea what you mean by this. Nothing is saved unless you store it in a field that can be referred to at a later time (if you are wondering, the 'setImage' method stores the image in a hidden GreenfootImage type field in the Actor class of the actor whose image is set). Where you declare the field is where it will be located.
Ben&Sam Ben&Sam

2015/2/3

#
The problem is we are rewriting the image as the two if statements are separate we want to make a global variable but we dont know when and how to implement it
danpost danpost

2015/2/3

#
Ben&Sam wrote...
The problem is we are rewriting the image
You mean 'rewriting code' -- don't you?
we want to make a global variable
What type of data will this variable hold? What will it be used for? If you gave a general idea of what the code is supposed to be doing and how it relates to your project, it might help in our understanding of what might be needed here.
Ben&Sam Ben&Sam

2015/2/3

#
Its for a code puzzle and this is this is the bit where hwaving already entered the clues the user needs to be able to add symbol-letter pairings however atm when they do this the clues are removed and eveytime you enter a new pairing the old one is deleted
danpost danpost

2015/2/3

#
It looks like you need to create a list of pairings. However, since you already know that both sides of the pairings are one-character strings, you can use a simple String object to store the pairings. Start with:
1
private String pairings = "";
as a field within the class (declared outside of any method). Then, as each pairing is entered, add the pair to the String object:
1
pairings += myString1+myString2;
To apply the pairings to Label, use something like this:
1
for (int i=0; i<pairings.length(); i+=2) Label.replace(pairings.charAt(i), pairings.charAt(i+1));
That should do it.
You need to login to post a reply.