How to repaint Ellipse2D.Double object

Hi guys,

I have got a silly probleme, that I had never had befor. I trying to develop a game DOTS. a two player game.Each player clicks on a dot in turn. The first player makes a dot red, the second player makes a dot green. and so on.... because I have stopped at this point:). I cant repeint an Ellipse object. I give you my test code. The mouse regognize color, and position of Ellipse2D object, but can't refresh image.

Please help as soon as possible. I'll appreciate any notes.

Take care.

My code is:

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.event.*;

public class dot extends Ellipse2D.Double{

int x;

int y;

boolean filled;

final static int pr=45;

final static BasicStroke stroke = new BasicStroke(3.0f);

Graphics2D g2;

Color color;

public dot(Graphics2D g,int x,int y, Color t){

super();

g2=g;

filled=false;

this.x=x-pr;

this.y=y-pr;

color=t;

this.setFrame(this.x,this.y,pr*2,pr*2);

g2.setPaint(color);

g2.fill(this);

}

public void changeColor(Color cl){

color=cl;

this.setFrame(x,y,pr*2,pr*2);

g2.setPaint(color);

g2.fill(this);

}

}

the next class is :

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Field extends JPanel implements MouseListener {

Color color;

Graphics2D g2;

dot point;

public Field(){

super();

setSize(500,500);

setBackground(new Color(0,125,0));

addMouseListener(this);

};

public void paintComponent(Graphics g){

super.paintComponent(g);

RenderingHints rend=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

g2=(Graphics2D)g;

g2.addRenderingHints(rend);

int j=10;int i=10;

point =new dot(g2,262,262,Color.gray);

}

public void mouseClicked(MouseEvent e){

int x=e.getX();

int y=e.getY();

if(point.getBounds().contains(x,y)){

point.changeColor(Color.blue);//

point.setFrame(x,y,15,15);

g2.fill(point);//

System.out.println("true");

System.out.println("dots color:"+point.color);

}else{

System.out.println("false");

point.changeColor(Color.gray);

System.out.println("dot color:"+point.color);

}

System.out.println("x="+x+""+"y="+y);

System.out.println("dot is at :"+point.x+""+point.y);

repaint();

}

public void mousePressed(MouseEvent e ){}

public void mouseReleased(MouseEvent e ){}

public void mouseEntered(MouseEvent e ){}

public void mouseExited(MouseEvent e ){}

};

and the main class is here:

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.event.*;

public class proba extends JFrame{

public proba(){

super("Game");

resize(500,540);

Dimension dim=getToolkit().getScreenSize();

this.setLocation((int)(dim.getWidth()-this.getWidth())/2,(int)(dim.getHeight()-this.getHeight())/2);

setResizable(false);

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(new Field(),BorderLayout.CENTER);

this.show(true);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.runFinalization();

System.exit(0);

System.gc();

}

});

};

public static void main(String[] args) {

new proba();

};

};

Sorry guys for this long post, but I want to make things clear

[3749 byte] By [MSashaa] at [2007-9-30 0:38:49]
# 1

It's helpful to format code using the code tags. That said, it looks like in Field's

paintComponent method you create a new dot each time the component gets

repainted. Hence, changing the color later on and then calling repaint causes

a new dot with the old color to be painted.

Also, standard java convention has class names beginning with a capital lettter,

e.g. Dot rather than dot.

: jay

JayDSa at 2007-7-16 5:10:18 > top of Java-index,Archived Forums,Swing...