Avatar billede ronnil Nybegynder
21. december 2003 - 01:22 Der er 5 kommentarer

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);
    }
}
Avatar billede _carsten Nybegynder
21. december 2003 - 10:51 #1
Lige et par hurtige kommentarer

1. I klassen increaseView er din paint OK, så hvis der ikke sker noget må det
    være fordi finalrate altid har den samme værdi

2. gbc.weightx og gbc.weighty skal være en double og ligge i intervallet 0 til 1

3. Grunden til at swatch ikke virker, skyldes du har erklæret den som en Canvas.
  Skal være: increaseView swatch;  ikke  Canvas swatch;

4. Slet repaint() i din update, er dobbelt konfekt

Lidt småting:  Button  bhigher, blower;  ville jeg for læselighedens kalde
                Button  bHigher, bLower;
                String label1, label2, label3    man tror der refereres til JLabes
                så text1, text2, text3 var måske et bedre valg

Vender måske tilbage senere på aftenen.
Avatar billede ronnil Nybegynder
21. december 2003 - 13:24 #2
hmmm det virker stadig ikke, increaseView bliver kaldt med den nye værdi men nægter at repainte, grunden til den sidste repaint er i update er at denne også skal opdateres med et nyt billede....

hmm jeg har endnu ikke sat gbc.weightx og gbc.weighty til double's men ændrer det monstro på noget, mener dog selv jeg har læst at man skal bruge int's...

jeg har også prøvet at smide en repaint ind i increaseView cunstroctoren uden held, ligeså prøvet at overskrive update...
Avatar billede ronnil Nybegynder
21. december 2003 - 13:50 #3
ok nu har jeg fået det til at virke dog er det ikke videre pænt i stedet for at smide interest til increaseView hentede jeg den fra HighLow class'en så koden ser sådan her ud

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;
    increaseView swatch;

    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;
        interest = 1.0;
        swatch = new increaseView(this);
        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");
        swatch.update(this);
        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;
    }
}


class increaseView extends Canvas{
    double increaserate, crate;
    int width, xpos, ypos;
    int finalrate, lala;

    increaseView(HighLow caller){
        update(caller
    }

    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 update(HighLow caller){
        crate = caller.interest;
        increaserate = crate;
        finalrate = findInc(crate);
        repaint();
    }

    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);
    }
}
Avatar billede _carsten Nybegynder
21. december 2003 - 22:03 #4
Fik lige lidt tid, har rodet lidt med din init() metode, og har sat det lidt anderledes op - kan ikke gennemskue hvordan du vil have det til at se ud, så jeg har gjort det så jeg synes det så fornuftigt ud, så må du omstrukturere herfra.

De andre metoder har jeg ikke kigget på, gør sikkert hvad du vil have.

Mht. gridwidth, gridheight, må du hellere læse det igen :
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbagConstraints.html



    public void init(){
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        setLayout(gridbag);

        status = 0;
        current = getRandom();
        bet = 100;
       
        buildConstraints(constraints,0,0,1,1,0.2,0.2);
        bstatus = new Label(Integer.toString(status));
        constraints.anchor = GridBagConstraints.WEST;
        constraints.fill = GridBagConstraints.BOTH;
        gridbag.setConstraints(bstatus,constraints);
        add(bstatus);

        buildConstraints(constraints,0,1,1,1,0.15,0.15);
        constraints.anchor = GridBagConstraints.NORTH;
        constraints.fill = GridBagConstraints.BOTH;
        lbet = new Label(""+bet);
        lbet.setBackground(Color.GREEN);
        gridbag.setConstraints(lbet,constraints);
        add(lbet);
       
        buildConstraints(constraints,0,2,1,1,0.15,0.15);
        constraints.anchor = GridBagConstraints.WEST;
        constraints.fill = GridBagConstraints.BOTH;
        lfull = new Label(""+status*interest);
        gridbag.setConstraints(lfull,constraints);
        add(lfull); 

        buildConstraints(constraints,1,2,1,1,0.15,0.15);
        constraints.anchor = GridBagConstraints.WEST;
        blower = new Button("lower");
        gridbag.setConstraints(blower,constraints);
        add(blower);

        buildConstraints(constraints,2,2,1,1,0.15,0.15);
        constraints.anchor = GridBagConstraints.WEST;
        bhigher = new Button("higher");
        gridbag.setConstraints(bhigher,constraints);
        add(bhigher);

       

        buildConstraints(constraints,1,0,2,3,0.2,0.2);
        constraints.fill = GridBagConstraints.BOTH;
        interest = 1.0;
        swatch = new increaseView(this);
        gridbag.setConstraints(swatch,constraints);
        add(swatch);

             

        img = getImage(getCodeBase(), "images/"+current+".jpg");
    }
   
    void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, double wx, double wy){
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;
        gbc.weightx = wx;
        gbc.weighty = wy;
    }
Avatar billede _carsten Nybegynder
19. januar 2004 - 19:35 #5
Har du fået det til at virke ??
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester