Hi,

I'm having trouble with using and updating graphics in a JFrame. I'll include my code below along with some classes. They're necessary for the program to work properly.

The issue is that the paint method is painting over my UI. Then it won't display the graphics. I don't have much experience yet with the Graphics object and the Graphics package chapter is afteer the OOP chapter, in which the author asks us to use the Graphics object. Nice.

Please advise. Using Eclipse IDE btw.

// Exercise_9_24 MAIN PROGRAM HERE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Exercise_9_24 extends JFrame
{
    private Shape arrayOfShapes[] = new Shape[20];
    private int numberOfShapes;
    private JLabel xPosLabel, yPosLabel, xSizeLabel, ySizeLabel, shapeNumberLabel, fillColorRedLabel,
            fillColorGreenLabel, fillColorBlueLabel;
    private JTextField xPosField, yPosField, xSizeField, ySizeField, shapeNumberField, fillColorRedField,
            fillColorGreenField, fillColorBlueField;
    private int xPos, yPos, xSize, ySize, shapeNumber, fillColorRedNumber, fillColorGreenNumber, 
                fillColorBlueNumber;
    private JButton submitButton;

    public Exercise_9_24()
    {
        super("Graphics, Shapes, and Exercise 9.24");

        Container c = getContentPane();

        c.setLayout(new FlowLayout());

        shapeNumberLabel = new JLabel("Enter 1 for Square, 2 for Circle, 3 for Rectangle, and 4 for a Triangle");
        shapeNumberField = new JTextField(10);
        xPosLabel = new JLabel("X Position");
        xPosField = new JTextField(10);
        yPosLabel = new JLabel("Y Position");
        yPosField = new JTextField(10);
        c.add(shapeNumberLabel);
        c.add(shapeNumberField);
        c.add(xPosLabel);
        c.add(xPosField);
        c.add(yPosLabel);
        c.add(yPosField);
        xSizeLabel = new JLabel("X Size");
        ySizeLabel = new JLabel("Y Size");
        xSizeField = new JTextField(10);
        ySizeField = new JTextField(10);
        c.add(xSizeLabel);
        c.add(xSizeField);
        c.add(ySizeLabel);
        c.add(ySizeField);
        fillColorRedLabel = new JLabel("Red");
        fillColorBlueLabel = new JLabel("Blue");
        fillColorGreenLabel = new JLabel("Green");
        fillColorRedField = new JTextField(10);
        fillColorBlueField = new JTextField(10);
        fillColorGreenField = new JTextField(10);
        c.add(fillColorRedLabel);
        c.add(fillColorRedField);
        c.add(fillColorGreenLabel);
        c.add(fillColorGreenField);
        c.add(fillColorBlueLabel);
        c.add(fillColorBlueField);

        //Graphics g = new Graphics();

        submitButton = new JButton("Submit Results");
        submitButton.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        getInputs();
                        clearFields();
                        createShape();
                        numberOfShapes++;
                        repaint();                      
                        clearVariables();
                    }
                }
            );

        c.add(submitButton);
    }

    public void paint(Graphics g)
    {
        for (int i = 0; i < numberOfShapes; i++)
        {
            System.out.println("Drawing: " + numberOfShapes);
            arrayOfShapes[i].draw(g);
        }
    }



    private void getInputs()
    {
        xPos = Integer.parseInt(xPosField.getText());
        yPos = Integer.parseInt(yPosField.getText());
        xSize = Integer.parseInt(xSizeField.getText());
        ySize = Integer.parseInt(ySizeField.getText());
        fillColorRedNumber = Integer.parseInt(fillColorRedField.getText());
        fillColorGreenNumber = Integer.parseInt(fillColorGreenField.getText());
        fillColorBlueNumber = Integer.parseInt(fillColorBlueField.getText());
        shapeNumber = Integer.parseInt(shapeNumberField.getText());
    }

    private void clearFields()
    {
        xPosField.setText("");
        yPosField.setText("");
        xSizeField.setText("");
        ySizeField.setText("");
        fillColorRedField.setText("");
        fillColorBlueField.setText("");
        fillColorGreenField.setText("");
        shapeNumberField.setText("");
    }

    public void createShape()
    {


        switch(shapeNumber)
        {

        case 1:
            arrayOfShapes[numberOfShapes] = new Square(xPos, yPos, xSize, ySize, fillColorRedNumber, 
                    fillColorGreenNumber, fillColorBlueNumber);
            break;

        case 2:
            arrayOfShapes[numberOfShapes] = new Circle(xPos, yPos, xSize, ySize, fillColorRedNumber,
                    fillColorGreenNumber, fillColorBlueNumber);
            break;

        case 3:
            arrayOfShapes[numberOfShapes] = new Rectangle(xPos, yPos, xSize, ySize, fillColorRedNumber,
                    fillColorGreenNumber, fillColorBlueNumber);
            break;

        case 4:
            arrayOfShapes[numberOfShapes] = new Triangle(xPos, yPos, xSize, ySize, fillColorRedNumber,
                    fillColorGreenNumber, fillColorBlueNumber);
            break;
        }
    }



    private void clearVariables()
    {
        xPos = yPos = xSize = ySize = fillColorRedNumber = fillColorGreenNumber = fillColorBlueNumber = 0;
    }

    public static void main (String args[])
    {
        Exercise_9_24 window = new Exercise_9_24();

        window.addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing (WindowEvent e)
                    {
                        System.exit(0);
                    }
                }
            );

        window.setSize(400, 120);
        window.show();
    }

}

// CIRCLE CLASS BEGINS HERE:  FILENAME(OBVIOUSLY): //CIRCLE.JAVA

import java.awt.Graphics;
import java.awt.Color;

public class Circle extends Shape
{
    protected int radius;

    public Circle()
    {
        setRadius(0,0);
    }

    public Circle(int x, int y, int sX, int sY, int r, int g, int b)
    {
        setX(x);
        setY(y);
        setRadius(sX, sY);
        setColor(r, g, b);
    }

    public void setRadius(int a, int b)
    {
        radius = (a <= b)? a: b;
        radius = (radius >= 0)? radius : 0;
    }

    public int getRadius()
    {
        return radius;
    }

    public void draw(Graphics g)
    {
        g.setColor(new Color(this.getRedFactor(), this.getGreenFactor(), this.getBlueFactor()));
        g.fillOval(this.getX(), this.getY(), this.getRadius(), this.getRadius());
    }

    public String getName()
    {
        return "Circle: " + " X: " + this.getX() + " Y: " + this.getY() + " Radius: " + this.getRadius();

    }
}

//RECTANGLE CLASS FILENAME: RECTANGLE.JAVA
import java.awt.Graphics;
import java.awt.Color;

public class Rectangle extends Shape
{
    protected int length, width;

    public Rectangle()
    {
        setSize(0,0);
    }

    public Rectangle(int x, int y, int sX, int sY, int r, int g, int b)
    {
        setX(x);
        setY(y);
        setColor(r, g, b);
        setSize(sX, sY);
    }

    public void setSize(int sX, int sY)
    {
        length = ( sX >= 0)? sX:0;
        width = ( sY >= 0)? sY:0;
    }

    public int getLength()
    {
        return length;
    }

    public int getWidth()
    {
        return width;
    }

    public void draw (Graphics g)
    {
        g.setColor(new Color(this.getRedFactor(), this.getGreenFactor(), this.getBlueFactor()));
        g.fillRect(this.getX(), this.getY(), this.getLength(), this.getWidth());    
    }

    public String getName()
    {
        return "Rectangle: " + "X: " + this.getX() + "Y: " 
                + this.getY() + "length: " + this.getLength() + "height: " + this.getWidth();
    }
}

// CLASS SQUARE FILENAME: SQUARE.JAVA
import java.awt.Color;
import java.awt.Graphics;

public class Square extends Shape
{
    protected int size;

    public Square()
    {
        setSide(0);

    }

    public Square (int x, int y, int sX, int sY, int r, int g, int b)
    {
        setX(x);
        setY(y);
        setColor(r,g,b);
        setSize(sX, sY);
    }

    public void setSize(int s1, int s2)
    {
        size = ( s1 <= s2 ? s1 : s2 );
    }

    public void setSide(int a)
    {
        size = ( a>=0 ? a: 0);
    }

    public int getSize()
    {
        return size;
    }

    public void draw (Graphics g)
    {
        g.setColor(new Color(this.getRedFactor(), this.getBlueFactor(), this.getGreenFactor()));
        g.fillRect(this.getX(), this.getY(), this.getSize(), this.getSize());
    }

    public String getName()
    {
        return "Square: " + "X: " + this.getX() + "Y: " + this.getY() + "Size: " + this.getSize();
    }

}

// ABSTRACT SUPERCLASS SHAPE.JAVA FILENAME:SHAPE.JAVA
import java.awt.Graphics;

public abstract class Shape
{
    protected int x, y, redFactor, greenFactor, blueFactor, sizeX, sizeY;

    public Shape()
    {
        setX(0);
        setY(0);
        setSize(0,0);
        setColor(0,0,0);
    }

    public Shape (int x, int y, int sX, int sY, int r, int g, int b)
    {
        setX(x);
        setY(y);
        setColor(r, g, b);
        setSize(sX, sY);
    }

    public abstract void draw(Graphics g);

    public void setX(int a)
    {
        x = ( a >= 0? a :0);
    }

    public void setY(int a)
    {
        y = (a >= 0 ? a : 0);
    }

    public void setSize (int sX, int sY)
    {
        sizeX = ( sX >= 0 ? sX : 0);
        sizeY = ( sY >= 0 ? sY : 0);
    }

    public void setColor (int a, int b, int c)
    {
        redFactor = (( a>=0 && a<=255)? a : 0);
        greenFactor = ((b>=0 && b<=255)? b : 0);
        blueFactor = ((c>=0 && c<=255)? c : 0);
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public int getSizeX()
    {
        return sizeX;
    }

    public int getSizeY()
    {
        return sizeY;
    }

    public int getRedFactor()
    {
        return redFactor;
    }

    public int getBlueFactor()
    {
        return blueFactor;
    }

    public int getGreenFactor()
    {
        return greenFactor;
    }

    public abstract String getName();
}

Recommended Answers

All 3 Replies

Please use code tags and formatting:

[code=JAVA] // paste code here

[/code]

That's a lot of code. Can you point us to a particular spot in the code to focus on where you think the problem is?

Sure. I think the problem is in the first code segment in Exercise_9_24.java

in this segment:

public void paint(Graphics g)
{
for (int i = 0; i < numberOfShapes; i++)
{
System.out.println("Drawing: " + numberOfShapes);
arrayOfShapes.draw(g);
}
}

or has something to do with the linkages with the Class files in the corresponding draw sections. But I really think the underlying problem has to do with my lack of knowledge about drawing using the Graphics object with respect to a JFrame. But here is the section of the class file where the draw() method resides. Note the draw() method in all the class files are very similar (so I won't repeat them).


public void draw (Graphics g)
{
g.setColor(new Color(this.getRedFactor(), this.getBlueFactor(), this.getGreenFactor()));
g.fillRect(this.getX(), this.getY(), this.getSize(), this.getSize());
}

Thanks. Any help would be greatly appreciated.

The problem is that you have overridden the complete painting behavior of the JFrame (and the components within it) by replacing its paint() method. At the very least, you would want to place a call to super.paint(g); before your own code.

A better solution would be to add a small class that extends JPanel and overrides the paintComponent(Graphics g) method to serve as a canvas for your shapes to be drawn on. You would then just add that panel in as another component in your JFrame.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.