我的GUI不显示我创建的对象

时间:2014-12-03 00:53:23

标签: java user-interface

我的GUI应该显示一个简笔画,你可以改变它的大小和颜色。我的所有按钮都显示在我的框架中,但棒图不是。请帮忙 以下是我的所有代码

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 个答案:

答案 0 :(得分:0)

您的StickFigureComponent没有任何可辨别的布局管理器大小来决定它应该有多大(事实上,它的默认大小实际上是0x0 ),所以布局管理员只是制作0x0

覆盖getPreferredSize方法并返回适当的大小,以便布局管理员更好地决定如何处理您的组件......

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

<强>更新

仔细查看您的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;
}

widthheight值已设置为0,因为scale在初始化时默认为0 ...

相反,在应用之前初始化scale,例如......

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;
}

也...

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

不是String比较在Java中是如何工作的,相反,你应该使用类似......

的东西
"2x".equals(sizeGroup.getSelection().getActionCommand())