/** Overrides <code>Graphics.drawArc</code>. */ public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) { DebugGraphicsInfo info = info(); if (debugLog()) { info() .log( toShortString() + " Drawing arc: " + new Rectangle(x, y, width, height) + " startAngle: " + startAngle + " arcAngle: " + arcAngle); } if (isDrawingBuffer()) { if (debugBuffered()) { Graphics debugGraphics = debugGraphics(); debugGraphics.drawArc(x, y, width, height, startAngle, arcAngle); debugGraphics.dispose(); } } else if (debugFlash()) { Color oldColor = getColor(); int i, count = (info.flashCount * 2) - 1; for (i = 0; i < count; i++) { graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); graphics.drawArc(x, y, width, height, startAngle, arcAngle); Toolkit.getDefaultToolkit().sync(); sleep(info.flashTime); } graphics.setColor(oldColor); } graphics.drawArc(x, y, width, height, startAngle, arcAngle); }
private void drawDarkerSide( Graphics graphics, Color[] br, int top, int left, int bottom, int right) { graphics.setLineWidth(1); graphics.setAlpha(200); Color color; for (int i = 0; i < br.length; i++) { color = br[i]; graphics.setForegroundColor(color); graphics.drawArc( right - i - corner.width, bottom - i - corner.height, corner.width, corner.height, 270, 90); Point rightButtomRight = new Point(right - i, bottom - i - corner.height / 2); Point rightTopRight = new Point(right - i, top + i + corner.height / 2); graphics.drawLine(rightButtomRight, rightTopRight); graphics.drawArc(right - i - corner.width, top - i, corner.width, corner.height, 0, 45); Point rightButtomButtom = new Point(right - i - corner.width / 2, bottom - i); Point leftBottomLeft = new Point(left + i + corner.width / 2, bottom - i); graphics.drawLine(rightButtomButtom, leftBottomLeft); graphics.drawArc( left - i, bottom - i - corner.height, corner.width, corner.height, 180 + 45, 45); } }
private void draw3DCircle(Graphics g, int x, int y, int radius, boolean raised) { Color foreground = getForeground(); Color light = foreground.brighter(); Color dark = foreground.darker(); g.setColor(foreground); g.fillOval(x, y, radius * 2, radius * 2); g.setColor(raised ? light : dark); g.drawArc(x, y, radius * 2, radius * 2, 45, 180); g.setColor(raised ? dark : light); g.drawArc(x, y, radius * 2, radius * 2, 225, 180); }
// färger, se sid 43 i boken // grafiska metoder, sid 248 i boken public void paintComponent(Graphics g) { // för att vara säker på att // "super"-klassen gör sitt // anropar vi den metoden, innan // vi skriver eller ritar super.paintComponent(g); g.drawLine(185, 10, 195, 40); // x1,y1 till x2,y2 g.drawLine(200, 10, 200, 40); g.drawLine(215, 10, 205, 40); g.setColor(Color.white); g.fillOval(50, 30, 300, 150); // x,y,b,h (x,y för ö v h) g.setColor(Color.red); g.drawArc(100, 100, 200, 50, 180, 180); // x,y,b,h,s,l g.setColor(Color.yellow); g.fillRect(200, 100, 30, 30); g.fill3DRect(150, 50, 30, 50, true); // true upphöjd figur g.fill3DRect(250, 50, 30, 50, true); // skriv ut en textsträng, samt ange läget i avståndet från // övre vänstra hörnet i x-led åt höger och i y-led neråt g.drawString("** Tjenare kompis !! **", 20, 20); f = new Font("Arial", Font.BOLD, 30); setBackground(Color.cyan); g.setFont(f); g.setColor(new Color(255, 175, 175)); g.drawString("YEEEEEEEES!!", 100, 250); }
protected void paintRoundedBottomTabBorder( int tabIndex, Graphics g, int x1, int y1, int x2, int y2, boolean isSelected) { Graphics2D g2D = (Graphics2D) g; Object savedRederingHint = g2D.getRenderingHint(RenderingHints.KEY_ANTIALIASING); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int d = 2 * GAP; if (isSelected) { g.setColor(AbstractLookAndFeel.getTheme().getFrameColor()); } else { g.setColor(ColorHelper.brighter(AbstractLookAndFeel.getTheme().getFrameColor(), 40)); } g.drawLine(x1 + GAP, y2, x2 - GAP, y2); g.drawArc(x1, y2 - d, d, d, 180, 90); g.drawArc(x2 - d, y2 - d, d, d, -90, 90); g.drawLine(x1, y1, x1, y2 - GAP - 1); g.drawLine(x2, y1, x2, y2 - GAP - 1); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedRederingHint); }
public void paint(Graphics g) { g.drawString("Hello", 40, 40); setBackground(Color.white); g.fillRect(130, 30, 100, 80); g.drawOval(30, 130, 50, 60); setForeground(Color.red); g.fillOval(130, 130, 50, 60); g.drawArc(30, 200, 40, 50, 90, 60); g.fillArc(50, 200, 40, 20, 180, 40); }
/** * Metoda wyswietlajaca okreg obrazujacy postep wykrywania urzadzen * * @param g Referencja do obiektu klasy Graphics, ktory pozwala na narysowanie pasku postepu * @param progress Liczba z zakresu 0 - 359: zakres luku */ public void showInquiryProgress(Graphics g, int progress) { int c = g.getColor(); g.setColor(255, 255, 255); g.drawArc(screenWidth - 25, screenHeight - 57, 10, 16, 0, progress); g.setColor(c); bluetoothLogoSmallSprite.setPosition(screenWidth - 24, screenHeight - 56); bluetoothLogoSmallSprite.paint(g); }
/** paints the Spinner */ public void paint(Graphics g) { int start_angle = 90; int done_angle = (int) (percentDone * 360); g.setColor(getBackground()); g.fillArc(3, 3, getSize().width - 8, getSize().height - 8, 0, 360); g.setColor(getForeground()); g.fillArc(3, 3, getSize().width - 8, getSize().height - 8, start_angle, done_angle); g.setColor(Color.black); g.drawArc(3, 3, getSize().width - 8, getSize().height - 8, 0, 360); }
/** * Draws and arc which looks like a curve.<br> * An ARC is a "piece" of an OVAL.<br> * The first 5 parameters (g and 4 ints) are the same as drawOval.<br> * There are 2 additional parameters for the starting degree value and finishing degree of the * arc. <br> * 0 degrees is at the 12:00 position and the degrees progress in a CLOCKWISE fashion. <br> * (90 degrees is at 3:00, 180 degrees is at 6:00, 270 degrees is at 9:00, 360 degrees is back at * 12:00). */ public static void drawArc( Graphics g, int centerX, int centerY, int hRadius, int vRadius, int start, int finish) { int hDiameter = 2 * hRadius; int vDiameter = 2 * vRadius; if (finish < start) finish += 360; int newStart = 90 - start; // shifts starting position from 3:00 to 12:00 int newFinish = start - finish; // as opposed to finish-start. Subtracting backwards changes from // counter-clockwise to clockwise. g.drawArc(centerX - hRadius, centerY - vRadius, hDiameter, vDiameter, newStart, newFinish); }
private void drawLighterSide( Graphics graphics, Color[] tl, int top, int left, int bottom, int right) { graphics.setAlpha(255); right--; bottom--; Color color; graphics.setLineWidth(1); for (int i = 0; i < tl.length; i++) { color = tl[i]; graphics.setForegroundColor(color); graphics.drawArc(left - i, top - i, corner.width, corner.height, 90, 90); Point leftTopLeft = new Point(left - i, top - i + corner.height / 2); Point leftBottomLeft = new Point(left - i, bottom + i - corner.height / 2); graphics.drawLine(leftTopLeft, leftBottomLeft); graphics.drawArc(left - i, bottom - i - corner.height, corner.width, corner.height, 180, 45); Point rightTopTop = new Point(right - i - corner.width / 2, top - i); Point leftTopTop = new Point(left + i + corner.width / 2, top - i); graphics.drawLine(rightTopTop, leftTopTop); graphics.drawArc(right - i - corner.width, top - i, corner.width, corner.height, 0 + 45, 45); } }
@Override public void paint(Graphics g) { super.paint(g); g.drawLine(150, 75, 150, 375); g.drawLine(120, 375, 400, 375); g.drawLine(150, 75, 250, 75); g.drawLine(250, 75, 250, 125); g.setColor(Color.blue); g.drawLine(250, 175, 250, 250); g.drawArc(225, 125, 50, 50, 0, 360); g.drawLine(250, 200, 225, 175); g.drawLine(250, 200, 275, 175); g.drawLine(250, 250, 225, 275); g.drawLine(250, 250, 275, 275); g.drawString("You have won the game", 300, 225); }
public void paint(Graphics g) { int x1 = 30; int y1 = 30; int x2 = 60; int y2 = 50; g.drawRect(x1, y1, x2, y2); x1 = 80; y1 = 80; x2 = 100; y2 = 100; g.drawOval(x1, y1, x2, y2); x1 = 200; y1 = 60; x2 = 60; y2 = 60; g.drawArc(x1, y1, x2, y2, 0, 180); }
/** * @param g * @param col * @param row * @param iDir */ void DrawBoundary(Graphics g, int col, int row, int iDir) { int x, y; x = col * 16; y = row * 16; switch (iDir) { case ctables.LEFT: // draw lower half segment if (iMaze[row + 1][col] != WALL) // down empty if (iMaze[row + 1][col - 1] != WALL) // left-down empty { // arc(x-8,y+8,270,0,6); g.drawArc(x - 8 - 6, y + 8 - 6, 12, 12, 270, 100); } else { g.drawLine(x - 2, y + 8, x - 2, y + 16); } else { g.drawLine(x - 2, y + 8, x - 2, y + 17); g.drawLine(x - 2, y + 17, x + 7, y + 17); } // Draw upper half segment if (iMaze[row - 1][col] != WALL) // upper empty if (iMaze[row - 1][col - 1] != WALL) // upper-left empty { // arc(x-8,y+7,0,90,6); g.drawArc(x - 8 - 6, y + 7 - 6, 12, 12, 0, 100); } else { g.drawLine(x - 2, y, x - 2, y + 7); } else { g.drawLine(x - 2, y - 2, x - 2, y + 7); g.drawLine(x - 2, y - 2, x + 7, y - 2); } break; case ctables.RIGHT: // draw lower half segment if (iMaze[row + 1][col] != WALL) // down empty if (iMaze[row + 1][col + 1] != WALL) // down-right empty { // arc(x+16+7,y+8,180,270,6); g.drawArc(x + 16 + 7 - 6, y + 8 - 6, 12, 12, 180, 100); } else { g.drawLine(x + 17, y + 8, x + 17, y + 15); } else { g.drawLine(x + 8, y + 17, x + 17, y + 17); g.drawLine(x + 17, y + 8, x + 17, y + 17); } // Draw upper half segment if (iMaze[row - 1][col] != WALL) // upper empty if (iMaze[row - 1][col + 1] != WALL) // upper-right empty { // arc(x+16+7,y+7,90,180,6); g.drawArc(x + 16 + 7 - 6, y + 7 - 6, 12, 12, 90, 100); } else { g.drawLine(x + 17, y, x + 17, y + 7); } else { g.drawLine(x + 8, y - 2, x + 17, y - 2); g.drawLine(x + 17, y - 2, x + 17, y + 7); } break; case ctables.UP: // draw left half segment if (iMaze[row][col - 1] != WALL) // left empty if (iMaze[row - 1][col - 1] != WALL) // left-upper empty { // arc(x+7,y-8,180,270,6); g.drawArc(x + 7 - 6, y - 8 - 6, 12, 12, 180, 100); } else { g.drawLine(x, y - 2, x + 7, y - 2); } // Draw right half segment if (iMaze[row][col + 1] != WALL) // right empty if (iMaze[row - 1][col + 1] != WALL) // right-upper empty { // arc(x+8,y-8,270,0,6); g.drawArc(x + 8 - 6, y - 8 - 6, 12, 12, 270, 100); } else { g.drawLine(x + 8, y - 2, x + 16, y - 2); } break; case ctables.DOWN: // draw left half segment if (iMaze[row][col - 1] != WALL) // left empty if (iMaze[row + 1][col - 1] != WALL) // left-down empty { // arc(x+7,y+16+7,90,180,6); g.drawArc(x + 7 - 6, y + 16 + 7 - 6, 12, 12, 90, 100); } else { g.drawLine(x, y + 17, x + 7, y + 17); } // Draw right half segment if (iMaze[row][col + 1] != WALL) // right empty if (iMaze[row + 1][col + 1] != WALL) // right-down empty { // arc(x+8,y+16+7,0,90,6); g.drawArc(x + 8 - 6, y + 16 + 7 - 6, 12, 12, 0, 100); } else { g.drawLine(x + 8, y + 17, x + 15, y + 17); } break; } }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); // Get the appropriate size for the figure int width = getWidth(); int height = getHeight(); switch (type) { case LINE: // Display two cross lines g.setColor(Color.BLACK); g.drawLine(10, 10, width - 10, height - 10); g.drawLine(width - 10, 10, 10, height - 10); break; case RECTANGLE: // Display a rectangle g.setColor(Color.blue); if (filled) g.fillRect( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); else g.drawRect( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); break; case ROUND_RECTANGLE: // Display a round-cornered rectangle g.setColor(Color.red); if (filled) g.fillRoundRect( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 20, 20); else g.drawRoundRect( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 20, 20); break; case OVAL: // Display an Oval g.setColor(Color.black); if (filled) g.fillOval( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); else g.drawOval( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height)); break; case ARC: // Display an arc g.setColor(Color.blue); if (filled) { g.fillArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 0, 30); g.fillArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 90, 30); g.fillArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 180, 30); g.fillArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 270, 30); } else { g.drawArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 0, 30); g.drawArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 90, 30); g.drawArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 180, 30); g.drawArc( (int) (0.1 * width), (int) (0.1 * height), (int) (0.8 * width), (int) (0.8 * height), 270, 30); } break; case POLYGON: g.setColor(Color.red); int radius = (int) (0.4 * Math.min(width, height)); int xCenter = (int) (0.4 * width); int yCenter = (int) (0.4 * height); Polygon p = new Polygon(); p.addPoint(xCenter + radius, yCenter); p.addPoint( (int) (xCenter + radius * Math.cos(2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(2 * Math.PI / 6))); p.addPoint( (int) (xCenter + radius * Math.cos(2 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(2 * 2 * Math.PI / 6))); p.addPoint( (int) (xCenter + radius * Math.cos(3 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(3 * 2 * Math.PI / 6))); p.addPoint( (int) (xCenter + radius * Math.cos(4 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(4 * 2 * Math.PI / 6))); p.addPoint( (int) (xCenter + radius * Math.cos(5 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(5 * 2 * Math.PI / 6))); if (filled) { g.fillPolygon(p); } else { g.drawPolygon(p); } break; default: g.setColor(Color.BLACK); g.drawLine(10, 10, width - 10, height - 10); g.drawLine(width - 10, 10, 10, height - 10); break; } }
// Private support methods private void drawAnalogClock(Graphics g) { Dimension d = getSize(); int centerX = d.width / 2; int centerY = d.height / 2; Calendar now = Calendar.getInstance(); // Draw the clock shape g.setFont(getFont()); g.setColor(Color.white); g.fillArc(0, 0, d.width, d.height, 0, 360); g.setColor(Color.black); g.drawArc(0, 0, d.width, d.height, 0, 360); // draw hour numbers g.setColor(getForeground()); for (int i = 1; i <= 12; i++) { double numberTheta = (double) i / 12 * 2 * Math.PI; double numberRadius = getEllipseRadius(centerX, centerY, numberTheta); int numberX = (int) (centerX + 0.9 * numberRadius * Math.sin(numberTheta) - g.getFontMetrics().getStringBounds(String.valueOf(i), g).getCenterX()); int numberY = (int) (centerY - 0.9 * numberRadius * Math.cos(numberTheta) - g.getFontMetrics().getStringBounds(String.valueOf(i), g).getCenterY()); g.drawString(String.valueOf(i), numberX, numberY); } // draw hour markers for (int i = 1; i <= 60; i++) { double barTheta = (double) i / 60 * 2 * Math.PI; double numberRadius = getEllipseRadius(centerX, centerY, barTheta); int numberXFull = (int) (centerX + numberRadius * Math.sin(barTheta)); int numberYFull = (int) (centerY - numberRadius * Math.cos(barTheta)); g.drawLine( (int) (centerX + 0.97 * numberRadius * Math.sin(barTheta)), (int) (centerY - 0.97 * numberRadius * Math.cos(barTheta)), numberXFull, numberYFull); } // Draw the hour hand double hourTheta = ((double) now.get(Calendar.HOUR) + (double) now.get(Calendar.MINUTE) / 60 + (double) now.get(Calendar.SECOND) / 3600) / 12 * 2 * Math.PI; double hourRadius = getEllipseRadius(centerX, centerY, hourTheta); int hourX = (int) (centerX + 0.5 * hourRadius * Math.sin(hourTheta)); int hourY = (int) (centerY - 0.5 * hourRadius * Math.cos(hourTheta)); int hourXOrigin = (int) (centerX - 0.1 * hourRadius * Math.sin(hourTheta)); int hourYOrigin = (int) (centerY + 0.1 * hourRadius * Math.cos(hourTheta)); g.setColor(Color.BLACK); g.fillArc(hourX - 3, hourY - 3, 6, 6, 0, 360); g.drawLine(hourXOrigin, hourYOrigin, hourX, hourY); // Draw the minute hand double minuteTheta = ((double) now.get(Calendar.MINUTE) + (double) now.get(Calendar.SECOND) / 60) / 60 * 2 * Math.PI; double minuteRadius = getEllipseRadius(centerX, centerY, minuteTheta); int minuteX = (int) (centerX + 0.8 * minuteRadius * Math.sin(minuteTheta)); int minuteY = (int) (centerY - 0.8 * minuteRadius * Math.cos(minuteTheta)); int minuteXOrigin = (int) (centerX - 0.1 * minuteRadius * Math.sin(minuteTheta)); int minuteYOrigin = (int) (centerY + 0.1 * minuteRadius * Math.cos(minuteTheta)); g.fillArc(minuteX - 3, minuteY - 3, 6, 6, 0, 360); g.drawLine(minuteXOrigin, minuteYOrigin, minuteX, minuteY); // Draw the second hand double secondTheta = ((double) now.get(Calendar.SECOND)) / 60 * 2 * Math.PI; double secondRadius = getEllipseRadius(centerX, centerY, secondTheta); int secondX = (int) (centerX + 0.9 * secondRadius * Math.sin(secondTheta)); int secondY = (int) (centerY - 0.9 * secondRadius * Math.cos(secondTheta)); int secondXOrigin = (int) (centerX - 0.1 * secondRadius * Math.sin(secondTheta)); int secondYOrigin = (int) (centerY + 0.1 * secondRadius * Math.cos(secondTheta)); // redness of second hand getting darker and darker g.setColor( new Color( 255, 199 - (int) (199 * (double) now.get(Calendar.SECOND) / 60), 199 - (int) (199 * (double) now.get(Calendar.SECOND) / 60))); g.fillArc(secondX - 3, secondY - 3, 6, 6, 0, 360); g.drawLine(secondXOrigin, secondYOrigin, secondX, secondY); // draw the center g.setColor(Color.BLACK); g.fillArc(centerX - 3, centerY - 3, 6, 6, 0, 360); }
// ----------------------------------------------------------------- // Draws a snowman. // ----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent(page); final int MID = 170; final int TOP = 50; // constants for snow body final int head = 40; final int UP_T[] = {70, 50}; final int LOW_T[] = {100, 60}; // coordinates for shadow hat polygon final int hatX[] = {MID - 70, MID - 50, MID - 65, MID - 85}; final int hatY[] = {TOP + 168, TOP + 173, TOP + 193, TOP + 188}; // coordinates for sun rays final int ray1X[] = {250, 255, -100, -110}; final int ray1Y[] = {5, 15, 95, 45}; final int ray2X[] = {260, 280, 170, -130}; final int ray2Y[] = {30, 45, 360, 220}; final char name[] = {'E', '.', 'Z', '.'}; setBackground(Color.blue); page.setColor(Color.yellow); page.fillOval(260, -40, 80, 80); // sun page.fillPolygon(ray1X, ray1Y, 4); // upper sun ray page.fillPolygon(ray2X, ray2Y, 4); // lower sun ray // ground changed to white page.setColor(Color.white); page.fillRect(0, 175, 300, 50); // ground // shadow body added page.setColor(Color.lightGray); page.fillOval(MID - 60, TOP + 120, LOW_T[0], LOW_T[1] / 2); // lower torso shadow page.fillOval(MID - 65, TOP + 140, UP_T[0], UP_T[1] / 2); // upper torso shadow page.fillOval(MID - 70, TOP + 155, head, head / 2); // head shadow page.drawLine(MID - 75, TOP + 165, MID - 45, TOP + 180); // hat brim shadow page.drawLine(MID - 75, TOP + 166, MID - 45, TOP + 181); // thicker brim shadow page.fillPolygon(hatX, hatY, 4); // had shadow page.setColor(Color.white); page.fillOval(MID - 20, TOP, 40, 40); // head page.fillOval(MID - 35, TOP + 35, UP_T[0], UP_T[1]); // upper torso page.fillOval(MID - 50, TOP + 80, LOW_T[0], LOW_T[1]); // lower torso page.setColor(Color.red); page.fillOval(MID - 5, TOP + 45, 10, 10); // 1st button page.fillOval(MID - 5, TOP + 65, 10, 10); // 2nd button page.setColor(Color.black); page.fillOval(MID - 10, TOP + 10, 5, 5); // left eye page.fillOval(MID + 5, TOP + 10, 5, 5); // right eye page.drawArc(MID - 10, TOP + 25, 20, 10, 10, 160); // frown, was smile page.drawLine(MID - 25, TOP + 60, MID - 50, TOP + 40); // left arm page.drawLine(MID + 25, TOP + 60, MID + 55, TOP + 60); // right arm page.drawLine(MID - 20, TOP + 5, MID + 20, TOP + 5); // brim of hat page.drawLine(MID - 20, TOP + 4, MID + 20, TOP + 4); // thicker brim page.fillRect(MID - 15, TOP - 20, 30, 25); // top of hat page.setColor(Color.white); page.drawChars(name, 0, 4, 10, 20); // display name }
/** * _more_ * * @param graphics */ public void drawNext(Graphics graphics) { Rectangle r = getBounds(); graphics.drawArc(r.x, r.y, r.width, r.height, startAngle, endAngle); }
public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); // Draws a white arrow and the principal axis g.drawLine(0, 200, 700, 200); g.drawLine(arrow_x, 200, arrow_x, arrow_y2); // Show coordinates of arrow tip arrowCoordinate_x = arrow_x - startingPosition; arrowCoordinate_x /= 10; arrowCoordinate_y = 200 - arrow_y2; arrowCoordinate_y /= 10; // Coordinates Optics.lbl_arrowCoordinates.setText( "<html>(d<sub>o</sub>, h<sub>o</sub>) = (" + arrowCoordinate_x + ", " + arrowCoordinate_y + ")</html>"); if (arrow_y2 < 200) // if arrow is above principal axis { g.drawLine(arrow_x, arrow_y2, arrow_x - 7, arrow_y2 + 7); g.drawLine(arrow_x, arrow_y2, arrow_x + 7, arrow_y2 + 7); } else if (arrow_y2 > 200) // if arrow is below principal axis { g.drawLine(arrow_x, arrow_y2, arrow_x - 7, arrow_y2 - 7); g.drawLine(arrow_x, arrow_y2, arrow_x + 7, arrow_y2 - 7); } // Draws lines for the grid if (lenses) startingPosition = 350; else { radiusOfCurvature = 20 * focalLength; if (type == 0) startingPosition = 500; else startingPosition = 350; } { for (int i = startingPosition; i <= 700; i += 10) { if ((i - startingPosition) % (10 * focalLength) == 0) { g.setColor(Color.ORANGE); g.drawLine(i, 195, i, 205); } else { g.setColor(Color.WHITE); g.drawLine(i, 197, i, 203); } } for (int i = startingPosition; i >= 0; i -= 10) { if ((i - startingPosition) % (10 * focalLength) == 0 && i != 0) { g.setColor(Color.ORANGE); g.drawLine(i, 195, i, 205); } else { g.setColor(Color.WHITE); g.drawLine(i, 197, i, 203); } } } g.setColor(Color.WHITE); if (lenses) { if (type == 0) // If Converging { // Draws a converging lens g.drawArc(340, 50, 40, 300, 120, 120); g.drawArc(320, 50, 40, 300, 60, -120); // draws horizontal line from the tip of the arrow to the lens (line 1/3) g.setColor(Color.RED); g.drawLine(arrow_x, arrow_y2, 350, arrow_y2); // calculates necessary information to form equation of line from lens to focal point (line // 2/3) dy_1 = 200 - arrow_y2; if (arrow_x > 350) dx_1 = -10 * focalLength; else dx_1 = 10 * focalLength; slope_1 = dy_1 / dx_1; if (arrow_x > 350) y_intercept_1 = 200 - slope_1 * (350 - 10 * focalLength); else y_intercept_1 = 200 - slope_1 * (10 * focalLength + 350); // Calculates coordinates of points on the edge of screen (endpoints) if (arrow_x <= 350) y_screenIntersection_1 = (int) (Math.round(slope_1 * 700 + y_intercept_1)); else y_screenIntersection_1 = (int) (Math.round(y_intercept_1)); if (slope_1 != 0) if (arrow_y2 <= 200) x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1)); else x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1)); if (x_screenIntersection_1 >= 0 && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge if (arrow_y2 <= 200) g.drawLine(350, arrow_y2, x_screenIntersection_1, 400); else g.drawLine(350, arrow_y2, x_screenIntersection_1, 0); else if (arrow_x > 350) g.drawLine(350, arrow_y2, 0, y_screenIntersection_1); else g.drawLine(350, arrow_y2, 700, y_screenIntersection_1); // Else: endpoint is on the y-edge } else // Else: Diverging { // Draws a diverging lens g.drawArc(360, 50, 40, 300, 120, 120); g.drawArc(300, 50, 40, 300, 60, -120); g.drawLine(330, 68, 370, 68); g.drawLine(330, 330, 370, 330); // draws horizontal line from the tip of the arrow to the lens (line 1/3) g.setColor(Color.RED); g.drawLine(arrow_x, arrow_y2, 350, arrow_y2); // calculates necessary information to form equation of line from lens to focal point (line // 2/3) dy_1 = arrow_y2 - 200; if (arrow_x > 350) dx_1 = -10 * focalLength; else dx_1 = 10 * focalLength; slope_1 = dy_1 / dx_1; if (arrow_x > 350) y_intercept_1 = 200 - slope_1 * (10 * focalLength + 350); else y_intercept_1 = 200 - slope_1 * (350 - 10 * focalLength); // Calculates coordinates of points on the edge of screen (endpoints) if (arrow_x <= 350) y_screenIntersection_1 = (int) (Math.round(slope_1 * 700 + y_intercept_1)); else y_screenIntersection_1 = (int) (Math.round(y_intercept_1)); if (slope_1 != 0) if (arrow_y2 <= 200) x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1)); else x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1)); if (x_screenIntersection_1 >= 0 && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge if (arrow_y2 <= 200) g.drawLine(350, arrow_y2, x_screenIntersection_1, 0); else g.drawLine(350, arrow_y2, x_screenIntersection_1, 400); else // Else: endpoint is on the y-edge if (arrow_x > 350) g.drawLine(350, arrow_y2, 0, y_screenIntersection_1); else g.drawLine(350, arrow_y2, 700, y_screenIntersection_1); } // Line 3/3 dy_2 = 200 - arrow_y2; dx_2 = 350 - arrow_x; slope_2 = dy_2 / dx_2; y_intercept_2 = 200 - slope_2 * 350; if (arrow_x <= 350) y_screenIntersection_2 = (int) (Math.round(slope_2 * 700 + y_intercept_2)); else y_screenIntersection_2 = (int) (Math.round(y_intercept_2)); if (slope_2 != 0) if (arrow_y2 <= 200) x_screenIntersection_2 = (int) (Math.round((400 - y_intercept_2) / slope_2)); else x_screenIntersection_2 = (int) (Math.round(-y_intercept_2 / slope_2)); if (x_screenIntersection_2 >= 0 && x_screenIntersection_2 <= 700) // If endpoint is on the x-edge if (arrow_y2 <= 200) g.drawLine(arrow_x, arrow_y2, x_screenIntersection_2, 400); else g.drawLine(arrow_x, arrow_y2, x_screenIntersection_2, 0); else if (arrow_x <= 350) g.drawLine( arrow_x, arrow_y2, 700, y_screenIntersection_2); // Else: endpoint is on the y-edge else g.drawLine(arrow_x, arrow_y2, 0, y_screenIntersection_2); // POI between Line 2 & Line 3 x_pointOfIntersection = (int) ((y_intercept_2 - y_intercept_1) / (slope_1 - slope_2)); y_pointOfIntersection = (int) (slope_1 * x_pointOfIntersection + y_intercept_1); // Draw image g.setColor(Color.ORANGE); g.drawLine(x_pointOfIntersection, 200, x_pointOfIntersection, y_pointOfIntersection); if (y_pointOfIntersection < 200) { g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection - 7, y_pointOfIntersection + 7); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection + 7, y_pointOfIntersection + 7); } else { g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection - 7, y_pointOfIntersection - 7); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection + 7, y_pointOfIntersection - 7); } // Same side image line continuation if (((x_pointOfIntersection > 350 && arrow_x > 350) || (x_pointOfIntersection < 350 && arrow_x < 350)) && (arrow_x != 350 - 10 * focalLength && arrow_x != 350 + 10 * focalLength || type == 1)) { g.setColor(Color.YELLOW); g.drawLine(x_pointOfIntersection, y_pointOfIntersection, 350, arrow_y2); if (type == 0) g.drawLine(x_pointOfIntersection, y_pointOfIntersection, arrow_x, arrow_y2); } // Mag calculations height_image = 200 - y_pointOfIntersection; height_object = 200 - arrow_y2; if (height_object != 0) magnification = height_image / height_object; if (magnification <= 9999 && magnification >= -9999) Optics.txt_magnification.setText("" + roundTwoDecimals(magnification)); else if (magnification > 9999) { magnification = Double.POSITIVE_INFINITY; Optics.txt_magnification.setText("N/A"); } else { magnification = Double.NEGATIVE_INFINITY; Optics.txt_magnification.setText("N/A"); } // Characteristics g.setColor(Color.ORANGE); g.drawString("Image Characteristics:", 20, 300); if (type == 0) { if ((Math.abs(magnification) > 1 && Math.abs(magnification) < 9999)) g.drawString("Magnification: Enlarged", 20, 320); else if (arrow_x == 350 - 20 * focalLength || arrow_x == 350 + 20 * focalLength || (int) (Math.abs(magnification)) == 1) g.drawString("Magnification: None", 20, 320); else if (Math.abs(magnification) < 1 && Math.abs(magnification) > 0) g.drawString("Magnification: Diminished", 20, 320); else g.drawString("Magnification: N/A", 20, 320); if (arrow_x == 350 - 10 * focalLength || arrow_x == 350 + 10 * focalLength) g.drawString("Orientation: N/A", 20, 335); else if ((arrow_y2 < 200 && y_pointOfIntersection < 200) || (arrow_y2 > 200 && y_pointOfIntersection > 200)) g.drawString("Orientation: Upright", 20, 335); else g.drawString("Orientation: Inverted", 20, 335); if (arrow_x == 350 - 10 * focalLength || arrow_x == 350 + 10 * focalLength) g.drawString("Type: N/A", 20, 350); else if ((x_pointOfIntersection < 350 && arrow_x < 350) || (x_pointOfIntersection > 350 && arrow_x > 350)) g.drawString("Type: Virtual", 20, 350); else g.drawString("Type: Real", 20, 350); } else { g.drawString("Magnification: Diminished", 20, 320); g.drawString("Orientation: Upright", 20, 335); g.drawString("Type: Virtual", 20, 350); } height_image /= 10; if (height_image > 9999 || height_image < -9999) Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>"); else Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= " + height_image + "</html>"); distance_image = x_pointOfIntersection - 350; distance_image /= 10; if (distance_image > 9999 || distance_image < -9999) Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>"); else Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= " + distance_image + "</html>"); } else // Else: mirrors { if (type == 0) // If converging { // draws converging mirror g.drawArc( 500 - 2 * radiusOfCurvature, 200 - radiusOfCurvature, 2 * radiusOfCurvature, 2 * radiusOfCurvature, 60, -120); // draws horizontal line from the tip of the arrow to the lens (line 1/4) g.setColor(Color.RED); x_arcIntersection_1 = (int) ((Math.sqrt(Math.abs(Math.pow(radiusOfCurvature, 2) - Math.pow(arrow_y2 - 200, 2)))) + (500 - radiusOfCurvature)); g.drawLine(arrow_x, arrow_y2, x_arcIntersection_1, arrow_y2); // line 2/4 dy_1 = arrow_y2 - 200; dx_1 = x_arcIntersection_1 - (500 - focalLength * 10); slope_1 = dy_1 / dx_1; y_intercept_1 = 200 - slope_1 * (500 - focalLength * 10); // Calculates coordinates of points on the edge of screen (endpoints) y_screenIntersection_1 = (int) (Math.round(y_intercept_1)); if (slope_1 != 0) if (arrow_y2 <= 200) x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1)); else x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1)); if (x_screenIntersection_1 >= 0 && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge if (arrow_y2 <= 200) g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 400); else g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 0); else g.drawLine( x_arcIntersection_1, arrow_y2, 0, y_screenIntersection_1); // Else: endpoint is on the y-edge // line 3/4 if (!(arrow_x > 495 - focalLength * 10 && arrow_x < 505 - focalLength * 10)) { dy_2 = 200 - arrow_y2; dx_2 = (500 - 10 * focalLength) - arrow_x; slope_2 = dy_2 / dx_2; y_intercept_2 = arrow_y2 - slope_2 * arrow_x; quadratic_a = (float) (Math.pow(slope_2, 2) + 1); quadratic_b = (float) (((2 * slope_2 * y_intercept_2) - (400 * slope_2) + ((radiusOfCurvature - 500) * 2))); quadratic_c = (float) ((Math.pow(y_intercept_2, 2) - Math.pow(radiusOfCurvature, 2) - (400 * y_intercept_2) + 40000 + Math.pow((radiusOfCurvature - 500), 2))); discriminant = (float) (Math.pow(quadratic_b, 2) - (4 * quadratic_a * quadratic_c)); if (discriminant >= 0) x_arcIntersection_2 = (int) (Math.max( ((-quadratic_b + Math.sqrt(discriminant)) / (2 * quadratic_a)), ((-quadratic_b - Math.sqrt(discriminant)) / (2 * quadratic_a)))); else System.out.println("Error, imaginary root!"); y_arcIntersection_2 = (int) (slope_2 * x_arcIntersection_2 + y_intercept_2); g.drawLine(arrow_x, arrow_y2, x_arcIntersection_2, y_arcIntersection_2); // System.out.println ("slope: " + slope_2 + "\n yintercept: " + y_intercept_2 + "\n // quadratic-a: " + quadratic_a + "\n quadratic-b: " + quadratic_b + "\n quadratic_c: " + // quadratic_c + "\n discriminant: " + discriminant + "\n xarcintersection2: " + // x_arcIntersection_2 + "\n yarcintersection2: " + y_arcIntersection_2); // line 4/4 g.drawLine(x_arcIntersection_2, y_arcIntersection_2, 0, y_arcIntersection_2); // POI between line 2 and line 4 x_pointOfIntersection = (int) ((y_arcIntersection_2 - y_intercept_1) / slope_1); y_pointOfIntersection = y_arcIntersection_2; g.setColor(Color.ORANGE); g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection, 200); if (y_pointOfIntersection < 200) { g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection - 7, y_pointOfIntersection + 7); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection + 7, y_pointOfIntersection + 7); } else { g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection - 7, y_pointOfIntersection - 7); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection + 7, y_pointOfIntersection - 7); } // Same side image line continuation if (arrow_x > 500 - 10 * focalLength) { g.setColor(Color.YELLOW); g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_1, arrow_y2); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_2, y_arcIntersection_2); } } } else // Diverging { // draws converging mirror g.drawArc( 350, 200 - radiusOfCurvature, 2 * radiusOfCurvature, 2 * radiusOfCurvature, 120, 120); // draws horizontal line from the tip of the arrow to the lens (line 1/4) g.setColor(Color.RED); x_arcIntersection_1 = (int) (-(Math.sqrt(Math.pow(radiusOfCurvature, 2) - Math.pow(arrow_y2 - 200, 2))) + (350 + radiusOfCurvature)); g.drawLine(arrow_x, arrow_y2, x_arcIntersection_1, arrow_y2); // line 2/4 dy_1 = arrow_y2 - 200; dx_1 = x_arcIntersection_1 - (350 + focalLength * 10); slope_1 = dy_1 / dx_1; y_intercept_1 = 200 - slope_1 * (350 + focalLength * 10); // Calculates coordinates of points on the edge of screen (endpoints) y_screenIntersection_1 = (int) (Math.round(y_intercept_1)); if (slope_1 != 0) if (arrow_y2 <= 200) x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1)); else if (arrow_y2 > 200) x_screenIntersection_1 = (int) (Math.round(400 - y_intercept_1 / slope_1)); if (x_screenIntersection_1 >= 0 && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge if (arrow_y2 <= 200) g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 0); else g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 400); else g.drawLine( x_arcIntersection_1, arrow_y2, 0, y_screenIntersection_1); // Else: endpoint is on the y-edge // line 3/4 dy_2 = 200 - arrow_y2; dx_2 = (350 + 10 * focalLength) - arrow_x; slope_2 = dy_2 / dx_2; y_intercept_2 = arrow_y2 - slope_2 * arrow_x; quadratic_a = (float) (Math.pow(slope_2, 2) + 1); quadratic_b = (float) ((2 * slope_2 * y_intercept_2) - (400 * slope_2) - (2 * radiusOfCurvature + 700)); quadratic_c = (float) ((Math.pow(y_intercept_2, 2) - Math.pow(radiusOfCurvature, 2) - (400 * y_intercept_2) + 40000 + Math.pow((radiusOfCurvature + 350), 2))); discriminant = (float) (Math.pow(quadratic_b, 2) - (4 * quadratic_a * quadratic_c)); if (discriminant >= 0) x_arcIntersection_2 = (int) (Math.min( ((-quadratic_b + Math.sqrt(discriminant)) / (2 * quadratic_a)), ((-quadratic_b - Math.sqrt(discriminant)) / (2 * quadratic_a)))); else System.out.println("Error, imaginary root!"); y_arcIntersection_2 = (int) (slope_2 * x_arcIntersection_2 + y_intercept_2); g.drawLine(arrow_x, arrow_y2, x_arcIntersection_2, y_arcIntersection_2); // System.out.println ("slope: " + slope_2 + "\n yintercept: " + y_intercept_2 + "\n // quadratic-a: " + quadratic_a + "\n quadratic-b: " + quadratic_b + "\n quadratic_c: " + // quadratic_c + "\n discriminant: " + discriminant + "\n xarcintersection2: " + // x_arcIntersection_2 + "\n yarcintersection2: " + y_arcIntersection_2); // line 4/4 g.drawLine(x_arcIntersection_2, y_arcIntersection_2, 0, y_arcIntersection_2); // POI between line 2 and line 4 x_pointOfIntersection = (int) ((y_arcIntersection_2 - y_intercept_1) / slope_1); y_pointOfIntersection = y_arcIntersection_2; g.setColor(Color.ORANGE); g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection, 200); if (y_pointOfIntersection < 200) { g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection - 7, y_pointOfIntersection + 7); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection + 7, y_pointOfIntersection + 7); } else { g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection - 7, y_pointOfIntersection - 7); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection + 7, y_pointOfIntersection - 7); } // Same side image line continuation g.setColor(Color.YELLOW); g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_1, arrow_y2); g.drawLine( x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_2, y_arcIntersection_2); } // Mag calculations height_image = 200 - y_pointOfIntersection; height_object = 200 - arrow_y2; if (height_object != 0) magnification = height_image / height_object; if (magnification <= 9999 && magnification >= -9999) Optics.txt_magnification.setText("" + roundTwoDecimals(magnification)); else if (magnification > 9999) { magnification = Double.POSITIVE_INFINITY; Optics.txt_magnification.setText("N/A"); } else { magnification = Double.NEGATIVE_INFINITY; Optics.txt_magnification.setText("N/A"); } // Characteristics g.setColor(Color.ORANGE); g.drawString("Image Characteristics:", 20, 300); if (type == 0) { if ((Math.abs(magnification) > 1 && Math.abs(magnification) < 9999) && arrow_x != 500 - 10 * focalLength) g.drawString("Magnification: Enlarged", 20, 320); else if ((int) (Math.abs(magnification)) == 1) g.drawString("Magnification: None", 20, 320); else if (Math.abs(magnification) < 1 && Math.abs(magnification) > 0) g.drawString("Magnification: Diminished", 20, 320); else { g.drawString("Magnification: N/A", 20, 320); Optics.txt_magnification.setText("N/A"); Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>"); Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>"); } if (arrow_x == 500 - 10 * focalLength) g.drawString("Orientation: N/A", 20, 335); else if ((arrow_y2 < 200 && y_pointOfIntersection < 200) || (arrow_y2 > 200 && y_pointOfIntersection > 200)) g.drawString("Orientation: Upright", 20, 335); else g.drawString("Orientation: Inverted", 20, 335); if (arrow_x == 500 - 10 * focalLength) g.drawString("Type: N/A", 20, 350); else if (x_pointOfIntersection < 500 && arrow_x < 500) g.drawString("Type: Real", 20, 350); else if (x_pointOfIntersection > 500 && arrow_x < 500) g.drawString("Type: Virtual", 20, 350); } else { g.drawString("Magnification: Diminished", 20, 320); g.drawString("Orientation: Upright", 20, 335); g.drawString("Type: Virtual", 20, 350); } height_image /= 10; if (height_image > 9999 || height_image < -9999 || arrow_x == 500 - 10 * focalLength) Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>"); else Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= " + height_image + "</html>"); if (type == 0) distance_image = x_pointOfIntersection - 500; else distance_image = x_pointOfIntersection - 350; distance_image /= 10; if (distance_image > 9999 || distance_image < -9999 || arrow_x == 500 - 10 * focalLength) Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>"); else Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= " + distance_image + "</html>"); } }