source code of a sample 2d co-ordinate system using java

// viewport.java = the 2d coordinate system
/*
method viewport (int ,int)= create a viewport
*/

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class viewport extends JPanel
{
private int x,y;
public viewport(int maxX,int maxY)
{
this.x=maxX;
this.y=maxY;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(0,y/2,x,y/2); // for horizontal
g.drawLine(x/2,0,x/2,y); //for vertical

}

public static void main(String []args)
{
JFrame frame=new JFrame(“2d Co-ordinate system”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
viewport vw=new viewport(480,480);
frame.add(vw);
frame.setSize(480,480);
frame.setVisible(true);

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s