Java 未调用repaint()方法

Java 未调用repaint()方法,java,swing,repaint,Java,Swing,Repaint,所以我制作了一个程序,用户可以根据点击的位置在屏幕上画点,然后从每个点到下一个点画线。现在我想让另一个JFrame弹出,用户可以选择更改点(顶点)、背景或线(边)的颜色。到目前为止,我的守则是: // Fig. 14.34: Painter.java // Testing PaintPanel. public class Painter { public static void main( String[] args ) { // create JFrame

所以我制作了一个程序,用户可以根据点击的位置在屏幕上画点,然后从每个点到下一个点画线。现在我想让另一个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(在屏幕上绘制线和点的类)


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

不要重写
paint
,然后继续断开绘制链。重写
paintComponent
,然后调用它的
super
方法。不要在任何绘制方法中更改组件的状态(例如,不要调用
setBackground
),这将导致基于示例代码的无休止的重新绘制循环,我不知道在哪里使用了
颜色变换器
,也不知道它会对屏幕上显示的
PaintPanel
实例产生什么影响。考虑提供一个说明你的问题的方法。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱,提高效率responses@MadProgrammer在我的画师课上,我有一行
colorchancer mycolorchancer=newcolorchancer
是的,但这就是你的全部。它与
PaintPanel
的实例有什么关系?调用它的
changeBackground
方法将如何更改
PaintPanel
的实例?您有一个
ColorChanger
的实例和
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
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);
    }
}