0

My GUI is supposed to show a stick figure and you are able to change its size and color. All my buttons are being displayed in my frame but the stick figure isn't. Please Help Below are is all my code

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.awt.Polygon;
import java.awt.BasicStroke;

public class StickFigure
{

    public StickFigure(int xIn, int yIn, int wIn, int hIn)
    {
        width = (int)(wIn*scale);
        height = (int)(hIn*scale);
        x = xIn;
        y = yIn;
        scale = 1;
        color = Color.BLACK;
     }

public void draw(Graphics2D g2)
{

    Ellipse2D.Double head = new Ellipse2D.Double((width/4)+x, y, width/2, width/2);

    Point2D bodyPoint1 = new Point2D.Double((int)((width/2)+x), (int)((height/5)+y));
    Point2D bodyPoint2 = new Point2D.Double((int)((width/2)+x), (int)(4*(height/5)+y));
    Line2D body = new Line2D.Double(bodyPoint1,bodyPoint2);

    Point2D leftArmPoint1 = new Point2D.Double((int)((width/2)+x), (int)(1.5*(height/5)+y));
    Point2D leftArmPoint2 = new Point2D.Double(x, (int)(2.5*(height/5)+y));
    Point2D leftArmPoint3 = new Point2D.Double((int)((width/2)+x), (int)(2.5*(height/5)+y));
    Line2D leftArmLine1 = new Line2D.Double(leftArmPoint1, leftArmPoint2);
    Line2D leftArmLine2 = new Line2D.Double(leftArmPoint2, leftArmPoint3);

    Point2D rightArmPoint1 = new Point2D.Double((int)((width/2)+x), (int)(1.5*(height/5)+y));
    Point2D rightArmPoint2 = new Point2D.Double(width + x, (int)(2.5*(height/5)+y));
    Point2D rightArmPoint3 = new Point2D.Double((int)((width/2)+x), (int)(2.5*(height/5)+y));
    Line2D rightArmLine1 = new Line2D.Double(rightArmPoint1, rightArmPoint2);
    Line2D rightArmLine2 = new Line2D.Double(rightArmPoint2, rightArmPoint3);

    Point2D leftLegPoint = new Point2D.Double(x, height + y);
    Point2D rightLegPoint = new Point2D.Double(width + x, height+y);
    Line2D leftLeg = new Line2D.Double(bodyPoint2, leftLegPoint);
    Line2D rightLeg = new Line2D.Double(bodyPoint2, rightLegPoint);

    g2.setStroke( new BasicStroke(5.0f));
    g2.setColor(color);
    g2.fill(head);
    g2.fill(body);
    g2.fill(leftArmLine1);
    g2.fill(leftArmLine2);
    g2.fill(rightArmLine1);
    g2.fill(rightArmLine2);
    g2.fill(leftLeg);
    g2.fill(rightLeg);
    g2.draw(head);
    g2.draw(body);
    g2.draw(leftArmLine1);
    g2.draw(leftArmLine2);
    g2.draw(rightArmLine1);
    g2.draw(rightArmLine2);
    g2.draw(leftLeg);
    g2.draw(rightLeg);

}

public void changeColorOfStickFigure(Color cIn)
{
    color = cIn;
}

public void changeSizeOfStickFigure(int sIn)
{
    scale = sIn;
}
private Color color;
private int width;
private int height;
private int scale;
private int x;
private int y;

}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Dimension;

public class StickFigureComponent extends JComponent
{
 public StickFigureComponent()
{
   stickFigure1 = new StickFigure(STICKFIGURE_X, STICKFIGURE_Y, STICKFIGURE_WIDTH, STICKFIGURE_HEIGHT)
   setPreferredSize(new Dimension(STICKFIGURE_WIDTH, STICKFIGURE_HEIGHT));
}

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    stickFigure1.draw(g2);
}

public void changeColor(Color cIn)
{
   stickFigure1.changeColorOfStickFigure(cIn);
   repaint();
}

public void changeSize(int sIn)
{
   stickFigure1.changeSizeOfStickFigure(sIn);
   repaint();
}

private StickFigure stickFigure1;
private static final int STICKFIGURE_X = 200;
private static final int STICKFIGURE_Y = 0;
private static final int STICKFIGURE_WIDTH = 100;
private static final int STICKFIGURE_HEIGHT = 100;

}

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JColorChooser;
import javax.swing.ButtonGroup;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class StickFigureComponentViewer 
{

    public static void main(String[] args)
    {
    JFrame frame = new JFrame();
    final StickFigureComponent stickFigure1 = new StickFigureComponent();
    class ColorChooserListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
        {
            color = JColorChooser.showDialog(null, "PICK A COLOR", color);
            if(color == null)
            {
                color = color.black;
            }
            stickFigure1.changeColor(color);
        }
    }

    JRadioButton radioButton1 = new JRadioButton("1x", true);
    radioButton1.setActionCommand("1x");
    JRadioButton radioButton2 = new JRadioButton("2x", false);
    radioButton2.setActionCommand("2x");
    JRadioButton radioButton3 = new JRadioButton("3x", false);
    radioButton3.setActionCommand("3x");
    JRadioButton radioButton4 = new JRadioButton("4x", false);
    radioButton4.setActionCommand("4x");
    final ButtonGroup sizeGroup = new ButtonGroup();
    sizeGroup.add(radioButton1);
    sizeGroup.add(radioButton2);
    sizeGroup.add(radioButton3);
    sizeGroup.add(radioButton4);

    class RadioListener implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
        {
            if(sizeGroup.getSelection().getActionCommand() == "2x")
            {
                stickFigure1.changeSize(2);
            }
            else if(sizeGroup.getSelection().getActionCommand() == "3x")
            {
                stickFigure1.changeSize(3);
            }

            else if(sizeGroup.getSelection().getActionCommand() == "4x")
            {
                stickFigure1.changeSize(4);
            }
            else
            {
                stickFigure1.changeSize(1);
            }
        }
    }

    JButton pickColorButton = new JButton("Pick Color");
    pickColorButton.setBackground(Color.CYAN);
    ActionListener colorChooserListener = new ColorChooserListener();
    pickColorButton.addActionListener(colorChooserListener);

    ActionListener radioListener = new RadioListener();
    radioButton1.addActionListener(radioListener);
    radioButton2.addActionListener(radioListener);
    radioButton3.addActionListener(radioListener);
    radioButton4.addActionListener(radioListener);

    final JLabel pickSize = new JLabel("Pick Size");

    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    p2.setLayout(new BorderLayout());
    JPanel p3 = new JPanel();
    p3.setLayout(new BorderLayout());
    p3.setPreferredSize(new Dimension(500,500));
    p1.setOpaque(false);
    p2.setOpaque(false);
    p3.setOpaque(false);

    p1.add(pickSize);
    p1.add(radioButton1);
    p1.add(radioButton2);
    p1.add(radioButton3);
    p1.add(radioButton4);
    p2.add(pickColorButton,BorderLayout.LINE_START);
    p3.add(stickFigure1);

    frame.add(p1,BorderLayout.PAGE_END);
    frame.add(p2,BorderLayout.LINE_START);
    frame.add(p3,BorderLayout.LINE_END);
    frame.setTitle("Stick Figure Customizer");
    frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
 }
private static final int FRAME_WIDTH = 1000;
private static final int FRAME_HEIGHT = 500;
private static Color color;
}
  • 1
    Your `StickFigureComponent` has no preferred size, so the layout manager is likely not applying a size to it... – MadProgrammer Dec 03 '14 at 00:59
  • Please see [Writing a Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Few people want to read through all your code to find the problem, so shortening it will make people more likely to answer your question. – James Westman Dec 03 '14 at 01:06
  • I gave it a preferred size in its constructor – BlessedTurtle Dec 03 '14 at 01:06
  • In that case, see [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Dec 03 '14 at 01:08
  • `sizeGroup.getSelection().getActionCommand() == "2x"` is not how `String` comparision works in Java – MadProgrammer Dec 03 '14 at 01:11

1 Answers1

0

Your StickFigureComponent doesn't have any discernible size of the layout manager to make decisions about how big it should make it (in fact, it's default size is actually 0x0), so the layout manager is simply making 0x0.

Override the the getPreferredSize method and return an appropriate size to allow the layout manager to make better decisions about what to do with your component...

public class StickFigureComponent extends JComponent
    //...
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(STICKFIGURE_X + STICKFIGURE_WIDTH, 
                             STICKFIGURE_Y + STICKFIGURE_HEIGHT);
    }

Updated

Take a closer look at your StickFigure constructor...

public StickFigure(int xIn, int yIn, int wIn, int hIn) {
    width = (int) (wIn * scale);
    height = (int) (hIn * scale);
    x = xIn;
    y = yIn;
    scale = 1;
    color = Color.BLACK;
}

The width and height values are been set to 0 because scale is default to 0 when it is initialised...

Instead, initialise scale before you apply it, for example...

public StickFigure(int xIn, int yIn, int wIn, int hIn) {
    scale = 1;
    width = (int) (wIn * scale);
    height = (int) (hIn * scale);
    x = xIn;
    y = yIn;
    color = Color.BLACK;
}

Also...

sizeGroup.getSelection().getActionCommand() == "2x"

Is not how String comparison works in Java, instead, you should be using something like...

"2x".equals(sizeGroup.getSelection().getActionCommand())
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366