Jump to content

Java graphics2D


Recommended Posts

Hi!

I'm working on checkers using java as my language of choice. For this I use the java graphics2d class.

My question is simple:

 

Does the coordinate system work with points of the form (x,0), (0,y)? In other words, are the coordinate axes part of the coordinate system?

 

I've already through trial and error found out that there are no negative axes. A piece of information that the documentation completely lacked.

 

For extra value, here is my code for creating the (chess)board.

 

import java.awt.*;
public class Board {
int x, y;
Graphics g;
String[] alphabet={"A","B","C","D","E","F","G","H"};
String[] numbers={"1","2","3","4","5","6","7","8"};
public Board(Graphics g1){
	g=g1;

}
public void paint(Graphics g, int width, int height){

	x=width/9;
      y=height/9;
      for ( int i = 0; i < 8; ++i ) { // row
    	  changeColor(g);
        for(int j=0;j<8;j++){ // columns in a row
        	changeColor(g);
        g.fillRect(x+j*x, x+i*x, x, y);	

        }
   }

      g.setColor(Color.darkGray);
    	  Font font = new Font("Serif", Font.BOLD, x);
    	  g.setFont(font);
    	  for (int i=0; i<8; i++)
    	  g.drawString(alphabet[i], x+x/7+i*x, 86*y/100 );
    	  for(int i=0; i<8; i++){
    		  g.drawString(numbers[i], x/5, 112*y/60+i*y);
    	  }



}
private void changeColor(Graphics g) {
	if(g.getColor().equals(Color.blue)){
		g.setColor(Color.red);
	}else{g.setColor(Color.blue);}

}
public Point getCorner(Point mouseClick) {
	Point corner=new Point(0,0);
	for(int i=0;i<9;i++){
		if(mouseClick.y>i*y){corner.y=i*y;}
		for(int j=0;j<9;j++){
			if (mouseClick.x>j*x){corner.x=j*x;}

		}

	}
	return corner;

}
}



 

 

 

Thanks for your help and other comments.

Link to comment
Share on other sites

Hi!

I'm working on checkers using java as my language of choice. For this I use the java graphics2d class.

My question is simple:

 

Does the coordinate system work with points of the form (x,0), (0,y)? In other words, are the coordinate axes part of the coordinate system?

 

I've already through trial and error found out that there are no negative axes. A piece of information that the documentation completely lacked.

 

I'll try to answer your question as best I can (I'm having a hard time understanding what you're asking, but maybe I can help) -

 

With a standard computer screen, the graphics plane does not translate directly to the traditional Cartesian coordinate system you learned in math (y axis is flipped). The point (0,0) typically corresponds to the top-left corner on your screen.

 

For a display that is 1920x1080, the point (1920,1080) typically corresponds to the bottom-right corner of the screen.

The form for these points are generally understood to be (X,Y).

 

 

 

When programming in Java, you might stumble upon the AffineTransform. If you're using an AffineTransform, visible screen coordinates can be negative.

 

 

Hope this helps.

99 dungeoneering achieved, thanks to everyone that celebrated with me!

 

♪♪ Don't interrupt me as I struggle to complete this thought
Have some respect for someone more forgetful than yourself ♪♪

♪♪ And I'm not done
And I won't be till my head falls off ♪♪

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.