I'm trying to use a Scanner to draw a rectangle in JFrame, but get these errors:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at DrawerAutoRect.main(DrawerAutoRect.java:39)
My goal of this program is to choose the type of object to draw i.e.: lines, rectangles,o vals, and then input the parameters, i.e. if it's a rectangle I'm drawing, the input would be r, 200,200,400,400
and for it to draw a rectangle with those dimensions on the JFrame. Then I'd just type "end" and it'd end waiting for objects to be input and drawn.
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Scanner;
import javax.swing.JFrame;
public class DrawerAutoRect extends JFrame {
public DrawerAutoRect(){
setSize(1020,1020);
}
public static void paint(Graphics g, int a, int b,int c, int d)
{
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(a, b, c, d);
}
public static void main(String[] args) {
int x1 = 100;
int x2 = 100;
int y1 = 50;
int y2 = 50;
Scanner s = new Scanner(System.in);
String dim = s.next();
while(dim!="end"){
if(dim.equals("r")){
x1 = s.nextInt();
y1 = s.nextInt();
x2 = s.nextInt();
y2 = s.nextInt();
}
}
paint(null, x1, y1, x2, y2);
DrawerAutoRect r = new DrawerAutoRect();
r.setDefaultCloseOperation(EXIT_ON_CLOSE);
r.setVisible(true);
//r.pack();
r.setTitle("Tutorial");
}