/** * Sets a mnemomic for the specified button. * * @param b button * @param mnem mnemonics that have already been assigned */ public static void setMnemonic(final AbstractButton b, final StringBuilder mnem) { // do not set mnemonics for Mac! Alt+key used for special characters. if (Prop.MAC) return; // find and assign unused mnemomic final String label = b.getText(); final int ll = label.length(); for (int l = 0; l < ll; l++) { final char ch = Character.toLowerCase(label.charAt(l)); if (!letter(ch) || mnem.indexOf(Character.toString(ch)) != -1) continue; b.setMnemonic(ch); mnem.append(ch); break; } }
public void tree(Graphics2D g2d, double size, int phase) { g2d.setColor(colors[phase % 3]); new TextLayout(theT.toString(), theFont, g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f); if (size > 10.0) { AffineTransform at = new AffineTransform(); at.setToTranslation(Twidth, -0.1); at.scale(0.6, 0.6); g2d.transform(at); size *= 0.6; new TextLayout(theR.toString(), theFont, g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f); at.setToTranslation(Rwidth + 0.75, 0); g2d.transform(at); Graphics2D g2dt = (Graphics2D) g2d.create(); at.setToRotation(-Math.PI / 2.0); g2dt.transform(at); tree(g2dt, size, phase + 1); g2dt.dispose(); at.setToTranslation(.75, 0); at.rotate(-Math.PI / 2.0); at.scale(-1.0, 1.0); at.translate(-Twidth, 0); g2d.transform(at); tree(g2d, size, phase); } g2d.setTransform(new AffineTransform()); }
private void fixTabs(int tabSize) { int rowIndex = 0; for (StringBuilder row1 : rows) { String row = row1.toString(); StringBuilder newRow = new StringBuilder(); char[] chars = row.toCharArray(); for (char c : chars) { if (c == '\t') { int spacesLeft = tabSize - newRow.length() % tabSize; if (DEBUG) { System.out.println("Found tab. Spaces left: " + spacesLeft); } String spaces = StringUtils.repeatString(" ", spacesLeft); newRow.append(spaces); } else { String character = Character.toString(c); newRow.append(character); } } rows.set(rowIndex, newRow); rowIndex++; } }
public void printLocation() { txtTranscript.insert( "You are at location " + row + "," + col + " in terrain: " + myMap.terrainTypes.get(Character.toString(myMap.terrain(row, col))) + "\n", txtTranscript.getText().length()); if (myMap.terrain(row, col) == '*') { Dragon found = new Dragon(); found.Dragon(); } }
public void first() { // System.out.println("Inside First. This is called only once"); java.util.Scanner s = null; try { s = new java.util.Scanner(new java.io.BufferedReader(new java.io.FileReader("sample"))); s.useLocale(java.util.Locale.US); position = 0; while (s.hasNext()) { if (s.hasNextLine()) { String c = s.nextLine(); ch[position] = c.charAt(0); for (int i = 0; i < 35; i++) { Character cha = new Character(c.charAt(i + 1)); String st = cha.toString(); data[position][i] = Integer.parseInt(st); } position++; } } } catch (java.io.IOException ert) { javax.swing.JOptionPane.showMessageDialog( null, ert, "Error", javax.swing.JOptionPane.ERROR_MESSAGE); } try { s = new java.util.Scanner(new java.io.BufferedReader(new java.io.FileReader("imageload"))); s.useLocale(java.util.Locale.US); while (s.hasNext()) { if (s.hasNextLine()) { imagedir = s.nextLine(); } } } catch (java.io.IOException ert) { javax.swing.JOptionPane.showMessageDialog( null, ert, "Error", javax.swing.JOptionPane.ERROR_MESSAGE); } s.close(); if (imagedir.equals("")) { javax.swing.JOptionPane.showMessageDialog( null, "This must be your first run of Live! Character Recognition.\nPlease set an image folder.", "Problem loading image", javax.swing.JOptionPane.INFORMATION_MESSAGE); } else { im.running(); } }
// Initialize Board public void init() { // Create cells and handlers cells = new JTextField[gameBoard.boardSize * gameBoard.boardSize + 1]; // Redraw Panel boardPanel.removeAll(); boardPanel.setLayout(new GridLayout(gameBoard.boardSize, gameBoard.boardSize)); // Set layout JTextFilter TextFilter = new JTextFilter(3); JTextDocumentListener JTextDocFilter = new JTextDocumentListener(); for (int i = 1; i <= gameBoard.boardSize * gameBoard.boardSize; i++) { cells[i] = new JTextField(); ((AbstractDocument) cells[i].getDocument()).setDocumentFilter(TextFilter); ((AbstractDocument) cells[i].getDocument()).addDocumentListener(JTextDocFilter); ((AbstractDocument) cells[i].getDocument()).putProperty("index", i); cells[i].setHorizontalAlignment(JTextField.CENTER); cells[i].setFont(new Font("Agency FB", Font.BOLD, 24)); // Add elements to the grid content pane boardPanel.add(cells[i]); } // Initialize booleans gameOver = false; // Clear Board for (int i = 1; i <= (gameBoard.boardSize * gameBoard.boardSize); i++) { String ch = Integer.toString(this.gameBoard.cells[i]); char chr = '-'; if (ch.compareTo("0") == 0 || ch == Character.toString(chr)) { cells[i].setText(""); } else { cells[i].setText(ch); cells[i].setBackground(Color.lightGray); } } // gameBoard.out(); setVisible(true); this.boardPanel.repaint(); this.gameTimer.reset(); jButtonSOLVE.setEnabled(true); }
public String ucWords(String wordsToConvert) { wordsToConvert = wordsToConvert.toLowerCase(); Boolean ucChar = false; String convertedString = ""; for (int i = 0; i < wordsToConvert.length(); i++) { if (i == 0 | ucChar) { convertedString += Character.toUpperCase(wordsToConvert.charAt(i)); } else { convertedString += Character.toString(wordsToConvert.charAt(i)); } if (Character.isWhitespace(wordsToConvert.charAt(i))) { ucChar = true; } else { ucChar = false; } } return convertedString; }
@EventHandler public void onTick(TickEvent event) { if (!bot.hasSpawned() || !bot.isConnected()) return; if (tickDelay > 0) { tickDelay--; return; } MainPlayerEntity player = bot.getPlayer(); if (player == null || !bot.hasSpawned() || spamMessage == null) return; if (nextMessage > 0) { nextMessage--; return; } try { String message = spamMessage; MessageFormatter formatter = new MessageFormatter(); synchronized (bots) { if (bots.size() > 0) { DarkBotMCSpambot bot = bots.get(++nextBot >= bots.size() ? nextBot = 0 : nextBot); if (bot != null && bot.bot != null && bot.bot.getSession() != null) formatter.setVariable("bot", bot.bot.getSession().getUsername()); } } if (spamList.length > 0) { formatter.setVariable( "spamlist", spamList[++nextSpamList >= spamList.length ? nextSpamList = 0 : nextSpamList]); } formatter.setVariable("rnd", Util.generateRandomString(15 + random.nextInt(6))); formatter.setVariable( "msg", Character.toString( msgChars[++nextMsgChar >= msgChars.length ? nextMsgChar = 0 : nextMsgChar])); message = formatter.format(message); bot.say(message); } catch (Exception e) { e.printStackTrace(); } nextMessage = messageDelay; }
/** * determineShortName * * @param fullName */ public static String determineShortName(String fullName) { String result = fullName; if (fullName != null) { String[] comps = fullName.split("\\."); if (comps.length == 1) { return fullName; } boolean truncate = true; for (int i = 0; i < comps.length; i++) { String comp = comps[i]; char c = comp.charAt(0); if (truncate && Character.isUpperCase(c)) { truncate = false; } if (truncate) { comps[i] = Character.toString(c); } } result = join(comps, '.'); } return result; }
static { for (char i = 0; i < CHARACTERS.length; i++) { CHARACTERS[i] = Character.toString(i); } }
public static void main(String[] args) { String lettersAndDigits = "abcdefghijklmnopqrstuvwxyz1234567890"; BiMap<Character, Double> rate = HashBiMap.create( new HashMap<Character, Double>() { { put('a', 8.167); put('b', 1.492); put('c', 2.782); put('d', 4.253); put('e', 12.702); put('f', 2.228); put('g', 2.015); put('h', 6.094); put('i', 6.966); put('j', 0.153); put('k', 0.772); put('l', 4.025); put('m', 2.406); put('n', 6.749); put('o', 7.507); put('p', 1.929); put('q', 0.095); put('r', 5.987); put('s', 6.327); put('t', 9.056); put('u', 2.758); put('v', 0.978); put('w', 2.360); put('x', 0.150); put('y', 1.974); put('z', 0.074); } }); Map sortedFreq = MapUtils.sortByValue(rate); BiMap<Double, Character> inverseRate = rate.inverse(); Map<Double, Character> sortedInverseRate = MapUtils.sortByValue(inverseRate); MapUtils.printMap(sortedInverseRate); System.out.println("================================================="); final String message = "nci3 y 2p7, h2p g2b 24pc m37xz njbs5bz 7pbz i4vscl d4cy43o3jbme " + "y mpyc4uc66p 6sc635p7xz d3426c5py, 5pri3 1bu5b n4puc6x.;"; final Map<Character, Double> messageRate = new HashMap<Character, Double>(); for (int i = 0; i < message.length(); i++) { Character character = message.charAt(i); if (lettersAndDigits.contains(character.toString())) { // only letters and digits if (messageRate.containsKey(character)) messageRate.put(character, messageRate.get(character) + 1); else messageRate.put(character, 1d); } } Double length = new Double(message.length()); for (Character key : messageRate.keySet()) { messageRate.put(key, messageRate.get(key) / length * 100); } System.out.println("==========Message Characters Rate, %=================="); MapUtils.printMap(messageRate); final Map<Character, Double> sortedMessageRate = MapUtils.sortByValue(messageRate); System.out.println("=======================Sorted==================="); MapUtils.printMap(sortedMessageRate); SwingUtilities.invokeLater( new Runnable() { @Override public void run() { JFrame jFrame = new JFrame(); jFrame.setLayout(new BorderLayout()); JTable jTable = new JTable(); final JTextArea jTextArea = new JTextArea(); jTextArea.append(message); jTextArea.setPreferredSize(new Dimension(600, 50)); jFrame.add(jTextArea, BorderLayout.NORTH); final JTextArea jTextArea2 = new JTextArea(); jTextArea2.setPreferredSize(new Dimension(600, 50)); jFrame.add(jTextArea2, BorderLayout.SOUTH); JButton jButton = new JButton("replace"); jButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jTextArea2.setText(""); jTextArea2.setText(replace()); } private String replace() { StringBuilder sb = new StringBuilder(); String src = jTextArea.getText(); for (int i = 0; i < src.length(); i++) { Character character = src.charAt(i); } return "new value"; } }); jFrame.add(jButton, BorderLayout.WEST); DefaultTableModel defaultTableModel = new DefaultTableModel(); jTable.setModel(defaultTableModel); defaultTableModel.setRowCount(messageRate.size()); defaultTableModel.setColumnCount(3); TableColumn column = jTable.getColumnModel().getColumn(2); Character[] values = new Character[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; column.setCellRenderer(new MyComboBoxCellRenderer(values)); column.setCellEditor(new MyComboBoxEditor(values)); jFrame.add(jTable, BorderLayout.EAST); int i = 0; for (Character character : sortedMessageRate.keySet()) { defaultTableModel.setValueAt(character.toString(), i, 0); defaultTableModel.setValueAt(sortedMessageRate.get(character).toString(), i, 1); JComboBox jComboBox = new JComboBox<Character>(new DefaultComboBoxModel<Character>()); defaultTableModel.setValueAt(jComboBox, i++, 2); } jFrame.setVisible(true); } }); Set<Double> usedSet = new HashSet<Double>(); StringBuilder newMessage = new StringBuilder(); for (int i = 0; i < message.length(); i++) { Character character = message.charAt(i); if (lettersAndDigits.contains(character.toString())) { Double charFreq = sortedMessageRate.get(character); Double minDelta = 100d; Character minValue; boolean moreThanOne = false; for (Double otherFreq : inverseRate.keySet()) { Double d = charFreq - otherFreq; if (usedSet.contains(otherFreq)) {} if (d < minDelta && !usedSet.contains(otherFreq)) { minDelta = d; minValue = inverseRate.get(minDelta); usedSet.add(otherFreq); } if (minDelta == d) { moreThanOne = true; return; } } if (!moreThanOne) { // newMessage.append() } } } }
private void renderCell( BandElement bandElement, String bandName, Object value, int gridRow, int sheetRow, int sheetColumn, int rowSpan, int colSpan, boolean image) { if (bandElement instanceof ReportBandElement) { colSpan = 1; } XSSFCellStyle cellStyle = buildBandElementStyle(bandElement, value, gridRow, sheetColumn, colSpan); // if we have a subreport on the current grid row we have to take care of the sheetColumn if (ReportLayout.HEADER_BAND_NAME.equals(bandName) && (gridRow == prevSubreportFirstRow) && (prevSubreportLastColumn != -1)) { sheetColumn = prevSubreportLastColumn - prevSubreportFirstColumn - 1 + sheetColumn; } XSSFCell c = xlsRow.createCell(sheetColumn); if (image) { if ((value == null) || "".equals(value)) { c.setCellType(XSSFCell.CELL_TYPE_STRING); c.setCellValue(wb.getCreationHelper().createRichTextString(IMAGE_NOT_FOUND)); } else { try { ImageBandElement ibe = (ImageBandElement) bandElement; byte[] imageBytes = getImage((String) value, ibe.getWidth(), ibe.getHeight()); XSSFClientAnchor anchor = new XSSFClientAnchor( 0, 0, 0, 0, (short) sheetColumn, sheetRow, (short) (sheetColumn + colSpan), (sheetRow + rowSpan)); int index = wb.addPicture(imageBytes, XSSFWorkbook.PICTURE_TYPE_JPEG); // image is created over the cells, so if it's height is bigger we set the row height short height = xlsRow.getHeight(); int realImageHeight = getRealImageSize((String) value)[1]; if (ibe.isScaled()) { realImageHeight = ibe.getHeight(); } short imageHeight = (short) (realImageHeight * POINTS_FOR_PIXEL / 2.5); boolean doResize = false; if (imageHeight > height) { xlsRow.setHeight(imageHeight); } else { doResize = true; } Picture picture = patriarch.createPicture(anchor, index); if (doResize) { picture.resize(); } anchor.setAnchorType(2); } catch (Exception ex) { c.setCellType(XSSFCell.CELL_TYPE_STRING); c.setCellValue(wb.getCreationHelper().createRichTextString(IMAGE_NOT_LOADED)); } } if (cellStyle != null) { c.setCellStyle(cellStyle); } } else { if (bandElement instanceof HyperlinkBandElement) { Hyperlink hyp = ((HyperlinkBandElement) bandElement).getHyperlink(); XSSFHyperlink link = wb.getCreationHelper().createHyperlink(XSSFHyperlink.LINK_URL); link.setAddress(hyp.getUrl()); c.setHyperlink(link); c.setCellValue(wb.getCreationHelper().createRichTextString(hyp.getText())); c.setCellType(XSSFCell.CELL_TYPE_STRING); } else if (bandElement instanceof ReportBandElement) { Report report = ((ReportBandElement) bandElement).getReport(); ExporterBean eb = null; try { eb = getSubreportExporterBean(report, true); XlsxExporter subExporter = new XlsxExporter(eb, cellStyle); subExporter.export(); XSSFSheet subreportSheet = subExporter.getSubreportSheet(); if (ReportLayout.HEADER_BAND_NAME.equals(bandName) && (gridRow == prevSubreportFirstRow)) { // other subreports on the same header line after the first sheetColumn = prevSubreportLastColumn; sheetRow -= addedPageRows; pageRow -= addedPageRows; addedPageRows = 0; } else { addedPageRows = subreportSheet.getLastRowNum(); pageRow += addedPageRows; // if subreport is not on the first column we merge all cells in the columns before, // between the rows subreport occupies if (sheetColumn > 0) { for (int i = 0; i <= sheetColumn - 1; i++) { CellRangeAddress cra = new CellRangeAddress(sheetRow, pageRow, i, i); regions.add(new XlsxRegion(cra, null)); } } } int cols = XlsxUtil.copyToSheet(xlsSheet, sheetRow, sheetColumn, subreportSheet); addRegions(xlsSheet, subExporter.getSubreportRegions(), wb); if (ReportLayout.HEADER_BAND_NAME.equals(bandName)) { prevSubreportFirstRow = gridRow; prevSubreportFirstColumn = sheetColumn; prevSubreportLastColumn = sheetColumn + cols; } } catch (Exception e) { e.printStackTrace(); } finally { if ((eb != null) && (eb.getResult() != null)) { eb.getResult().close(); } } } else if (bandElement instanceof ImageColumnBandElement) { try { ImageColumnBandElement icbe = (ImageColumnBandElement) bandElement; String v = StringUtil.getValueAsString(value, null); if (StringUtil.BLOB.equals(v)) { c.setCellType(XSSFCell.CELL_TYPE_STRING); c.setCellValue(wb.getCreationHelper().createRichTextString(StringUtil.BLOB)); } else { byte[] imageD = StringUtil.decodeImage(v); byte[] imageBytes = getImage(imageD, icbe.getWidth(), icbe.getHeight()); XSSFClientAnchor anchor = new XSSFClientAnchor( 0, 0, 0, 0, (short) sheetColumn, sheetRow, (short) (sheetColumn + colSpan), (sheetRow + rowSpan)); int index = wb.addPicture(imageBytes, XSSFWorkbook.PICTURE_TYPE_JPEG); // image is created over the cells, so if it's height is bigger we set the row height short height = xlsRow.getHeight(); int realImageHeight = getRealImageSize(imageBytes)[1]; if (icbe.isScaled()) { realImageHeight = icbe.getHeight(); } short imageHeight = (short) (realImageHeight * POINTS_FOR_PIXEL / 2.5); if (imageHeight > height) { xlsRow.setHeight(imageHeight); } Picture picture = patriarch.createPicture(anchor, index); picture.resize(); anchor.setAnchorType(2); } } catch (Exception e) { e.printStackTrace(); c.setCellType(XSSFCell.CELL_TYPE_STRING); c.setCellValue(wb.getCreationHelper().createRichTextString(IMAGE_NOT_LOADED)); } } else { if (value == null) { c.setCellType(XSSFCell.CELL_TYPE_STRING); c.setCellValue(wb.getCreationHelper().createRichTextString("")); } else if (value instanceof Number) { c.setCellType(XSSFCell.CELL_TYPE_NUMERIC); c.setCellValue(((Number) value).doubleValue()); } else { String pattern = null; if (bandElement instanceof FieldBandElement) { FieldBandElement fbe = (FieldBandElement) bandElement; pattern = fbe.getPattern(); } if ((value instanceof java.sql.Date) || (value instanceof java.sql.Timestamp)) { Date date; if (value instanceof java.sql.Date) { date = new Date(((java.sql.Date) value).getTime()); } else { date = (java.sql.Timestamp) value; } if (cellStyle != null) { if (pattern == null) { // use default pattern if none selected Locale locale = Locale.getDefault(); pattern = ((SimpleDateFormat) DateFormat.getDateInstance(SimpleDateFormat.MEDIUM, locale)) .toPattern(); } else { pattern = StringUtil.getI18nString(pattern, getReportLanguage()); } cellStyle.setDataFormat(wb.createDataFormat().getFormat(pattern)); } c.setCellValue(date); } else { c.setCellType(XSSFCell.CELL_TYPE_STRING); String text = StringUtil.getValueAsString(value, pattern); if ((bandElement != null) && bandElement.isWrapText()) { // try to interpret new line characters // \\n is used here to be possible to add in designer grid cell with \n if (text.contains("\\n") || text.contains("\n") || text.contains("\r") || text.contains("\r\n")) { String crLf = Character.toString((char) 13) + Character.toString((char) 10); int lines = countLines(text); if (text.contains("\r\n")) { text = text.replaceAll("\r\n", crLf); } else { text = text.replaceAll("(\n)|(\r)|(\\\\n)", crLf); } c.setCellValue(text); cellStyle.setWrapText(true); xlsRow.setHeightInPoints(lines * (cellStyle.getFont().getFontHeightInPoints() + 3)); } else { c.setCellValue(wb.getCreationHelper().createRichTextString(text)); } } else { c.setCellValue(wb.getCreationHelper().createRichTextString(text)); } } } } if (cellStyle != null) { if (bandElement != null) { cellStyle.setRotation(bandElement.getTextRotation()); } if (!(bandElement instanceof ReportBandElement)) { c.setCellStyle(cellStyle); } } if ((rowSpan > 1) || (colSpan > 1)) { CellRangeAddress cra = new CellRangeAddress( sheetRow, sheetRow + rowSpan - 1, sheetColumn, sheetColumn + colSpan - 1); Border beBorder = bandElement.getBorder(); if (hasRowRenderConditions(bandElement, gridRow, value)) { // for row render conditions we must keep the row border beBorder = border; } regions.add(new XlsxRegion(cra, beBorder)); } } }