private void drawEnemyMessage(Graphics2D g2d) { Font messageFont = new Font("Arial", Font.BOLD, 15); g2d.setFont(messageFont); if (enemyMoveChosen) { String text = ""; if (drawMoveApplication) { text = effectMessage; } else { text = enemy.getName() + " uses " + lastEnemyMove.getName() + " on " + MainCharacter.characterName + "!"; } ArrayList<String> lines = new ArrayList<>(); int width = (int) (messageFont.getStringBounds(text, g2d.getFontRenderContext()).getWidth()); if (width > cardButton.getWidth() * 2 + 40) { String[] split = text.split("\\s+"); int tempWidth = 0; int maxWidth = 0; String line = ""; for (int i = 0; i < split.length; i++) { tempWidth += (int) (messageFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); if (tempWidth > cardButton.getWidth() * 2 + 40) { tempWidth = (int) (messageFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); lines.add(line); line = split[i] + " "; } else { line = line + split[i] + " "; } if (tempWidth > maxWidth) { maxWidth = tempWidth; } } lines.add(line); } else { lines.add(text); } for (int i = 0; i < lines.size(); i++) { g2d.drawString(lines.get(i), cardButton.getX(), cardButton.getY() + 15 + 30 * i); } } else { g2d.drawString("The Enemy is deciding", cardButton.getX(), cardButton.getY() + 15); g2d.drawString("their move...", cardButton.getX(), cardButton.getY() + 15 + 30); } }
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw ordinate. g2.draw(new Line2D.Double(Points, Points, Points, h - Points)); // Draw abcissa. g2.draw(new Line2D.Double(Points, h - Points, w - Points, h - Points)); // Draw labels. Font font = g2.getFont(); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); float sh = lm.getAscent() + lm.getDescent(); // Ordinate label. String s = "Strecke"; float sy = Points + ((h - 2 * Points) - s.length() * sh) / 2 + lm.getAscent(); for (int i = 0; i < s.length(); i++) { String letter = String.valueOf(s.charAt(i)); float sw = (float) font.getStringBounds(letter, frc).getWidth(); float sx = (Points - sw) / 2; g2.drawString(letter, sx, sy); sy += sh; } // Abcissa label. s = "Datum"; sy = h - Points + (Points - sh) / 2 + lm.getAscent(); float sw = (float) font.getStringBounds(s, frc).getWidth(); float sx = (w - sw) / 2; g2.drawString(s, sx, sy); // Draw lines. double xInc = (double) (w - 2 * Points) / (data.length - 1); double scale = (double) (h - 2 * Points) / getMax(); g2.setPaint(Color.green.darker()); for (int i = 0; i < data.length - 1; i++) { double x1 = Points + i * xInc; double y1 = h - Points - scale * data[i]; double x2 = Points + (i + 1) * xInc; double y2 = h - Points - scale * data[i + 1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } // Mark data points. g2.setPaint(Color.red); for (int i = 0; i < data.length; i++) { double x = Points + i * xInc; double y = h - Points - scale * data[i]; g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4)); } }
public Dimension getPreferredSize(int rows) { toolkit.lockAWT(); try { Dimension minSize = getMinimumSize(rows); if (items.isEmpty()) { return minSize; } if (!isDisplayable()) { return new Dimension(); } int maxItemWidth = minSize.width; Graphics2D gr = (Graphics2D) getGraphics(); FontRenderContext frc = gr.getFontRenderContext(); Font font = getFont(); for (int i = 0; i < items.size(); i++) { String item = getItem(i); int itemWidth = font.getStringBounds(item, frc).getBounds().width; if (itemWidth > maxItemWidth) { maxItemWidth = itemWidth; } } gr.dispose(); return new Dimension(maxItemWidth, minSize.height); } finally { toolkit.unlockAWT(); } }
/* * (non-Javadoc) * * @see org.math.plot.render.AbstractDrawer#drawStringRatio(java.lang.String, * double[], double, double, double) */ public void drawTextBase(String label, double... rC) { int[] sC = projection.screenProjectionBase(rC); // Corner offset adjustment : Text Offset is used Here FontRenderContext frc = comp2D.getFontRenderContext(); Font font1 = comp2D.getFont(); int x = sC[0]; int y = sC[1]; double w = font1.getStringBounds(label, frc).getWidth(); double h = font1.getSize2D(); x -= (int) (w * text_Eastoffset); y += (int) (h * text_Northoffset); int wc = (int) (w * FastMath.cos(text_angle) + h * FastMath.sin(text_angle)); int hc = (int) (h * FastMath.cos(text_angle) + w * FastMath.sin(text_angle)); if (!comp2D.hitClip(x, y, wc, hc)) { return; } if (text_angle != 0) { comp2D.rotate(text_angle, x + w / 2, y - h / 2); } String[] lines = label.split("\n"); for (int i = 0; i < lines.length; i++) { comp2D.drawString(lines[i], x, y); y += h; } // comp2D.drawString(label, x, y); if (text_angle != 0) { comp2D.rotate(-text_angle, x + w / 2, y - h / 2); } }
/** * @param g2 * @throws IOException */ private void drawNode(Map<String, Node> nodes, Graphics2D g2, Font font) throws IOException { for (Node node : nodes.values()) { String name = node.getName(); if (nodeInfos.get(node.getType()) != null) { BufferedImage bi2 = ImageIO.read(getClass().getResourceAsStream( "icons/48/" + nodeInfos.get(node.getType()))); g2.drawImage(bi2, node.getX(), node.getY(), null); } else { int x = node.getX(); int y = node.getY(); int w = node.getWitdth(); int h = node.getHeight(); g2.setColor(DEFAULT_FILL_COLOR); g2.fillRoundRect(x, y, w, h, RECT_ROUND, RECT_ROUND); g2.setColor(DEFAULT_STROKE_COLOR); g2.setStroke(DEFAULT_STROKE); g2.drawRoundRect(x, y, w, h, RECT_ROUND, RECT_ROUND); FontRenderContext frc = g2.getFontRenderContext(); Rectangle2D r2 = font.getStringBounds(name, frc); int xLabel = (int) (node.getX() + ((node.getWitdth() - r2.getWidth()) / 2)); int yLabel = (int) ((node.getY() + ((node.getHeight() - r2.getHeight()) / 2)) - r2.getY()); g2.setStroke(DEFAULT_LINE_STROKE); g2.setColor(Color.black); g2.drawString(name, xLabel, yLabel); } } }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect); }
/** * Gets the page count of this section. * * @param g2 the graphics context * @param pf the page format * @return the number of pages needed */ public int getPageCount(Graphics2D g2, PageFormat pf) { if (message.equals("")) return 0; FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); Rectangle2D bounds = f.getStringBounds(message, context); scale = pf.getImageableHeight() / bounds.getHeight(); double width = scale * bounds.getWidth(); int pages = (int) Math.ceil(width / pf.getImageableWidth()); return pages; }
public void paintComponent(Graphics g) { // необходиом чтобы текст коректно отрисовывался в окне super.paintComponent(g); // рисуем текст в окне Graphics2D g2 = (Graphics2D) g; AffineTransform t = g2.getTransform(); g.drawString("It is text", 5, 5); // создание шрифта Font f = new Font("SanasSerif", Font.ITALIC, 20); g2.setFont(f); g2.drawString("It is new text", 5, 33); String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (int i = 5; i < 20; i++) { g2.rotate(-0.05); g2.setColor( new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); Font f1 = new Font(fontNames[i], Font.BOLD, 20); g2.setFont(f1); g2.drawString(fontNames[i], 5, 20 * i); } // текст в центре g2.setTransform(t); // возращение к кординатам, которые запонилив начале Font f2 = new Font("SanasSerif", Font.ITALIC, 20); g2.setFont(f2); String s = "It is center!"; FontRenderContext context = g2.getFontRenderContext(); Rectangle2D r = f2.getStringBounds(s, context); double x1 = (getWidth() - r.getWidth()) / 2; double y1 = (getHeight() - r.getHeight()) / 2; double ascent = -r.getY(); // узнаем высоту текста double y2 = y1 + ascent; Rectangle2D rect = new Rectangle2D.Double(x1, y1, r.getWidth(), r.getHeight()); g2.setColor(Color.YELLOW); g2.fill(rect); g2.setColor(Color.red); g2.drawString(s, (int) x1, (int) y2); g2.setColor(Color.blue); g2.draw(new Line2D.Double(x1, y2, x1 + r.getWidth(), y2)); g2.draw(rect); }
public void drawShadowedText(String label, float alpha, double... pC) { int[] sC = projection.screenProjection(pC); // Corner offset adjustment : Text Offset is used Here FontRenderContext frc = comp2D.getFontRenderContext(); Font font1 = comp2D.getFont(); int x = sC[0]; int y = sC[1]; double w = font1.getStringBounds(label, frc).getWidth(); double h = font1.getSize2D(); x -= (int) (w * text_Eastoffset); y += (int) (h * text_Northoffset); int wc = (int) (w * FastMath.cos(text_angle) + h * FastMath.sin(text_angle)); int hc = (int) (h * FastMath.cos(text_angle) + w * FastMath.sin(text_angle)); if (!comp2D.hitClip(x, y, wc, hc)) { return; } if (text_angle != 0) { comp2D.rotate(text_angle, x + w / 2, y - h / 2); } Composite cs = comp2D.getComposite(); Color c = comp2D.getColor(); String[] lines = label.split("\n"); for (int i = 0; i < lines.length; i++) { comp2D.setColor(Color.white); comp2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); comp2D.fillRect(x, y - (int) h, (int) w, (int) h); comp2D.setComposite(cs); comp2D.setColor(c); comp2D.drawString(lines[i], x, y); y += h; } if (text_angle != 0) { comp2D.rotate(-text_angle, x + w / 2, y - h / 2); } }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); // measure the size of the message FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); // set (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // add ascent to y to reach the baseline double ascent = -bounds.getY(); double baseY = y + ascent; // draw the message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // draw the baseline g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // draw the enclosing rectangle Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect); }
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); double xScale = (w - 2 * PAD) / (xMax - xMin); double yScale = (h - 2 * PAD) / (yMax - yMin); if (firstTime) System.out.printf("xScale = %.1f yScale = %.1f%n", xScale, yScale); Point2D.Double origin = new Point2D.Double(); // Axes origin. Point2D.Double offset = new Point2D.Double(); // Locate data. if (xMax < 0) { origin.x = w - PAD; offset.x = origin.x - xScale * xMax; } else if (xMin < 0) { origin.x = PAD - xScale * xMin; offset.x = origin.x; } else { origin.x = PAD; offset.x = PAD - xScale * xMin; } if (yMax < 0) { origin.y = h - PAD; offset.y = origin.y - yScale * yMax; } else if (yMin < 0) { origin.y = PAD - yScale * yMin; offset.y = origin.y; } else { origin.y = PAD; offset.y = PAD - yScale * yMin; } if (firstTime) { System.out.printf("origin = [%6.1f, %6.1f]%n", origin.x, origin.y); System.out.printf("offset = [%6.1f, %6.1f]%n", offset.x, offset.y); } // Draw abcissa. g2.draw(new Line2D.Double(PAD, origin.y, w - PAD, origin.y)); // Draw ordinate. g2.draw(new Line2D.Double(origin.x, PAD, origin.x, h - PAD)); g2.setPaint(Color.red); // Mark origin. g2.fill(new Ellipse2D.Double(origin.x - 2, origin.y - 2, 4, 4)); // Plot data. g2.setPaint(Color.blue); for (int i = 0; i < x.length; i++) { double x1 = offset.x + xScale * x[i]; double y1 = offset.y + yScale * y[i]; if (firstTime) System.out.printf("i = %d x1 = %6.1f y1 = %.1f%n", i, x1, y1); g2.fill(new Ellipse2D.Double(x1 - 2, y1 - 2, 4, 4)); g2.drawString(String.valueOf(i), (float) x1 + 3, (float) y1 - 3); } // Draw extreme data values. g2.setPaint(Color.black); Font font = g2.getFont(); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); String s = String.format("%.1f", xMin); float width = (float) font.getStringBounds(s, frc).getWidth(); double x = offset.x + xScale * xMin; g2.drawString(s, (float) x, (float) origin.y + lm.getAscent()); s = String.format("%.1f", xMax); width = (float) font.getStringBounds(s, frc).getWidth(); x = offset.x + xScale * xMax; g2.drawString(s, (float) x - width, (float) origin.y + lm.getAscent()); s = String.format("%.1f", yMin); width = (float) font.getStringBounds(s, frc).getWidth(); double y = offset.y + yScale * yMin; g2.drawString(s, (float) origin.x + 1, (float) y + lm.getAscent()); s = String.format("%.1f", yMax); width = (float) font.getStringBounds(s, frc).getWidth(); y = offset.y + yScale * yMax; g2.drawString(s, (float) origin.x + 1, (float) y); if (firstTime) System.out.println("------------------------------"); firstTime = false; }
/** * Returns the bounds of the characters indexed in the specified <code>CharacterIterator</code> in * the specified <code>Graphics</code> context. * * <p>Note: The returned bounds is in baseline-relative coordinates (see {@link * java.awt.FontMetrics class notes}). * * @param ci the specified <code>CharacterIterator</code> * @param beginIndex the initial offset in <code>ci</code> * @param limit the end index of <code>ci</code> * @param context the specified <code>Graphics</code> context * @return a <code>Rectangle2D</code> that is the bounding box of the characters indexed in the * specified <code>CharacterIterator</code> in the specified <code>Graphics</code> context. * @see java.awt.Font#getStringBounds(CharacterIterator, int, int, FontRenderContext) */ public Rectangle2D getStringBounds( CharacterIterator ci, int beginIndex, int limit, Graphics context) { return font.getStringBounds(ci, beginIndex, limit, myFRC(context)); }
/** * Returns the bounds of the specified array of characters in the specified <code>Graphics</code> * context. The bounds is used to layout the <code>String</code> created with the specified array * of characters, <code>beginIndex</code> and <code>limit</code>. * * <p>Note: The returned bounds is in baseline-relative coordinates (see {@link * java.awt.FontMetrics class notes}). * * @param chars an array of characters * @param beginIndex the initial offset of the array of characters * @param limit the end offset of the array of characters * @param context the specified <code>Graphics</code> context * @return a <code>Rectangle2D</code> that is the bounding box of the specified character array in * the specified <code>Graphics</code> context. * @see java.awt.Font#getStringBounds(char[], int, int, FontRenderContext) */ public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context) { return font.getStringBounds(chars, beginIndex, limit, myFRC(context)); }
/** * Returns the bounds of the specified <code>String</code> in the specified <code>Graphics</code> * context. The bounds is used to layout the <code>String</code>. * * <p>Note: The returned bounds is in baseline-relative coordinates (see {@link * java.awt.FontMetrics class notes}). * * @param str the specified <code>String</code> * @param context the specified <code>Graphics</code> context * @return a {@link Rectangle2D} that is the bounding box of the specified <code>String</code> in * the specified <code>Graphics</code> context. * @see java.awt.Font#getStringBounds(String, FontRenderContext) */ public Rectangle2D getStringBounds(String str, Graphics context) { return font.getStringBounds(str, myFRC(context)); }
@Override public void draw(Graphics2D g2d) { // After updating we will draw all the objects to the screen g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, windowWidth, windowHeight); g2d.setColor(Color.BLACK); Font font = new Font("Arial", Font.BOLD, 30); g2d.setFont(font); int textWidth = (int) (font.getStringBounds("You Are Now In Battle State", g2d.getFontRenderContext()) .getWidth()); g2d.drawString( "You Are Now In Battle State", windowWidth / 2 - textWidth / 2, windowHeight / 2); // Draw the battle images of the enemy and MainCharacter g2d.drawImage( mainCharacterImage, 60, 3 * windowHeight / 4 - 2 * 60, mainCharacterImage.getWidth() * 2, mainCharacterImage.getHeight() * 2, null); g2d.drawImage( enemyImage, windowWidth - 60 - enemyImage.getWidth(), 60, enemyImage.getWidth(), enemyImage.getHeight(), null); // Draw an action box at the bottom where we decide our actions on each turn g2d.setColor(Color.CYAN); g2d.fillRect(0, 3 * windowHeight / 4, windowWidth, windowHeight / 4 + 2); // Draw the hp and mp bars of the MainCharacter and the Enemy g2d.setColor(Color.BLACK); g2d.drawRect(0, 3 * windowHeight / 4, windowWidth, windowHeight / 4 + 2); Font hpFont = new Font("Arial", Font.BOLD, 15); g2d.setFont(hpFont); String text = "Health: " + String.valueOf(StartGameState.character.health) + "/" + String.valueOf(StartGameState.character.maxHealth); textWidth = (int) (hpFont.getStringBounds(text, g2d.getFontRenderContext()).getWidth()); g2d.drawString( text, 60 + mainCharacterImage.getWidth() * 2 + 45, 3 * windowHeight / 4 - 30 - 15 - 20); text = "Mana: " + String.valueOf(StartGameState.character.mana) + "/" + String.valueOf(StartGameState.character.maxMana); g2d.drawString(text, 60 + mainCharacterImage.getWidth() * 2 + 45, 3 * windowHeight / 4 - 30); text = MainCharacter.characterName; g2d.drawString( text, 60 + mainCharacterImage.getWidth() * 2 + 45, 3 * windowHeight / 4 - 30 - 2 * 15 - 2 * 20); g2d.setColor(Color.GRAY); g2d.fillRoundRect( 60 + mainCharacterImage.getWidth() * 2 + 45 + textWidth + 20, 3 * windowHeight / 4 - 45 - 15 - 20, 100, 15, 3, 3); g2d.fillRoundRect( 60 + mainCharacterImage.getWidth() * 2 + 45 + textWidth + 20, 3 * windowHeight / 4 - 45, 100, 15, 3, 3); g2d.setColor(Color.GREEN); g2d.fillRoundRect( 60 + mainCharacterImage.getWidth() * 2 + 45 + textWidth + 20, 3 * windowHeight / 4 - 45 - 15 - 20, (int) (((float) StartGameState.character.health / (float) StartGameState.character.maxHealth) * 100), 15, 3, 3); g2d.setColor(Color.BLUE); g2d.fillRoundRect( 60 + mainCharacterImage.getWidth() * 2 + 45 + textWidth + 20, 3 * windowHeight / 4 - 45, (int) (((float) StartGameState.character.mana / (float) StartGameState.character.maxMana) * 100), 15, 3, 3); g2d.setColor(Color.BLACK); g2d.drawRoundRect( 60 + mainCharacterImage.getWidth() * 2 + 45 + textWidth + 20, 3 * windowHeight / 4 - 45 - 15 - 20, 100, 15, 3, 3); g2d.drawRoundRect( 60 + mainCharacterImage.getWidth() * 2 + 45 + textWidth + 20, 3 * windowHeight / 4 - 45, 100, 15, 3, 3); // Enemy health and mana bars text = "Health: " + String.valueOf(enemy.health) + "/" + String.valueOf(enemy.maxHealth); textWidth = (int) (hpFont.getStringBounds(text, g2d.getFontRenderContext()).getWidth()); g2d.drawString( text, windowWidth - 60 - enemyImage.getWidth() - 45 - 100 - 20 - textWidth, 60 + 15); text = "Mana: " + String.valueOf(enemy.mana) + "/" + String.valueOf(enemy.maxMana); g2d.drawString( text, windowWidth - 60 - enemyImage.getWidth() - 45 - 100 - 20 - textWidth, 60 + 2 * 15 + 20); text = enemy.getName(); g2d.drawString( text, windowWidth - 60 - enemyImage.getWidth() - 45 - 100 - 20 - textWidth, 60 - 20); g2d.setColor(Color.GRAY); g2d.fillRoundRect(windowWidth - 60 - enemyImage.getWidth() - 45 - 100, 60, 100, 15, 3, 3); g2d.fillRoundRect( windowWidth - 60 - enemyImage.getWidth() - 45 - 100, 60 + 15 + 20, 100, 15, 3, 3); g2d.setColor(Color.GREEN); g2d.fillRoundRect( windowWidth - 60 - enemyImage.getWidth() - 45 - 100, 60, (int) (((float) enemy.health / (float) enemy.maxHealth) * 100), 15, 3, 3); g2d.setColor(Color.BLUE); g2d.fillRoundRect( windowWidth - 60 - enemyImage.getWidth() - 45 - 100, 60 + 15 + 20, (int) (((float) enemy.mana / (float) enemy.maxMana) * 100), 15, 3, 3); g2d.setColor(Color.BLACK); g2d.drawRoundRect(windowWidth - 60 - enemyImage.getWidth() - 45 - 100, 60, 100, 15, 3, 3); g2d.drawRoundRect( windowWidth - 60 - enemyImage.getWidth() - 45 - 100, 60 + 15 + 20, 100, 15, 3, 3); // Draw the player's hand and the current card int x = drawButton.getX() + drawButton.getWidth() + 40; int y = drawButton.getY() + 10; int i = 0; if (deckCount < MainCharacter.deck.size()) { g2d.drawImage(cardBack, 3 * windowWidth / 4 - 15 - cardBack.getWidth(), y, null); } if (hand.size() > 0) { int cardSpacing = cardWidth / hand.size(); for (Card card : hand.values()) { if (i != cardScroller.getCountX() - 1) { card.draw(g2d, x + i * (cardSpacing), y, 0, 0); } i++; } Card current = hand.getIndexed(cardScroller.getCountX() - 1); current.draw(g2d, x + (cardScroller.getCountX() - 1) * (cardSpacing), y - 20, 0, 0); // If it is MainCharacter turn then we draw the actions in the action box g2d.setFont(new Font("Arial", Font.BOLD, 20)); g2d.setColor(Color.BLACK); int explanationWidth = windowWidth / 4 - 25; g2d.drawString(current.getName(), 3 * windowWidth / 4 + 15, cardButton.getY()); Font explanationFont = new Font("Arial", Font.BOLD, 10); g2d.setFont(explanationFont); ArrayList<String> lines = new ArrayList<>(); // Determines width and height of only the text String dialog = current.getExplanation(); int width = (int) (explanationFont.getStringBounds(dialog, g2d.getFontRenderContext()).getWidth()); if (width > explanationWidth) { String[] split = dialog.split("\\s+"); int tempWidth = 0; int maxWidth = 0; String line = ""; for (i = 0; i < split.length; i++) { tempWidth += (int) (explanationFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); if (tempWidth > explanationWidth) { tempWidth = (int) (explanationFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); lines.add(line); line = split[i] + " "; } else { line = line + split[i] + " "; } if (tempWidth > maxWidth) { maxWidth = tempWidth; } } lines.add(line); } else { lines.add(dialog); } for (i = 0; i < lines.size(); i++) { g2d.drawString( lines.get(i), 3 * windowWidth / 4 + 15, cardButton.getY() + (i + 1) * (10 + 4)); } g2d.setFont(hpFont); String[] effects = current.getEffectStrings(); for (i = 0; i < effects.length; i++) { g2d.drawString( effects[i], 3 * windowWidth / 4 + 15, cardButton.getY() + (lines.size()) * (10 + 4) + (i + 1) * (5 + 15)); } g2d.drawString( "Mana Cost: " + String.valueOf(current.getManaCost()), 3 * windowWidth / 4 + 15, cardButton.getY() + (lines.size()) * (10 + 4) + (i + 1) * (15 + 5)); } if (drawNoMana) { g2d.setFont(new Font("Arial", Font.BOLD, 15)); g2d.drawString("You don't have", cardButton.getX(), cardButton.getY() + 15); g2d.drawString("enough mana!", cardButton.getX(), cardButton.getY() + 2 * 15 + 10); } else if (drawNoCards) { g2d.setFont(new Font("Arial", Font.BOLD, 15)); g2d.drawString("You are out of", cardButton.getX(), cardButton.getY() + 15); g2d.drawString("cards to draw!", cardButton.getX(), cardButton.getY() + 2 * 15 + 10); } else if (drawItemUsed) { g2d.setFont(new Font("Arial", Font.BOLD, 15)); g2d.drawString("You have used", cardButton.getX(), cardButton.getY() + 15); g2d.drawString( itemMenu.getLastItem().name + "!", cardButton.getX(), cardButton.getY() + 2 * 15 + 10); } else if (turn == 0 && drawParalyzed) { g2d.setFont(new Font("Arial", Font.BOLD, 15)); g2d.drawString("You cannot move,", cardButton.getX(), cardButton.getY() + 15); g2d.drawString("you're paralyzed!", cardButton.getX(), cardButton.getY() + 2 * 15 + 10); } else if (turn == 1 && drawParalyzed) { Font messageFont = new Font("Arial", Font.BOLD, 15); g2d.setFont(messageFont); text = enemy.getName() + " cannot move, they're paralyzed!"; ArrayList<String> lines = new ArrayList<>(); int width = (int) (messageFont.getStringBounds(text, g2d.getFontRenderContext()).getWidth()); if (width > cardButton.getWidth() * 2 + 40) { String[] split = text.split("\\s+"); int tempWidth = 0; int maxWidth = 0; String line = ""; for (i = 0; i < split.length; i++) { tempWidth += (int) (messageFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); if (tempWidth > cardButton.getWidth() * 2 + 40) { tempWidth = (int) (messageFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); lines.add(line); line = split[i] + " "; } else { line = line + split[i] + " "; } if (tempWidth > maxWidth) { maxWidth = tempWidth; } } lines.add(line); } else { lines.add(text); } for (i = 0; i < lines.size(); i++) { g2d.drawString(lines.get(i), cardButton.getX(), cardButton.getY() + 15 + 30 * i); } } else if (turn == 0 && turnState == 4) { g2d.setFont(new Font("Arial", Font.BOLD, 15)); g2d.drawString("You try to", cardButton.getX(), cardButton.getY() + 15); g2d.drawString("run away...", cardButton.getX(), cardButton.getY() + 2 * 15 + 10); } else if (turn == 0 && turnState == 5) { Font messageFont = new Font("Arial", Font.BOLD, 15); g2d.setFont(messageFont); if (drawMoveApplication) { text = effectMessage; } else { text = "You have used " + playedCard.getName() + " on " + enemy.getName() + "!"; } ArrayList<String> lines = new ArrayList<>(); int width = (int) (messageFont.getStringBounds(text, g2d.getFontRenderContext()).getWidth()); if (width > cardButton.getWidth() * 2 + 40) { String[] split = text.split("\\s+"); int tempWidth = 0; int maxWidth = 0; String line = ""; for (i = 0; i < split.length; i++) { tempWidth += (int) (messageFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); if (tempWidth > cardButton.getWidth() * 2 + 40) { tempWidth = (int) (messageFont .getStringBounds(split[i] + "_", g2d.getFontRenderContext()) .getWidth()); lines.add(line); line = split[i] + " "; } else { line = line + split[i] + " "; } if (tempWidth > maxWidth) { maxWidth = tempWidth; } } lines.add(line); } else { lines.add(text); } for (i = 0; i < lines.size(); i++) { g2d.drawString(lines.get(i), cardButton.getX(), cardButton.getY() + 15 + 30 * i); } } else if (turn == 0) { drawActionBox(g2d); } else if (turn == 1) { drawEnemyMessage(g2d); } else if (turn == 2) { drawVictoryMessage(g2d); } else if (turn == 3) { drawLossMessage(g2d); } }