update af Canvas()
Jeg kan ikke få det nye canvas til at update, increaseView class'en virker fint når jeg starter og swatch.update() virker tilsyneladende ikke selvom det egentlig burde...kan det ikke lade sig gøre eller hvordan får jeg det til at virke...
import java.awt.*;
import java.lang.*;
import java.net.*;
public class HighLow extends java.applet.Applet{
String label1, label2, label3;
String label4;
double interest;
int status, current, bet;
Label bstatus, lbet, linterest;
Label lfull;
Button bhigher, blower;
Image img;
Frame window;
Dialog dl;
Canvas swatch, thecard;
public void init(){
GridBagLayout gridbag = new GridBagLayout();
int[] gridwidth = {3,3,3,2};
int[] gridheight = {3,3,3,3};
gridbag.columnWidths = gridwidth;
gridbag.rowHeights = gridheight;
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
status = 0;
current = getRandom();
bet = 100;
buildConstraints(constraints,1,0,1,1,25,20);
label4 = "" + status;
bstatus = new Label(label4);
gridbag.setConstraints(bstatus,constraints);
add(bstatus);
buildConstraints(constraints,2,3,1,1,38,10);
constraints.anchor = GridBagConstraints.WEST;
label2 = "lower";
blower = new Button(label2);
gridbag.setConstraints(blower,constraints);
add(blower);
buildConstraints(constraints,3,3,1,1,36,10);
constraints.anchor = GridBagConstraints.WEST;
label1 = "higher";
bhigher = new Button(label1);
gridbag.setConstraints(bhigher,constraints);
add(bhigher);
buildConstraints(constraints,1,3,1,1,25,10);
constraints.anchor = GridBagConstraints.NORTH;
lbet = new Label(""+bet);
gridbag.setConstraints(lbet,constraints);
add(lbet);
buildConstraints(constraints,1,2,1,1,25,50);
constraints.fill = GridBagConstraints.BOTH;
swatch = new increaseView(1.4);
gridbag.setConstraints(swatch,constraints);
add(swatch);
buildConstraints(constraints,1,3,1,1,25,10);
constraints.anchor = GridBagConstraints.SOUTH;
lfull = new Label(""+status*interest);
gridbag.setConstraints(lfull,constraints);
add(lfull);
img = getImage(getCodeBase(), "images/"+current+".jpg");
}
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public int getRandom(){
Long randomint = new Long(Math.round(Math.random()*13));
int randstr = randomint.intValue();
return randstr;
}
public int increase(int from, int with){
int increasing = from + with;
return increasing;
}
public double increaseRate(double r){
double a = 1+r/10;
return a;
}
void update(int previous,int card, boolean win){
int amount;
if(win == true){
if(card == 0){
amount = 2;
} else {
amount = 1;
}
status = increase(status,amount);
bstatus.setText(""+status);
Integer dstatus = Integer.valueOf(""+status);
interest = increaseRate(dstatus.doubleValue());
lfull.setText(""+bet*interest);
} else {
status = 0;
interest = 1;
bstatus.setText(""+status);
}
current = card;
img = getImage(getCodeBase(), "images/"+card+".jpg");
//dette virker tilsyneladende ikke...
swatch = new increaseView(increase);
swatch.repaint();
repaint();
}
public void paint(Graphics g){
setBackground(Color.blue);
g.drawImage(img,100,50,200,300,this);
}
public boolean action(Event evt, Object arg){
boolean bool = false;
String label = (String)arg;
if(evt.target instanceof Button){
int next = getRandom();
if(label.equals(label1)){
if(current <= next || next == 0){
bool = true;
} else {
bool = false;
}
} else {
if(label.equals(label2)){
if(current >= next || next == 0){
bool = true;
} else {
bool = false;
}
}
}
update(current, next, bool);
return true;
} else return false;
}
}
//selve class indeholdende canvassen
class increaseView extends Canvas{
double increaserate, crate;
int width, xpos, ypos;
int finalrate, lala;
increaseView(double crate){
increaserate = crate;
finalrate = findInc(crate);
}
public int findInc(double crate){
Double lala = new Double(crate);
int lala2 = lala.intValue();
Long rate = new Long(Math.round((crate-lala2)*10));
finalrate = rate.intValue();
return finalrate;
}
public void paint(Graphics g){
g.setFont(new Font("verdana", Font.BOLD, 14));
g.drawString("lala "+findInc(increaserate),10,80);
setBackground(Color.blue);
width = 100;
for(int i = 1; i <= 9; i++){
if(i >= 10-finalrate){
g.setColor(Color.yellow);
} else {
g.setColor(Color.gray);
}
width = width/4*3;
xpos = 100-width;
ypos = i*5;
g.fillRoundRect(xpos,ypos,width,3,5,2);
}
g.setColor(Color.black);
g.drawString(increaserate+"X",50,ypos);
}
}