Java Help

3dfx

New member
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MyDraughts extends Applet implements MouseListener,
                 MouseMotionListener  {

    private Point counter1, counter2, counter3, mouse;
    private int select;

    public void init() {
       this.addMouseMotionListener(this);
       this.addMouseListener(this);
       select = 0;
       counter1 = new Point(20,60);
       counter2 = new Point(40,60);
       counter3 = new Point(60,60);
       mouse = new Point();
       }

    public void paint (Graphics g) {
       
       g.fillOval(counter1.x, counter1.y, 5,5);
       g.fillOval(counter2.x, counter2.y, 5,5);
       g.fillOval(counter3.x, counter3.y, 5,5);
       g.drawString("Use the mouse to move the Draughts around", 10,100);
                          
     }

    public void mouseDragged(MouseEvent e) {
       mouse = e.getPoint();
       // continuously change the coordinates of the selected counter
       if (select == 1) counter1 = mouse;
       if (select == 2) counter2 = mouse;
       if (select == 3) counter3 = mouse;
       repaint();
       }

    public void mouseMoved(MouseEvent e) {}
    // required for the interface



    public void mousePressed(MouseEvent e) {
    //select a counter using the mouse
       mouse = e.getPoint();
       // this could be done a lot more neatly using an array
       if (mouse.x > counter1.x - 5 && mouse.x < counter1.x + 5 &&
              mouse.y > counter1.y - 5 && mouse.y < counter1.y + 5) select = 1;
       if (mouse.x > counter2.x - 5 && mouse.x < counter2.x + 5 &&
              mouse.y > counter2.y - 5 && mouse.y < counter2.y + 5) select = 2;
       if (mouse.x > counter3.x - 5 && mouse.x < counter3.x + 5 &&
              mouse.y > counter3.y - 5 && mouse.y < counter3.y + 5) select = 3;
       }


    // required for the interface
    public void mouseClicked(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}


    
  public MyDraughts()
    {
        
        setLayout(new GridLayout(8,8));

        for (int i=0; i<64; i++)
        {
            Panel square = new Panel();

            if ((i+i/8)%2 == 0)
                square.setBackground(Color.black);
            else
                square.setBackground(Color.white);

            add(square);
        }
       
        setVisible(true);
    }
}

Post follows:

For some reason my draught pieces aren't showing but my chessboard is, can anyone help out? :nuts:

thanks :heart:
 
Last edited by a moderator:
I can't help you with java, but can with your avatar, which is too small, here is a full sized version for you .....
BigFloppies.gif
 
maybe the chess board is drawing over the draught pieces. try commenting out the chess board drawing section of code and see what happens.
 
At least he got some replies, in the Programming forum I waited for days until I got a single reply.
 
I would start adding in stuff to figure out how it is flowing.

Such as, in each function println something to the console or the error log to tell you what function it's in, maybe displaying the variables you are checking to redraw the piece.

You could also set it to draw a piece automatically, just to see if the repaint scheme is working right (I always had trouble with those). It's difficult too look at isolated code like this and find out anything useful without understanding the context of its usage/ect. If you add little debug lines to monitor the progress, or extra display code to make sure the redraw system works, you may be able to pinpoint and fix the problem yourself.
 
Haven't looked at your code but two things:

1: Are you drawing the pieces after the background?
2: Are you instructing the screen to update the area which you want redrawn (under the piece). EG in Win32 it's InvalidateRect(), or Invalidate().
 
Looks like you are creating lots of separate panels for each square on the board, then you don't specify a panel to draw the pieces when using the paint method. If you get rid of the MyDraughts constructor it would draw the pieces. So either draw the squares in paint, or put a piece on each of the panels :hmm:
 
Back
Top