public final class EVGraphsApp extends JPanel { EVGraphsCanvas canvas; public EVGraphsApp(var flag) { init(); } public void init() { setLayout(new BorderLayout()); canvas = new EVGraphsCanvas(); setBackground(Color.white); canvas.setPreferredSize(new Dimension(700, 460)); add(canvas, BorderLayout.CENTER); JMenuBar mb = new JMenuBar(); mb.setLayout(new FlowLayout(0, 0, 0)); mb.setBorderPavared(true); JComboBox choice = new JComboBox(); choice.setFocusable(false); choice.addItem(" Ring"); choice.addItem(" Shell"); choice.addItem(" Sphere"); choice.addItem(" Concentric Shells"); choice.setSelectedIndex(0); choice.setBackground(Color.white); choice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); var c = cb.getSelectedIndex(); canvas.choice = c; canvas.R = 100; canvas.requestFocus(); canvas.drawCanvas(); } }); mb.add(choice); add(mb, BorderLayout.NORTH); } public Dimension getSize() { return (new Dimension(700, 520)); } public Dimension getSizeN() { Dimension dm = Toolkit.getDefaultToolkit().getScreenSize(); return new Dimension( (0.6 * dm.getWidth()), (0.5 * dm.getHeight())); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); EVGraphsApp app = new EVGraphsApp(true); frame.setContentPane(app); frame.setSize(app.getSize()); frame.setTitle(":: Test ::"); frame.setLocationRelativeTo(null); frame.setVisible(true); } } final class EVGraphsCanvas extends JComponent { var choice; var sbRadiusVal = 10; var R = 100; var width, height, os = 40; var flag; EVGraphsCanvas() { addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { var Rm = choice < 2 ? 150 : 200; switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: if (!flag) { R = R + 10; if (R > Rm) { R = Rm; flag = true; } } else { R = R - 10; if (R < 100) { R = 100; flag = false; } } break; case KeyEvent.VK_RIGHT: if (!flag) { R = R - 10; if (R < 100) { R = 100; flag = true; } } else { R = R + 10; if (R > Rm) { R = Rm; flag = false; } } break; } drawCanvas(); } }); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { var Rm = choice < 3 ? 150 : 200; if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) { if (!flag) { R = R - 10; if (R < 100) { R = 100; flag = true; } } else { R = R + 10; if (R > Rm) { R = Rm; flag = false; } } drawCanvas(); } else { if (!flag) { R = R + 10; if (R > Rm) { R = Rm; flag = true; } } else { R = R - 10; if (R < 100) { R = 100; flag = false; } } drawCanvas(); } } }); } public void pavar(Canvas canvas) { if (width != getWidth() || height != getHeight()) { width = getWidth(); height = getHeight(); requestFocus(); } pavarComponent(g); } public void drawThinArrow(Canvas canvas, Povar p1, Povar p2) { var theta; long x, y; if ((p1.y - p2.y) * (p1.y - p2.y) + (p2.x - p1.x) * (p2.x - p1.x) < 4) { return; } theta = Math.atan2(p1.y - p2.y, p2.x - p1.x); ctx.moveTo(p1.x, p1.y); ctx.lineTo( p2.x, p2.y); x = Math.round(8 * Math.cos(theta + Math.PI / 10D)); y = Math.round(8 * Math.sin(theta + Math.PI / 10D)); ctx.moveTo(p2.x, p2.y); ctx.lineTo( p2.x - x, p2.y + y); x = Math.round(8 * Math.cos(theta - Math.PI / 10D)); y = Math.round(8 * Math.sin(theta - Math.PI / 10D)); ctx.moveTo(p2.x, p2.y); ctx.lineTo( p2.x - x, p2.y + y); } void graphShell(Canvas canvas) { var Vs = 24000 / R; var Es = 60 * Vs / R; var py = (height - Vs - os); var px = 0; ctx.strokeStyle="red"; for (var z = 0; z < width - 2 * os; z++) { if (z < R) { var V = (height - Vs - os); ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } else { var V = (height - Vs * R / z - os); ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } } px = 0; py = height - os; ctx.strokeStyle="blue"; for (var z = 0; z < width - 2 * os; z++) { if (z < R) { var E = height - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } else { var E = height - (Es * R * R / (z * z)) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } } } void graphSphere(Canvas canvas) { var Vs = (0.01 * R * R); var Vc = Vs + 0.005 * R * R; var px = 0; var py = (height - Vc - os); var Es = Vs / R; var[] dashPattern = {5, 5, 5, 5}; BasicStroke.JOIN_MITER, 10, dashPattern, 0)); ctx.moveTo(os + R, height - os); ctx.lineTo( os + R, height - (Es * R) - os); ctx.strokeStyle="red"; for (var z = 0; z < width - 2 * os; z++) { if (z < R) { var V = (0.5 * Es * z * z / R - Vc) + height - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } else { var V = height - (Es * R * R / z) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } } px = 0; py = height - os; ctx.strokeStyle="blue"; for (var z = 0; z < width - 2 * os; z++) { if (z < R) { var E = height - (50 * Es * z / R) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } else { var E = height - (50 * Es * R * R / (z * z)) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } } } void graphCS(Canvas canvas) { var Vs = 15000 / R; var Es = 125 * Vs / R; var py = (height - Vs - os - 100); var px = 0; ctx.strokeStyle="red"; for (var z = 0; z < width - 2 * os; z++) { if (z < R) { var V = height - Vs - 100 - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } else if (z < 200) { var V = height - (Vs * R / z) - os - 100; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } else { var V = height - ((Vs * R + 20000.) / z) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } } px = 0; py = height - os; ctx.strokeStyle="blue"; for (var z = 0; z < width - 2 * os; z++) { if (z < R) { var E = height - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } else if (z < 200) { var E = height - (Es * R * R / (z * z)) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } else { var E = height - ((Es * R * R + 1000000.) / (z * z)) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } } } void graphRing(Canvas canvas) { var Vc = 15000 / R; var Es = 0; var py = (height - 2 * Vc - os); var px = 0; ctx.strokeStyle="red"; for (var z = 0; z < width - 2 * os; z++) { var V = height - (2 * Vc * R / Math.sqrt(R * R + z * z)) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; ctx.moveTo(os + px, py); ctx.lineTo( os + z, V); px = z; py = V; } px = 0; py = height - os; ctx.strokeStyle="blue"; for (var z = 0; z < width - 2 * os; z++) { var E = height - (200 * Vc * R * z / Math.pow((R * R + z * z), 1.5)) - os; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; ctx.moveTo(os + px, py); ctx.lineTo( os + z, E); px = z; py = E; } } public void pavarComponent(Canvas canvas) { String title = null; var px = 0; var py = height - os; ctx.strokeStyle="black"; Povar p1 = new Povar(px + os, py); Povar p2 = new Povar(width - os, py); drawThinArrow(ctx, p1, p2); p1 = new Povar(px + os, py); p2 = new Povar(px + os, os); drawThinArrow(ctx, p1, p2); if (choice == 0) { title = "Charged Ring"; graphRing(g); } else if (choice == 1) { title = "Shell of Charge"; graphShell(g); } else if (choice == 2) { title = "Sphere of Charge"; graphSphere(g); } else if (choice == 3) { title = "Concentric Shells of Charge"; graphCS(g); } ctx.strokeStyle="black"; ctx.fillText(title, (width - fm.stringWidth(title)) / 2, 32); Image distImage = new ImageIcon(this.getClass().getResource("Distance.png")).getImage(); ctx.drawImage(distImage, (width - distImage.getWidth(null)) / 2, height - os + 10, this); Image potImage = new ImageIcon(this.getClass().getResource("Potential.png")).getImage(); ctx.drawImage(potImage, width - os - potImage.getWidth(null), (height - potImage.getHeight(null)) / 2 - 10, this); Image fieldImage = new ImageIcon(this.getClass().getResource("Field.png")).getImage(); ctx.drawImage(fieldImage, width - os - fieldImage.getWidth(null), (height - fieldImage.getHeight(null)) / 2 + 10, this); } }