repaint()方法没有被调用

时间:2016-01-30 20:04:04

标签: java swing repaint

所以我创建了一个程序,用户可以根据点击的位置在屏幕上绘制点,然后从每个点到下一个点绘制线条。现在我想让它弹出另一个JFrame,用户可以选择更改点(顶点),背景或线条(边缘)的颜色。我到目前为止的代码是:

// Fig. 14.34: Painter.java
// Testing PaintPanel.
public class Painter
{
   public static void main( String[] args )
   { 
      // create JFrame
      JFrame application = new JFrame( "A simple paint program" );

      PaintPanel paintPanel = new PaintPanel(); // create paint panel
      ColorChanger myColorChanger = new ColorChanger();
      application.add( paintPanel, BorderLayout.CENTER ); // in center

      // create a label and place it in SOUTH of BorderLayout
      application.add( new JLabel( "Drag the mouse to draw" ), 
         BorderLayout.SOUTH );

      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      application.setSize(600, 600 ); // set frame size
      application.setVisible( true ); // display frame
   } // end main
} // end class Painter

类PaintPanel(在屏幕上绘制线条和点的类)

// Fig. 14.34: PaintPanel.java
// Using class MouseMotionAdapter.

public class PaintPanel extends JPanel
{
   private int pointCount = 0; // count number of points

   // array of 10000 java.awt.Point references
   private Point[] points = new Point[100];
   private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   private Color backgroundColour = Color.GRAY;
   private Color edgeColour = Color.WHITE;
   private Color vertexColour = Color.WHITE;
   private Color textColour = Color.BLACK;

   // set up GUI and register mouse event handler
   public PaintPanel()
   {
       setLayout(new BorderLayout());
       setBackground(backgroundColour);
      // handle frame mouse motion event
       addMouseListener(
               new MouseAdapter() // anonymous inner class
               {
                   public void mousePressed(MouseEvent e) {
                       points[pointCount] = e.getPoint();
                       //Subtract 15 from both x and y so the middle of the circle is drawn where we click
                       points[pointCount].x -= 15;
                       points[pointCount].y -= 15;
                       pointCount++;
                       repaint(); //THIS repaint() IS BEING CALLED
                   }
               } // end anonymous inner class
       ); // end call to addMouseMotionListener
   } // end PaintPanel constructor

   public void changeBackground(Color newColour) {
       System.out.println("Paint Panel change");
       backgroundColour = newColour;
       edgeColour = newColour;
       repaint(); //repaint() NOT BEING CALLED
   }

   public void changeEdge(Color newColour) {
       edgeColour = newColour;
       repaint(); //repaint() NOT BEING CALLED
   }

   // draw ovals in a 4-by-4 bounding box at specified locations on window
   public void paint( Graphics g )
   {
      System.out.println("PAINTCOMPONENT");
      super.paintComponent( g ); // clears drawing area
      System.out.println("paintComponent backgroundColour = " + backgroundColour);
      setBackground(backgroundColour);
      g.setColor(textColour);
      Font myFont = new Font("Serif", Font.BOLD, 20);
      g.setFont(myFont);
      // draw all points in array
      for ( int i = 0; i < pointCount; i++ ) {
         g.setColor(vertexColour);
         g.fillOval( points[ i ].x, points[ i ].y, 30, 30 );
         g.setColor(textColour);
         g.drawString(Character.toString(alphabet.charAt(i % 26)), points[i].x + 10, points[i].y + 20);
         g.setColor(edgeColour);
         if(i > 0) {
             g.drawLine(points[i].x + 15, points[i].y + 15, points[i-1].x + 15, points[i-1].y + 15);
         }
      }
   } // end method paintComponent
} // end class PaintPanel

Class ColorChanger(打开显示三个JButton的JFrame(改变线条,点或背景的颜色):

public class ColorChanger extends PaintPanel {
    JFrame myFrame;
    JButton background, edge, vertex;
    public ColorChanger() {
        myFrame = new JFrame("Color select tool");
        myFrame.setLayout(new FlowLayout());
        myFrame.setSize(500, 100);
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        background = new JButton("Change background colour");
        edge = new JButton("Change edge colour");
        vertex = new JButton("Change vertex colour");
        background.addActionListener(new ButtonListener());
        edge.addActionListener(new ButtonListener());
        vertex.addActionListener(new ButtonListener());
        myFrame.add(background);
        myFrame.add(edge);
        myFrame.add(vertex);

    }
    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() ==  background) {
                changeBackground();
            } else if (e.getSource() == edge) {
                changeEdge();
            } else {
                System.out.println("Vertex");
            }   
        }
    }
    private void changeBackground() {
        System.out.println("ColorChanger change Background");
        super.changeBackground(Color.BLACK);
    }
    private void changeEdge() {
        System.out.println("change  edge");
        super.changeEdge(Color.RED);
    }
}

所以这个程序成功地绘制了点并连接线但我的问题是它不会改变任何颜色,因为类repaint()中的PaintPanel调用不会工作,除了第一个内部匿名内部类

0 个答案:

没有答案