用参数repaint()调用paintComponent() [英] Calling paintComponent() with parameters using repaint()

查看:175
本文介绍了用参数repaint()调用paintComponent()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据鼠标点击的位置绘制形状。

我试图根据鼠标点击的位置绘制形状屏幕上。因此,我需要将点击的位置的x和y坐标传递给 paintComponent()方法,以便它知道在哪里绘制形状。

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

class CanvasDrawArea extends JPanel {
//程序第一次启动时应该运行
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
canvas.setBackground(CANVAS_COLOR);
}

//这里的问题是
public void paintComponent(Graphics g,int x,int y){
super.paintComponent(g);
g.fillRect(x,y,RECTANGLE_WIDTH,RECTANGLE_HEIGHT);


$ / code>

基本上我试图通过让paintComponent重载在程序开始时通过调用 repaint() / pack()方法来运行,另一个将运行当我给它的X和Y坐标。然而,我不确定我是如何去传递x和y参数的,因为没有办法在 repaint()方法中传递它们。

解决方案


  1. 你永远不需要调用 paintComponent paint 直接由repaint管理器在需要时自动调用它们...

  2. 创建 java .util.List 并存储每个鼠标点击的 Point ,调用 repaint mouseClicked 方法完成此操作。

  3. paintComponent(Graphics)方法,遍历点的 List 并绘制所需的形状。

举个简单的例子...

  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();

$ b public Dotty(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
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>点;

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();
}

}

}


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. 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...

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();
        }

    }

}

这篇关于用参数repaint()调用paintComponent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆