在&之间蹦蹦跳跳重新定义窗户

时间:2013-11-24 21:58:47

标签: java swing

经过两个星期的努力想要自己解决这个问题,我有点想到这一点。

假设是为使用两个窗口的Java类编写程序:

  • 第一个窗口显示面部图像(使用绘图基元生成)

  • 第二个窗口显示控件以修改面部的不同组件(眼睛等)

画脸,检查。 挑选脸部组件,检查。 获取控件以修改组件,请检查。

我遇到的问题是让第二个窗口根据拾取的组件更改控件(通过鼠标点击图像)。

每次选择不同的组件时,是否需要完全重新定义第二个窗口,或者是否只需更改按钮&滑块每次都没有重做第二个窗口?

DrawPanel.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{   
    public Color[] faceColors = { Color.GREEN, Color.YELLOW, Color.MAGENTA };
    public int target = 0;
    public String targetName = "";
    private int xPos = 350, yPos = 350;
    private int xDim = 500, yDim = 500;
    private Color faceColor = Color.GREEN;
    int eW, eH, eY, eX1, eX2;

    private int eyeX = 100, eyeY = 150;
    private int eyeSize = 100;
    private Color eyeColor = Color.BLUE;

    private int nose = 1, noseW = 100, noseH = 150, noseY = 75;
    private int mouth = 1, mouthW = 150, mouthH = 30, mouthY = 100;

    private MyShape[] shapes; // array containing all the shapes

    private JLabel statusLabel; // label displaying mouse coordinates

    private ControlFrame inspect;

    // constructor
    public DrawPanel( JLabel status )
    {
        shapes = new MyShape[ 50 ]; // create the array
        setBackground( Color.WHITE ); // set a white background

        // add the mouse listeners
        MouseHandler mouseHandler = new MouseHandler();
        addMouseListener( mouseHandler );
        addMouseMotionListener( mouseHandler );

        // set the status label for displaying mouse coordinates
        statusLabel = status;

        //  Load the default image
        initDrawing();


        inspect = new ControlFrame();
        inspect.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        inspect.setLocation( 764, 32 );
        inspect.setSize( 200, 700 );
        inspect.setResizable( false );
        inspect.setVisible( true );

    } // end DrawPanel constructor

    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );

        for ( int i = 0; i < 50; i++ )
        {
            if ( shapes[i] != null )
            {
                shapes[ i ].draw( g );
            }
        }
    } // end method paintComponent

    public void initDrawing()
    {
        defineFace( faceColor );
        defineEyes( eyeColor );
        defineMouth( mouth, mouthW, faceColor );
        defineNose( nose, noseY );
    }

    public void defineFace ( Color faceCol )
    {   //  Create the face & ears
        eW = (int)Math.round( xDim / 4 );
        eH = (int)Math.round( yDim / 4 );
        eY = (int)Math.round( yPos - ( yDim / 2 ) );
        eX1 = (int)Math.round( xPos - ( xDim / 2 ) );
        eX2 = (int)Math.round( xPos - eW + ( xDim / 2 ) );

        shapes[0] = new MyOval( eX1, eY, eX1+eW, eY+eH, faceCol, true );
        shapes[1] = new MyOval( eX1, eY, eX1+eW, eY+eH, Color.BLACK, false );
        shapes[2] = new MyOval( eX2, eY, eX2+eW, eY+eH, faceCol, true );
        shapes[3] = new MyOval( eX2, eY, eX2+eW, eY+eH, Color.BLACK, false );
        shapes[4] = new MyOval( eX1, eY, eX1+xDim, eY+yDim, faceCol, true );
        shapes[5] = new MyOval( eX1, eY, eX1+xDim, eY+yDim, Color.BLACK, false );
        for ( int i = 6; i < 10; i++ )
            shapes[i] = null;
    }

    public void defineEyes ( Color eyeCol )
    {   //  Create the eyes
        eW = (int)Math.round( eyeSize * 1.2 );
        eH = (int)Math.round( eyeSize * 0.8 );
        eY = (int)Math.round( yPos - eyeY );
        eX1 = (int)Math.round( xPos - eyeX - ( eW / 2 ) );
        eX2 = (int)Math.round( xPos + eyeX - ( eW / 2 ) );
        //  Whites of Eyes
        shapes[10] = new MyOval( eX1, eY, eX1+eW, eY+eH, Color.WHITE, true );
        shapes[11] = new MyOval( eX1, eY, eX1+eW, eY+eH, Color.BLACK, false );
        shapes[12] = new MyOval( eX2, eY, eX2+eW, eY+eH, Color.WHITE, true );
        shapes[13] = new MyOval( eX2, eY, eX2+eW, eY+eH, Color.BLACK, false );
        //  Irises of eyes
        eW = (int)Math.round( eyeSize * 0.8 );
        eX1 = (int)Math.round( xPos - eyeX - ( eW / 2 ) );
        eX2 = (int)Math.round( xPos + eyeX - ( eW / 2 ) );
        shapes[14] = new MyOval( eX1, eY, eX1+eW, eY+eH, eyeCol, true );
        shapes[15] = new MyOval( eX1, eY, eX1+eW, eY+eH, Color.BLACK, false );
        shapes[16] = new MyOval( eX2, eY, eX2+eW, eY+eH, eyeCol, true );
        shapes[17] = new MyOval( eX2, eY, eX2+eW, eY+eH, Color.BLACK, false );
        eH = (int)Math.round( eyeSize * 0.6 );
        shapes[18] = new MyOval( eX1+eW/4, eY+eW/4, eX1+eH, eY+eH, Color.BLACK, true );
        shapes[19] = new MyOval( eX2+eW/4, eY+eW/4, eX2+eH, eY+eH, Color.BLACK, true );
        for ( int i = 20; i < 30; i++ )
            shapes[i] = null;
    }

    public void defineMouth ( int style, int width, Color faceCol )
    {   //  Create the mouth
        switch ( style )
        {   //  Construct default mouth
            case 1: shapes[30] = new MyOval( xPos-width, yPos+mouthY-mouthH, xPos+width, yPos+mouthY+mouthH, Color.PINK, true );
                    for ( int i = 31; i < 39; i++ )
                        shapes[i] = null;
                    shapes[39] = new MyOval( xPos-width, yPos+mouthY-mouthH*2, xPos+width, yPos+mouthY, faceCol, true );
                    break;
            //  More definitions to come
        }
    }

    public void defineNose ( int style, int height )
    {   //  Create the nose
        switch ( style )
        {   //  Construct default nose
            case 1: shapes[40] = new MyOval( xPos-noseW/2, yPos-height, xPos+noseW/2, yPos-height+noseH, Color.BLACK, true );
                    for ( int i = 41; i < 50; i++ )
                        shapes[i] = null;
                    break;
            //  More definitions to come
        }
    }

   // handles mouse events for this JPanel
   private class MouseHandler extends MouseAdapter
      implements MouseMotionListener
   {
       // creates and sets the initial position for the new shape
       public void mousePressed( MouseEvent e )
       {
           if ( target > 0 && target < 10 || target == 39 ) 
           {
               inspect.select( 1 );
               targetName = "face";
           }
           else if ( target > 9 && target < 30 ) 
           {
               inspect.select( 2 );
               targetName = "eyes";
           }
           else if ( target > 29 && target < 39 ) 
           {
               inspect.select( 3 );
               targetName = "mouth";
           }
           else if ( target > 39 && target < 48 ) 
           {
               inspect.select( 4 );
               targetName = "nose";
           }
           else 
           {
               inspect.select( 0 );
               targetName = "none";
           }
       } // end method mousePressed

       // updates the status bar to show the current mouse coordinates
       public void mouseMoved( MouseEvent e )
       {
           int x = e.getX();
           int y = e.getY();
           target = 0;
           for ( int i = 49; i >= 0; i--)
           {
               if ( shapes[i] != null )
               {
                   if ( x > shapes[i].getX1() && x < shapes[i].getX2() )
                   {
                       if ( y > shapes[i].getY1() && y < shapes[i].getY2() )
                       {
                           target = i;
                           break;
                       }
                   }
               }
           }

           statusLabel.setText(String.format( "(%d,%d)  ", e.getX(), e.getY() ) + inspect.picked() );
       } // end method mouseMoved
   } // end class MouseHandler
} // end class DrawPanel

1 个答案:

答案 0 :(得分:0)

我不确定你在问什么,但我认为你正在寻找removeAll()方法。它将删除现有控件,以便您可以放置​​新控件。类似的东西:

void inspect(int thing) {
    removeAll();
    if (thing == 0) { // nothing
        add(new JLabel("Nothing selected")); 
    } else if (thing == 1) { // face
        JComboBox faceColors = new JComboBox(
            new String[] { "Green", "Yellow", "Magenta" });
        faceColors.addItemListener(...);
        add(faceColors);
    } else {
        // etc
    }
    pack();
}