简体   繁体   中英

How to get JPanel to remember user-drawn graphics when panel is resized?

I'm trying to make a program that allows users to place music notes on staffs. There is a class called SheetMusicPane that extends JPanel, and it starts out with a bunch of staffs drawn from top to bottom. By clicking on the buttons in the toolbar at the top, users can choose notes to draw. There is also a button on the toolbar that allows the user to increase the amount of sheet music available (the SheetMusicPane is set inside a JScrollPane). I don't know how to make the music notes copy over when the user resizes the SheetMusicPane. The initial staffs stay put, however.

I tried the JPanel.repaint() method; it didn't work.

The resizing is pretty simple:

JButton moreMusicButton = new JButton("Get More Sheet Music");
    tb.add(moreMusicButton);
    moreMusicButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           smp.setPreferredSize(new Dimension(scrollSize.width, smp.getHeight() +     moreMusicAmount));
           smp.repaint();
        }
    });

As said above, repaint() isn't doing anything.

Thanks in advance!

Adding note-drawing code:

The note-drawing buttons each call this method (the String "note" is obtained from the button's ActionCommand):

  public void getShape(final Graphics g, final String note) {
     MouseListener[] listeners = this.getMouseListeners();
     for (MouseListener ml : listeners) {
        this.removeMouseListener(ml);  //makes graphics stop drawing previous note
     }
     this.addMouseListener(new MouseListener() {
        public void mousePressed(MouseEvent e) {
        }
        public void mouseReleased(MouseEvent e) {
        }
        public void mouseEntered(MouseEvent e) {
        }
        public void mouseExited(MouseEvent e) {
        }
        public void mouseClicked(MouseEvent e) {
           Point p = MouseInfo.getPointerInfo().getLocation();
           addShape(g, p.x, p.y, note);
           int pitch = 12;
           piano.playNote(pitch);
           advance(1.0, piano);
           try { addToFile(pitch, note);}
           catch(FileNotFoundException fnfe) {}
           catch(IOException ioe) {}
        }
     });
  }

Here's how the panel itself is created. It comprises a set of private nested classes and it starts out showing plain, note-free sheet music.

private class SheetMusicPane extends JPanel {

  public SheetMusicPane() {
     final Graphics g = this.getGraphics();  
  } 

  //paints staffs
  public void paint(Graphics g) {
     super.paintComponent(g);
     for (int i = 0; (i - 1)*staffWidth < scrollSize.height + 100; i++) {
        int y = paddingNorth + i*staffWidth;
        Staff staff = new Staff(g, y, "treble");
     }
  }

  public void paintComponent(Graphics g) {
     super.paintComponent(g);
     for (String note : noteList) {
        addShape(g, 100, 200, note); 
     }
  }

  //takes in mouse info and calls addShape to draw notes on clicks
  public void getShape(final Graphics g, final String note) {  ... } //as shown above

  //draws a note
  private void addShape(Graphics g, int x, int y, String note) { ... }

  private void addToFile(int pitch, String note) throws FileNotFoundException, IOException { ...//adds note info to a file }

//just makes a five-line staff at this point private class Staff {

  private Graphics g;
  private int y;
  private int[] pitches;

  public Staff(Graphics g, int y, String cleff) {  ...//draws the lines }

}

you need to override the paintComponent method of your Panel. You save everything to paint in an Array (or List) and the paintComponent method draws them. (on every repaint).

If you draw them on click, the next time repaint() is called the Panel will be cleared and repainted using the paintComponent Method. Your drawings are as long on the Panel as reapaint() is not called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM