Okay, I feel like I busted something in my head because I can't figure out anything in Java anymore! I just confused the crap out of myself, and I can't seem to get back on the horse. What I'm trying to do is read from a text file to create a picture. Each picture has shapes, and it makes them dance. I'm using the DrawingPanel class to help create the circle. My driver class reads commands to create the pictures from the text document; I had my shape working just fine, but I messed with it and confused myself all day. Now I can't undo all the different changes I've made : ( So yes, the driver reads info and will put into an arraylist I will make. Circle is the shape I am trying out, and it seems easy enough, yet I'm getting thrown somehow. Below is the class that confirms my circle flipping works, then my circle class, then my class I created to work the file. I'm not necessarily looking for you guys to come in and save the day with all the code; I'm certainly fine with ideas that point me in the right direction. Thank you for the help; I really do appreciate it.

import java.io.*;
import java.util.*;
import java.awt.*;
import learngraphics.Circle;
import learngraphics.DrawingPanel;
import learngraphics.Hwk1Useful;

public class TCircle {

public static void main ( String[] args) 
        //Create the panel
        DrawingPanel panel = new DrawingPanel(600, 600);
        Graphics g = panel.getGraphics();

        //set the color
        //using the circle class, creating a new circle object
        Circle c1 = new Circle("circ", 10, 10, 30); //small and upper left
        Circle c2 = new Circle("circ", 10, 200, 100); //bigger and lower left
        Circle c3 = new Circle("circ", 300, 10, 150); //even bigger and upper right
        //using the draw method in Circle class, passing in coords of 0,0
        c1.draw(g, 0, 0);
        c2.draw(g, 0, 0);
        c3.draw(g, 0, 0);
        //same circles drawn down and to the right of their former location
        c1.draw(g, 100, 200);
        c2.draw(g, 100, 200);
        c3.draw(g, 100, 200);

        c1.dance(g, 0, 0, 600, 600); //make little circle dance
        c2.dance(g, 0, 0, 600, 600); //make bigger circle dance
        c3.dance(g, 100, 100, 600, 600); //make medium circle dance,
            //starting from different location

        //see if dancing works on smaller window
        DrawingPanel panel2 = new DrawingPanel(100, 200);
        Graphics g2 = panel2.getGraphics();
        c1.dance(g2, 10, 10, 100, 200);
        c1.draw(g2, 10, 10);

        public static void delay(int milliseconds) {
         // Calling this routine causes a delay of the specified number
         // of milliseconds in the program that calls the routine.  It is
         // provided here as a convenience.
      if (milliseconds > 0) {
        try { Thread.sleep(milliseconds); }
        catch (InterruptedException e) { }

Then here is my circle class that extends Shape. I plan on adding different shapes as well.

//Import libraries
import java.io.*;
import java.util.*;
import java.awt.*;

public class Circle extends Shape

    //Declare variables
    static int x = 0;
    static int y = 0;
    static int scalex = 0;
    static int scaley = 0;
    public int size;
    int circleX = 0;

   public Circle(String name, int size, int inx, int iny) {
       super(name, inx, iny);
       this.size = size;
    //Method drawOval draws the oval
    //Draws the outline of an oval. The result is a circle or ellipse that fits within the 
         //rectangle specified by the x, y, width, and height arguments. If/else if statement(s) 
          //adjust location
    public void drawOval(String name, Graphics g, int size, int inx, int iny)

            /*If statement that uses the horizontal variable and equals(String s) 
            if (horizontal.equals("left"))
                        y = 0;

            //If statement that uses the verticle variable and equals(String s)
            if (verticle.equals("top"))
                        x = size;

                else if (verticle.equals("bottom"))
                            x = size/2;
            scalex = size/2;
            scaley = size/4;    

            //Draw the oval 
            g.drawOval(x, y, inx, iny);   

    public void dance(Graphics g, int inX, int inY, int maxX, int maxY){

            g.drawOval(circleX, y, inY, inY);
            circleX = circleX+1;
        //first two location and second two are w and h
        public void draw(Graphics g, int inX, int inY){

                g.drawOval(0, 0, inX, inY);


import java.awt.*;
import java.io.*;

public abstract class Shape {
    private String myname;
    private int x, y;
    public Shape() {
        myname = "Shape";
        x = y = 0;

    public Shape(String newname, int inx, int iny) {
        if (newname.length() > 0) {
            myname = newname;
        else { myname = "Shape"; }
        if (inx >= 0) { 
            x = inx; 
        } else { x = 0; }
        if (iny >= 0) { 
            y = iny;
        } else { x = 0; }

    public String getName() {
        return myname;
    public int getX() {
        return x;
    public int getY() {
        return y;
    protected void setX(int in) {
        if (in >= 0) {
            x = in;
    protected void setY(int in) {
        if (in >= 0) {
            y = in;
    /* Using the given Graphics variable, draws the current
     * Shape at the current location of this Shape (x, y)
     * offset by the given parameters (inX, inY).  In other words,
     * the Shape will be drawn at (x+inX, y+inY).         */
    public abstract void draw(Graphics g, int inX, int inY);

    /* Using the given Graphics variable, draws the current
     * Shape repeatedly in some pattern.  The pattern should
     * either start at, or be centered around, the current 
     * location of this Shape (x, y) offset by the given 
     * parameters (inX, inY).  In other words, the "dance" will 
     * start at or center around (x+inX, y+inY).
     * maxX: the maximum width of the window to draw in
     * maxY: the maximum height of the window to draw in */
    public abstract void dance(Graphics g, int inX, int inY, int maxX, int maxY);

    public String toString() {
        return myname + ": x:" + x + ", y:" + y;
} // End of Shape class

My readfile class I was using for the scanner.

import java.io.File;
import java.util.Scanner;

public class readfile {
        private Scanner x;
        //Method that opens file that reads in shape specifics and also handles error
        public void openFile()
                x = new Scanner(new File("myHwk1InputTestFile.txt"));
            catch (Exception e)
                System.out.println("Couldn't find the file.");

        public void readFile()

            Scanner fileScanner = new Scanner("TestFile.txt");
            while (fileScanner.hasNext()){
      //  String start = x.nextLine();
      //  System.out.println(start);
        /*    while(x.hasNext())

                String start = x.next();
                String picture = x.next();
                String pictureLetter = x.next();
                /*String shape = x.next();
                String shapex = x.next();
                String shapey = x.next();
                String shapea = x.next();
                String end = x.next();
                String pictureitem = x.next();
                String draw = x.next();
                String pictureitem2 = x.next();
                String pictureLetter2 = x.next();
                String pictureColor = x.next();
                String pictureSize1 = x.next();
                String pictureSize2 = x.next();

                System.out.printf("%s %s %s\n", start, picture, pictureLetter);

        public void closeFile()

Edited by Hanyouslayer: Additional detail

3 Years
Discussion Span
Last Post by JamesCherrill


  • this code is about old and refused practicies

  • never use Thread.sleep(int) in Java

  • never use Graphics g2 = panel2.getGraphics(); this method is proper for printing to printer or saving to BufferedImage

  • search here JPanel + paintComponent, delayed by Swing Timer


mKorbel, as usual, is right. You may think that code is OK, but it's going to fail.

Calling getGraphics and just drawing there hasn't worked since Java 1.5 made double-buffering the default. It may work (especially the very first time), but will fail totally later, eg when the window is resized. The only safe approach is to override paintComponent for your panel.

Thread.sleep sooner or later causes problems in a Swing environment, typically because it gets executed on the Swing event dispatch thread and blocks the whole GUI. Use a javax.swing.Timer for GUI animation

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.