Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java

  • 22-08-2006 07:20PM
    #1
    Closed Accounts Posts: 74 ✭✭


    Im having some trouble running this code, it doesn't seem to want to work for me unless I minimize it. any help appreaciated.

    // Description:

    //
    //
    // Directory: c:\myjava\teaching
    // Date: 5/4/00
    // Uses:
    //
    //
    // Comments: note the convenient use of the Point object for
    // manipulating coordinates


    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class MyDraughts1 extends Applet implements MouseListener,
    MouseMotionListener {
    private int top=0, left1=0, BWidth=200, Width,
    Height,N=4;

    private Point counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, 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);
    counter4 = new Point(80,60);
    counter5 = new Point(100,60);
    counter6 = new Point(120,60);
    counter7 = new Point(140,60);
    counter8 = new Point(160,60);
    mouse = new Point();
    repaint();
    }

    public void paint (Graphics g) {
    Draw_Board(g);
    g.setColor (Color.black);
    g.fillOval(counter1.x, counter1.y, 20,20);
    g.fillOval(counter2.x, counter2.y, 20,20);
    g.fillOval(counter3.x, counter3.y, 20,20);
    g.fillOval(counter4.x, counter4.y, 20,20);
    g.setColor (Color.white);
    g.fillOval(counter5.x, counter5.y, 20,20);
    g.fillOval(counter6.x, counter6.y, 20,20);
    g.fillOval(counter7.x, counter7.y, 20,20);
    g.fillOval(counter8.x, counter8.y, 20,20);
    }

    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;
    if (select == 4) counter4 = mouse;
    if (select == 5) counter5 = mouse;
    if (select == 6) counter6 = mouse;
    if (select == 7) counter7 = mouse;
    if (select == 8) counter8 = 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 - 20 && mouse.x < counter1.x + 20 &&
    mouse.y > counter1.y - 20 && mouse.y < counter1.y + 20) select = 1;
    if (mouse.x > counter2.x - 20 && mouse.x < counter2.x + 20 &&
    mouse.y > counter2.y - 20 && mouse.y < counter2.y + 20) select = 2;
    if (mouse.x > counter3.x - 20 && mouse.x < counter3.x + 20 &&
    mouse.y > counter3.y - 20 && mouse.y < counter3.y + 20) select = 3;
    if (mouse.x > counter4.x - 20 && mouse.x < counter4.x + 20 &&
    mouse.y > counter4.y - 20 && mouse.y < counter4.y + 20) select = 4;
    if (mouse.x > counter5.x - 20 && mouse.x < counter5.x + 20 &&
    mouse.y > counter5.y - 20 && mouse.y < counter5.y + 20) select = 5;
    if (mouse.x > counter6.x - 20 && mouse.x < counter6.x + 20 &&
    mouse.y > counter6.y - 20 && mouse.y < counter6.y + 20) select = 6;
    if (mouse.x > counter7.x - 20 && mouse.x < counter7.x + 20 &&
    mouse.y > counter7.y - 20 && mouse.y < counter7.y + 20) select = 7;
    if (mouse.x > counter8.x - 20 && mouse.x < counter8.x + 20 &&
    mouse.y > counter8.y - 20 && mouse.y < counter8.y + 20) select = 8;
    repaint();

    }

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


    private void Draw_Board(Graphics g) {
    int SqWidth = BWidth/N; // Rounds down

    for(int Row=0; Row<4; Row++)
    for(int Col=0; Col<4; Col++) {
    g.setColor(Pos_Color(Row, Col));
    g.fillRect(left1+Col*SqWidth, top+Row*SqWidth,
    SqWidth, SqWidth);
    }

    }
    private boolean Is_Even(int X) {
    return(X == 2*(X/2));
    }
    private Color Pos_Color(int Row, int Col) {
    if ( (Is_Even(Row) && Is_Even(Col)) ||
    (!Is_Even(Row) && !Is_Even(Col)))
    return(Color.GRAY);
    else
    return(Color.lightGray);

    }
    public void update (Graphics g) {

    }
    }


Comments

  • Closed Accounts Posts: 503 ✭✭✭OMcGovern


    1. Try adding super.paint(g) as the first line in your overloaded paint() method


    2. Not sure why you overloaded update() but done nothing in it...
    I suggest commenting that out completely.


    Ps. a more flexible design approach would be to create your own Draughts panel, which could then be added to a Frame or Applet.
    Ps. I tend to use Swing rather than the AWT.
    So I'd use JApplet, with a new class called DraughtsPanel which extends JPanel.

    regards,
    Owen


Advertisement