截至目前,我有一个程序可以随机生成一条线,该线沿着所有可能的方向行进,当它到达圆形或正方形的边缘时终止。
我是否可以使用不对角线的随机线?所以它本质上看起来像一个蛇游戏,而不是随机涂鸦?
我会粘贴整个代码,以便您了解最新情况。
import java.util.Scanner;
import java.util.Random;
import java.awt.*;
public class ABP {
private static final Scanner CONSOLE = new Scanner(System.in);
public static void main(String args[]) {
System.out.println("Lab 7 written by Devin Tuchsen");
int rad = promptIntWithRange("Please enter the circle's radius (50-400 pixels): ", 50, 400);
boolean cont = true;
Color color = null;
while(cont) {
String colorStr = promptString("Please enter the color of the circle (green [g] or blue [b]): ");
if(matchesChoice(colorStr, "green")) {
color = Color.GREEN;
cont = false;
}
else if(matchesChoice(colorStr, "blue")) {
color = Color.BLUE;
cont = false;
}
else {
System.err.println("ERROR: \"" + colorStr +
"\" is an invalid choice. Please enter \"green\", \"blue\", \"g\", or \"b\" (Not case-sensitive).");
}
}
DrawingPanel panel = new DrawingPanel(rad * 2 + 32, rad * 2 + 32);
Graphics g = panel.getGraphics();
g.setColor(color);
g.drawRect(16,16,rad * 2,rad * 2);
g.setColor(Color.BLUE);
int x = 16 + rad;
int y = 16 + rad;
g.drawLine(x,y,x,y);
Random rand = new Random();
int dir = 0;
int cycles = 0;
while(pointInsideCircle(x,y,rad+16,rad+16,rad)) {
dir = rand.nextInt(4);
switch(dir) {
case 0:
g.drawLine(x,--y,x,y);
break;
case 1:
g.drawLine(++x,y,x,y);
break;
case 2:
g.drawLine(x,++y,x,y);
break;
case 3:
g.drawLine(--x,y,x,y);
break;
}
cycles++;
panel.sleep(1);
}
System.out.println("After " + cycles + " steps, the walk is finished.");
}
private static String promptString(String prompt) {
System.out.print(prompt);
String str = CONSOLE.nextLine();
if(str.length() == 0)
str = CONSOLE.nextLine();
return str;
}
private static int promptIntWithRange(String prompt, int min, int max) {
int n = 0;
boolean cont = true;
while(cont) {
System.out.print(prompt);
if(CONSOLE.hasNextInt()) {
n = CONSOLE.nextInt();
if(n >= min && n <= max)
cont = false;
else
//Handle errors where the user enters an integer outside the range
System.err.println("ERROR: " + n + " is not between " + min + " and " + max + ".");
}
else {
//Handle errors where the user enters something besides an int
String str = CONSOLE.nextLine();
//This check avoids posting an error with an empty string (leftover data in RAM)
if(str.length() == 0)
str = CONSOLE.nextLine();
if (str.length() >= 22 && str.toLowerCase().substring(0,22).equals("open the pod bay doors"))
System.err.println("I'm sorry, Dave. I'm afraid I can't do that.");
else
System.err.println("ERROR: \"" + str + "\" is not an integer value.");
}
}
return n;
}
private static boolean matchesChoice(String str1, String str2) {
str1 = str1.toLowerCase().trim();
str2 = str2.toLowerCase().trim();
if(str1.length() > 1)
return str1.equals(str2);
else if(str1.length() == 1)
return str1.charAt(0) == str2.charAt(0);
else
return false;
}
private static boolean pointInsideCircle(int x, int y, int cx, int cy, int r) {
return Math.pow(x-cx,2) + Math.pow(y-cy,2) < Math.pow(r,2);
}
}
画班
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingPanel implements ActionListener {
private static final int DELAY = 100; // delay between repaints in millis
private static final boolean PRETTY = false; // true to anti-alias
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
statusBar = new JLabel(" ");
statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(width, height));
panel.add(new JLabel(new ImageIcon(image)));
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
statusBar.setText("(" + e.getX() + ", " + e.getY() + ")");
}
public void mouseExited(MouseEvent e) {
statusBar.setText(" ");
}
};
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);
g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.BLACK);
if (PRETTY) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.1f));
}
frame = new JFrame("Drawing Panel");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.getContentPane().add(statusBar, "South");
frame.pack();
frame.setVisible(true);
toFront();
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
panel.repaint();
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// makes drawing panel become the frontmost window on the screen
public void toFront() {
frame.toFront();
}
}
图像:
答案 0 :(得分:-1)
我会给你一些想法。我希望,我清楚地了解你;
Square在每条边上都有X个单位长度。所以你可以用这个标准来限制。并且您可以在思考圆半径R的同时创建随机直线(不是对角线),在此步骤中您可以使用向量,对于square(1,1)(1,2)(1,3)第一个x向量点是对角线必须相同(与y相反)。如果行长于或等于正方形X或圆的R,则可以终止步骤。