Calling paintComponent() with parameters using rep

2019-08-31 17:48发布

I am very new to graphics with Java, so just ask if any additional information is needed :)

I am trying to paint shapes based on where the mouse clicks on the screen. Because of this, I need to pass the x and y coordinates of where I clicked to the paintComponent() method so that it will know where to paint the shape.

public void mouseClicked(MouseEvent e) {
    System.out.println("Adding Shape");
    repaint();
}

class CanvasDrawArea extends JPanel{
    //this should run when the program first starts
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        canvas.setBackground(CANVAS_COLOR);
    }

    //here is where the question is
    public void paintComponent(Graphics g, int x, int y){
        super.paintComponent(g);
        g.fillRect(x, y, RECTANGLE_WIDTH, RECTANGLE_HEIGHT);
    }
}

basically I am trying to overload the paintComponent by having one that runs right when the program starts by calling the repaint() / pack() method, and one that will run when I give it the x and y coordinates. I am unsure, however, how I am supposed to go about passing the x and y parameters, as there is no way to pass them in the repaint() method.

1条回答
萌系小妹纸
2楼-- · 2019-08-31 18:09
  1. You should never have the need to call paintComponent or paint directly these are called automatically by the repaint manager when required...
  2. Create java.util.List and store the Point of each mouse click in it, call repaint from the mouseClicked method after you have done this.
  3. In your paintComponent(Graphics) method, iterate over the List of points and paint the shape you need.

As a simple example...

Dotty

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Dotty {

    public static void main(String[] args) {
        new Dotty();
    }

    public Dotty() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DottyPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class DottyPane extends JPanel {

        private List<Point> points;

        public DottyPane() {
            points = new ArrayList<>(25);
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    points.add(e.getPoint());
                    repaint();
                }

            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            for (Point p : points) {
                g2d.fillOval(p.x - 5, p.y - 5, 10, 10);
            }
            g2d.dispose();
        }

    }

}
查看更多
登录 后发表回答