Repaint()和PaintComponent()未正确更新

时间:2015-11-17 22:54:15

标签: java swing paintcomponent repaint

我有一个带有两个按钮的Jframe:' A'和' B'。点击按钮' A'应该在JPanel中显示大写字母A.这里的问题是,在重复鼠标点击时,之前绘制的字母不会显示。我想确保绘制的内容保留在图纸中。以下是我的程序的代码。

JFrame代码:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawFrame extends JFrame{
    private final int WIDTH = 500;
    private final int HEIGHT = 300;

    private GUIModel model;

    private JButton number1;
    private JButton number2;

    private JPanel numberPanel;
    private DrawPanel graphicsPanel;

    public DrawFrame()
    {
        this.model = new GUIModel(" ");

        createSelectionPanel();
        createGraphicsPanel();

        this.setSize(WIDTH, HEIGHT);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void createSelectionPanel()
    {
        numberPanel = new JPanel();

        ButtonListener listener = new ButtonListener();

        number1 = new JButton("A");
        number1.addActionListener(listener);

        number2 = new JButton("B");
        number2.addActionListener(listener);  

        numberPanel.setLayout(new GridLayout(2,2));
        numberPanel.add(number1);
        numberPanel.add(number2);

        this.add(numberPanel, BorderLayout.WEST);
    }

    private void createGraphicsPanel()
    {
        //instantiate drawing panel
        graphicsPanel = new DrawPanel(WIDTH, HEIGHT, model);
        //add drawing panel to right
        add(graphicsPanel);
    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            model.setDisplayString(event.getActionCommand());
        }
    }

    //creates a drawing frame
    public static void main(String[] args)
    {
        DrawFrame draw = new DrawFrame();
    }   
}

JPanel代码:

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

public class DrawPanel extends JPanel{
    private static final long serialVersionUID = 3443814601865936618L;

    private Font font = new Font("Default", Font.BOLD, 30);

    private static final Color DEFAULT_TEXT_COLOR = Color.BLACK;
    private static final Color HOVER_TEXT_COLOR = Color.RED;
    private Color color = DEFAULT_TEXT_COLOR;

    private List<GUIModel> numberList = new ArrayList<GUIModel>();
    private GUIModel model;
    boolean mouseHover = false;

    public DrawPanel(int width, int height, GUIModel model){
        this.setPreferredSize(new Dimension(width, height));
        this.model = model;

        //set white background for drawing panel
        setBackground(Color.WHITE);

        //add mouse listeners
        MouseHandler mouseHandler = new MouseHandler();
        this.addMouseListener(mouseHandler);
        this.addMouseMotionListener(mouseHandler);
    }

//    void checkForHover(MouseEvent event) {
//        FontMetrics metrics = getFontMetrics(font);
//
//        Graphics g = getGraphics();
//        Rectangle textBounds = metrics.getStringBounds("C", g).getBounds();
//        g.dispose();
//
//        int index = 0;
//        while (index < numberList.size()) {
//            Double x = numberList.get(index).getCoordinate().getX();
//            Double y = numberList.get(index).getCoordinate().getY();
//            
//            textBounds.translate(x.intValue(), y.intValue());
//            
//            if (textBounds.contains(event.getPoint())) {
//                color = HOVER_TEXT_COLOR;
//            }
//            else {
//                color = DEFAULT_TEXT_COLOR;
//            }
//            index++;
//        }
//    }
  

这是我的PaintComponent方法,我将浏览不同坐标绘制的字母列表并将其绘制在画布上

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setFont(font);
        g.setColor(color);

        int index = 0;
        while (index < numberList.size()) {
            Double x = numberList.get(index).getCoordinate().getX();
            Double y = numberList.get(index).getCoordinate().getY();
            String display = numberList.get(index).getDisplayString();
            g.drawString(display, x.intValue(), y.intValue());
            index++;
        }

        if (model.getCoordinate() != null) {
            Point p = model.getCoordinate();            
            g.drawString(model.getDisplayString(), p.x, p.y);
            numberList.add(model);
        }
    }

    //class to handle all mouse events
    private class MouseHandler extends MouseAdapter implements MouseMotionListener
    {
        @Override
        public void mousePressed(MouseEvent event)
        {
           model.setCoordinate(event.getPoint());
        }

        @Override
        public void mouseReleased(MouseEvent event)
        {
            DrawPanel.this.repaint();
        }

//        @Override
//        public void mouseEntered(MouseEvent event) {
//            checkForHover(event);
//        }
//        
//        @Override
//        public void mouseMoved(MouseEvent event) {
//            checkForHover(event);
//        }
    }
}

GUIModel的代码:

import java.awt.Point;

public class GUIModel {
    private String displayString;
    private Point coordinate;

    public GUIModel(String displayString) {
        this.displayString = displayString;
    }
    public void setDisplayString(String displayString) {
        this.displayString = displayString;
    }

    public String getDisplayString() {
        return displayString;
    }

    public Point getCoordinate() {
        return coordinate;
    }

    public void setCoordinate(int x, int y) {
        this.coordinate = new Point(x, y);
    }

    public void setCoordinate(Point coordinate) {
        this.coordinate = coordinate;
    }   
}

非常感谢任何帮助或建议。感谢。

1 个答案:

答案 0 :(得分:0)

@MadProgrammer您的评论帮助我修复了我的代码。就像你说的我需要创建一个新的GUIModel并将其添加到我的numberList。

我修改了我的paintComponent方法来反映它,现在它可以工作了。所以,谢谢!

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setFont(font);
    g.setColor(color);

    int index = 0;
    while (index < numberList.size()) {
        Double x = numberList.get(index).getCoordinate().getX();
        Double y = numberList.get(index).getCoordinate().getY();
        String display = numberList.get(index).getDisplayString();
        g.drawString(display, x.intValue(), y.intValue());
        index++;
    }

    if (model.getCoordinate() != null) {
        Point p = model.getCoordinate();            
        g.drawString(model.getDisplayString(), p.x, p.y);
        GUIModel number = new GUIModel();
        number.setCoordinate(p);
        number.setDisplayString(model.getDisplayString());
        numberList.add(number);
    }
}