代码已启动并正在运行。感谢MadProgrammer的所有帮助。他是一名球员。
基本上这个代码的主要内容是随机字符,随机大小和随机颜色。
下面是代码:
随机颜色:
int a = (int)(Math.random() * 255 - 0);
int b = (int)(Math.random() * 255 - 0);
int c = (int)(Math.random() * 255 - 0);
int z = (int)(Math.random() * 300 - 0);
Label.setForeground(new java.awt.Color(a, b, c));
随机尺寸:
Random rand = new Random();
int random = (int)(Math.random() * 100000 - 50000);
Label.setFont(new Font("Courier New", Font.ITALIC, z));
RANDOM TEXT:
char randomChar = (char)(int)((Math.random() * 93) + 33);
Label.setText(String.valueOf(randomChar));
真实OG的完整代码:
package my.dimensional;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Random;
import static javafx.scene.paint.Color.color;
import static javafx.scene.text.Font.font;
import static javafx.scene.text.Font.font;
import javax.swing.JLabel;
public class DimensionalUI extends javax.swing.JFrame
{
public DimensionalUI()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jLabel2 = new javax.swing.JLabel();
Run = new javax.swing.JButton();
Label = new javax.swing.JLabel();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jLabel2.setText("jLabel2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Run.setText("Run");
Run.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RunActionPerformed(evt);
}
});
Label.setText("BOOM");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Run, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(Label, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE)
.addComponent(Run)
.addContainerGap())
);
pack();
}// </editor-fold>
private void RunActionPerformed(java.awt.event.ActionEvent evt) {
Random rand = new Random();
int random = (int)(Math.random() * 100000 - 50000);
int a = (int)(Math.random() * 255 - 0);
int b = (int)(Math.random() * 255 - 0);
int c = (int)(Math.random() * 255 - 0);
int z = (int)(Math.random() * 300 - 0);
Label.setForeground(new java.awt.Color(a, b, c));
Label.setFont(new Font("Courier New", Font.ITALIC, z));
/*String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
int index = (int)(Math.random() * (fontNames.length - 1));
String fontName = fontNames[index];
Font font = new Font(fontName, Font.PLAIN, z);
Label.setFont(new Font(fontName, Font.PLAIN, z));*/
char randomChar = (char)(int)((Math.random() * 93) + 33);
Label.setText(String.valueOf(randomChar));
}
public static void main(String args[])
{
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DimensionalUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new DimensionalUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Label;
private javax.swing.JButton Run;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
private void getAvailableFontFamilyNames() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
答案 0 :(得分:0)
Color
只是int
- 0
范围内的3 255
个值...
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
Color color = new Color(red, green, blue);
可以通过建立预定义的字符数组来实现,在0
和数组length
之间生成随机数并在指定位置提取元素,就个人而言,这很多繁琐的工作。
您可以将文本值中的值加载到char
数组中,但同样,生成原始文件需要做很多工作。
更简单的过程可能是计算33
和126
之间的随机值,并将数字转换为char
,这将为您提供该值的ASCII字符。
char randomChar = (char)(int)((Math.random() * 93) + 33);
嗯,字体大小很简单。要获取可用字体名称列表,您需要调用GraphicsEnvironment#getAvailableFontFamilyNames()
,它将返回String
字体名称数组...
String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
然后,您只需在0
和数组length
...
int index = (int)(Math.random() * (fontNames.length - 1));
并提取所需的字体名称......
String fontName = fontNames[index];
然后你创建一个新字体......
Font font = new Font(fontName, Font.PLAIN, size);
答案 1 :(得分:0)
为了能够从所有字符中随机选择,您首先必须将字符放入字符串数组中:
String[] chars = {0,1,2,3,4,5,a,b,c /*all characters you want to be printable*/ ,y,z};
然后绘制它们:
Random randomGenerator = new Random();
int random = randomGenerator.nextInt(chars.length + 2); //randomly pic a number between 0 and the length of the array.
Output.setText(chars[random]); //might have to convert to string
此外,我建议您将randomGenerator设为字段而不是局部变量,因为每次执行代码时重新播种随机数生成器都是不必要的。