Results 1 to 2 of 2
  1. #1
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Unhappy applet won't draw a cross

    hello!

    I'm making an applet and I add a mouseListener to it by implement the mouseListeners methods...now when mouseClick I want the applet to draw a cross in the coordinates where the user have clicked. The problem is that the program goes into the mouseClick method and then into my drawCross method...repaint the JApplet and everything but doesn't draw the cross at all :S...why is that? please help! :) here's my code:

    Java Code:
    package kolko;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    
    //VAR SKA JA SÄTTA MOUSELISTENER?????
    public class kolko extends JApplet implements MouseListener{
        boolean kryss = true;
        MinMax[][] rut;
        Graphics2D g2;
    
        @Override
        public void init() {
            setSize(400, 401);
    
            try{
                SwingUtilities.invokeAndWait(new Runnable(){
                    public void run(){
                        create();
                    }
                });
            }
            catch(Exception e){}
    
            addMouseListener(this);
    
        }
    
        public void create() {
            getContentPane().setBackground(Color.WHITE);
        }
    
        @Override
        public void paint(Graphics g){
            //FYLLER UT  TILL VÄNSTER OCH UPP!! dvs linje som ritas på 20 fylls ut på 19 (vänster/upp)
            //ritar ut rutnätet
            super.paint(g);
            g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(2));
            for(int i=20; i<400; i+=20){
                if(i==0){
                    g2.drawLine(1, 20, 1, 380);
                    g2.drawLine(20, 1, 380, 1);
                }
                else{
                    g2.drawLine(i, 20, i, 380);
                    g2.drawLine(20, i, 380, i);
                }
            }
            g2.setStroke(new BasicStroke(1));
    
            //registrerar alla rutorna:
            rut = new MinMax[18][18];
            int countX = 0;
            int countY = 0;
            
            for(int i=20; i<380; i+=20){
                if(countX==18){
                        countX=0;
                }
                for(int j=20; j<380; j+=20){
                    if(countY==18){
                        countY=0;
                    }
                    rut[countX][countY] = new MinMax(i,j);
                    countY++;
                }
                countX++;
            }
    
        }
    
        public void drawCross(MinMax m, Graphics g){
            int xMin = m.drawX;
            int yMin = m.drawY;
            int xMax = m.xMax-1;
            int yMax = m.yMax-1;
            g.drawLine(xMin,yMin,xMax,yMax);
            g.drawLine(xMax, yMin, xMin, yMax);
            repaint();
        }
        public void drawOval(MinMax m){
            int x = m.drawX;
            int y = m.drawY;
            g2.drawOval(x,y,18,18);
        }
    
        public int[] run(int x, int y){
            for(int i=0; i<18; i++){
                for(int j=0; j<18; j++){
                    if(x>=rut[i][j].xMin && x<=rut[i][j].xMax && y>=rut[i][j].yMin && y<=rut[i][j].yMax){
                        return new int[]{i,j};
                    }
                }
            }
            return new int[]{-1,-1};
        }
    
    
        public void mouseClicked(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            int a = run(x,y)[0];
            int b = run(x,y)[1];
            if(a>-1 && b>-1){
                if(kryss){
                drawCross(rut[a][b], g2);
                }
                else{
                    drawOval(rut[a][b]);
                }
                kryss = !kryss;
            }
        }
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
    
    
    }
    thankfull for answers :)

    michcio


    EDIT: solved the problem ... instead of calling the drawCross method from the mouseClicked method...I've now made a LinkedList where I add new cross coordinates while mouse is clicked..and repainth the apllet from mouseClicked as well...here is teh new code :)

    Java Code:
    package kolko;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.LinkedList;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    
    //VAR SKA JA SÄTTA MOUSELISTENER?????
    public class kolko extends JApplet implements MouseListener{
        boolean kryss = true;
        MinMax[][] rut;
        Graphics2D g2;
        LinkedList<MinMax> crosses = new LinkedList<MinMax>();
        LinkedList<MinMax> ovals = new LinkedList<MinMax>();
    
        @Override
        public void init() {
            setSize(400, 401);
    
            try{
                SwingUtilities.invokeAndWait(new Runnable(){
                    public void run(){
                        create();
                    }
                });
            }
            catch(Exception e){}
    
            addMouseListener(this);
    
        }
    
        public void create() {
            getContentPane().setBackground(Color.WHITE);
        }
    
        @Override
        public void paint(Graphics g){
            //FYLLER UT  TILL VÄNSTER OCH UPP!! dvs linje som ritas på 20 fylls ut på 19 (vänster/upp)
            //ritar ut rutnätet
            super.paint(g);
            g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(2));
            for(int i=20; i<400; i+=20){
                if(i==0){
                    g2.drawLine(1, 20, 1, 380);
                    g2.drawLine(20, 1, 380, 1);
                }
                else{
                    g2.drawLine(i, 20, i, 380);
                    g2.drawLine(20, i, 380, i);
                }
            }
            g2.setStroke(new BasicStroke(1));
    
            //registrerar alla rutorna:
            rut = new MinMax[18][18];
            int countX = 0;
            int countY = 0;
            
            for(int i=20; i<380; i+=20){
                if(countX==18){
                        countX=0;
                }
                for(int j=20; j<380; j+=20){
                    if(countY==18){
                        countY=0;
                    }
                    rut[countX][countY] = new MinMax(i,j);
                    countY++;
                }
                countX++;
            }
    
            for(int i=0; i<crosses.size(); i++){
                drawCross(crosses.get(i));
            }
            for(int i=0; i<ovals.size(); i++){
                drawCross(ovals.get(i));
            }
    
        }
    
        public void drawCross(MinMax m){
            int xMin = m.drawX;
            int yMin = m.drawY;
            int xMax = m.xMax-1;
            int yMax = m.yMax-1;
            g2.drawLine(xMin,yMin,xMax,yMax);
            g2.drawLine(xMax, yMin, xMin, yMax);
            
        }
        public void drawOval(MinMax m){
            int x = m.drawX;
            int y = m.drawY;
            g2.drawOval(x,y,18,18);
        }
    
        public int[] run(int x, int y){
            for(int i=0; i<18; i++){
                for(int j=0; j<18; j++){
                    if(x>=rut[i][j].xMin && x<=rut[i][j].xMax && y>=rut[i][j].yMin && y<=rut[i][j].yMax){
                        return new int[]{i,j};
                    }
                }
            }
            return new int[]{-1,-1};
        }
    
    
        public void mouseClicked(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            int[] tmp = run(x,y);
            int a = tmp[0];
            int b = tmp[1];
            if(a>-1 && b>-1){
                crosses.add(rut[a][b]);
                repaint();
            }
        }
    
    
    
        public void mousePressed(MouseEvent e) {
            }
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
    
    }
    still I don't get it why the first method is wrong :S

    cheers
    Last edited by michcio; 06-02-2009 at 12:59 PM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Because every time you call repaint(), paint() gets called and overwrites whatever was drawn on the panel before.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Cross-Browser Issue with show/hide divs
    By jmchaffie in forum New To Java
    Replies: 3
    Last Post: 03-10-2011, 10:41 AM
  2. Replies: 1
    Last Post: 06-30-2010, 08:12 PM
  3. Replies: 1
    Last Post: 11-23-2008, 03:21 AM
  4. JAVA a cross-platform language? I have my doubts!
    By willemjav in forum Advanced Java
    Replies: 34
    Last Post: 10-25-2008, 12:26 AM
  5. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •