public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); g2d.setStroke(new BasicStroke(1.0f)); if (isOpaque()) { g2d.setColor(getBackground()); g2d.fillRect(0, 0, getWidth(), getHeight()); } g2d.setColor(Color.black); g2d.drawLine(0, frameHeight / 2, frameWidth, frameHeight / 2); g2d.drawLine(frameWidth / 2, 0, frameWidth / 2, frameHeight); for (int i = unityX; i < frameWidth / 2; i += unityX) { g2d.drawLine( frameWidth / 2 + i, frameHeight / 2 - 3, frameWidth / 2 + i, frameHeight / 2 + 3); g2d.drawLine( frameWidth / 2 - i, frameHeight / 2 - 3, frameWidth / 2 - i, frameHeight / 2 + 3); } for (int i = unityY; i < frameHeight / 2; i += unityY) { g2d.drawLine( frameWidth / 2 - 3, frameHeight / 2 + i, frameWidth / 2 + 3, frameHeight / 2 + i); g2d.drawLine( frameWidth / 2 - 3, frameHeight / 2 - i, frameWidth / 2 + 3, frameHeight / 2 - i); } g2d.setColor(Color.blue); function.drawFunctionToGraphic(g2d, frameWidth, frameHeight, unityX, unityY); paintCurrentMethodState(g2d); g2d.dispose(); }
private static int[] makeGradientPallet() { BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); Point2D start = new Point2D.Float(0f, 0f); Point2D end = new Point2D.Float(99f, 0f); float[] dist = {0f, .5f, 1f}; Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN}; g2.setPaint(new LinearGradientPaint(start, end, dist, colors)); g2.fillRect(0, 0, 100, 1); g2.dispose(); int width = image.getWidth(null); int[] pallet = new int[width]; PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width); try { pg.grabPixels(); } catch (InterruptedException ex) { ex.printStackTrace(); } return pallet; }