import javax.swing.*; import java.awt.*; // for Graphics public class CartesianPlane extends JApplet { private int x; private int y; public void init() { String str; // Get two input values str = JOptionPane.showInputDialog(null, "Enter x value (-250~250)"); //If wrong type of data entered (non-integer) or click Cancel, error occurs x = Integer.parseInt(str); //Class Integer in java.lang str = JOptionPane.showInputDialog(null, "Enter y value(-250~250)"); y = Integer.parseInt(str); } public void paint( Graphics g ) { //JFrame plane; //Container contentPane; //Graphics g; int width =500; int height =500; /** x-axis tick marks. */ int tickX; /** y-axis tick marks. */ int tickY; /** Maximum x-axis value. */ // private int maxX; /** Maximum y-axis value. */ // private int maxY; /* plane = new JFrame(); plane.setTitle("Cartesian Plane"); plane.setSize(600,600); plane.setLocation(100,100); plane.setVisible(true); */ // int x=50, y=-100; String str; /* contentPane = plane.getContentPane(); contentPane.setBackground(Color.white); */ //g= contentPane.getGraphics(); Graphics2D g2D = (Graphics2D) g; int maxX, maxY; maxX = width/2; maxY = height/2; int divX =10, divY=10; tickX = width/divX; tickY = height/divY; g2D.drawString("x 250",2*maxX-10,maxY-5); g2D.drawString("250 y",maxX+5,+10); g2D.translate(maxX,maxY); g2D.scale(1.0,-1.0); // abstract void scale(double sx, double sy) // Concatenates the current Graphics2D Transform with a scaling transformation Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling. g2D.drawLine(-maxX,0,maxX,0); g2D.drawLine(0,maxY,0,-maxY); // mark the x-axis int count = -maxX; while (count < maxX) { g2D.drawLine(count,-5,count,5); g2D.drawLine(-count,-5,-count,5); count = count + tickX; } // mark the y-axis count = -maxY; while (count < maxY) { g2D.drawLine(-5,count, 5,count); g2D.drawLine(- 5,-count, 5,-count); count = count + tickY; } g2D.setColor(Color.RED ); g2D.drawLine(0,0,x,y); g2D.scale(1.0,-1.0); //must negate y value. scale concatenates, doesn't set g2D.drawString("(" + x + "," + y + ")", x, -y); //**note -y g2D.scale(1.0,-1.0); g2D.setColor(Color.BLUE ); g2D.drawLine(0,0,x,-y); g2D.scale(1.0,-1.0); g2D.drawString("(" + x + "," + -y + ")", x, y); //**y==-Y g2D.scale(1.0,-1.0); g2D.setColor(Color.GREEN ); g2D.drawLine(0,0,-x,y); g2D.scale(1.0,-1.0); g2D.drawString("(" + -x + "," + y + ")", -x, -y);//flipped y g2D.scale(1.0,-1.0); g2D.setColor(Color.CYAN ); g2D.drawLine(0,0,-x,-y); g2D.scale(1.0,-1.0); g2D.drawString("(" + -x + "," + -y + ")", -x, y);//**flipped y g2D.scale(1.0,-1.0); //g2D.drawString("(" + x + "," + y + ")", x, y); // Graphics g2= contentPane.getGraphics(); // g2.drawString("(" + x+ "," + y + ")", x+250, -(y-250)); //plane.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }