如何不重绘JFrame?只是画画

时间:2011-09-09 05:57:59

标签: java frame paintcomponent mouselistener

我需要简单程序的帮助。我想,当我点击程序标记它。我想看看我的举动过去了。就像油漆中的铅笔一样。

class Test.java

import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.IOException;
import java.lang.*;
import javax.swing.*;

public class Test{

    JFrame frame;

    public static void main(String[] args){
        Test smallTest = new Test();
        smallTest.letsDoIt();
    }

    public void letsDoIt(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton buttonOfTheEnd = new JButton("EXIT");
        buttonOfTheEnd.addActionListener(new theEndListener());

        graphPanel panelR = new graphPanel();
        panelR.setBackground(Color.WHITE);
        frame.getContentPane().add(BorderLayout.CENTER, panelR);
        frame.getContentPane().add(BorderLayout.SOUTH, buttonOfTheEnd);
        frame.setSize(500,500);
        frame.setVisible(true);


        frame.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {

                frameMouseClicked(evt);

            }
        });

    }

    void frameMouseClicked(java.awt.event.MouseEvent evt) {


        System.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
        frame.repaint();

    }

    class theEndListener implements ActionListener {
        public void actionPerformed(ActionEvent zdarzenie){
            System.exit(0);
        }
    }


    // public void paintComponent(Graphics g){
        // super.paintComponent(g);
        // int wspX = MouseInfo.getPointerInfo().getLocation().x;
        // int wspY = MouseInfo.getPointerInfo().getLocation().y;

        // g.setColor(Color.RED);
        // g.fillRect( wspX, wspY, 10, 10);

    // 




}

class graphPanel

import java.awt.*;
import javax.swing.*;

class graphPanel extends JPanel{


    public void paintComponent(Graphics g){
        super.paintComponent(g);


        int locX = MouseInfo.getPointerInfo().getLocation().x - 10;
        int locY = MouseInfo.getPointerInfo().getLocation().y - 30;
        g.setColor(Color.ORANGE);
        g.fillRect(locX, locY, 10, 10);
        }
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

尝试在graphPanel的图像上绘画,将图像绘制到组件上 如果您没有清除图像,则附上每张图纸。

class graphPanel extends JPanel{
    Image img;
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(img == null) {
            img = new BufferedImage( getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR );
            img.getGraphics().setColor( getBackground() );
            img.getGraphics().fillRect( 0, 0, getWidth(), getHeight() );
        }
        int locX = MouseInfo.getPointerInfo().getLocation().x - 10;
        int locY = MouseInfo.getPointerInfo().getLocation().y - 30;
        Graphics imgG = img.getGraphics();
        imgG.setColor(Color.ORANGE);
        imgG.fillRect(locX, locY, 10, 10);
        g.drawImage( img, 0, 0, this );
    }
}

答案 1 :(得分:2)

您必须将最近的位置存储在某处。 List是个不错的选择。现在,当您单击屏幕上的某个位置时,将位置添加到列表中,当您需要重新绘制屏幕时,将绘制所有存储的位置。这就像分离模型和视图模型是您的2D位置列表,视图是您在屏幕上绘制的内容)。< / p>

所以在某处创建一个列表:

// public static only to keep it simple!!
public static List<Point> points = new ArrayList<Point>();

然后(谢谢,oliholz!)将听众添加到panelR而不是frame

panelR.addMouseListener(new MouseAdapter() {
  // ...

并编写frameMouseClicked,如:

void frameMouseClicked(MouseEvent evt) {
    points.add(evt.getPoint());
    frame.repaint();
}

paintComponent内,您遍历列表并绘制所有存储的位置(再次:非常简单,简化,可改进!)

g.setColor(Color.ORANGE);
for (Point point:points) {   // <- this is the list of stored points
  g.fillRect(point.x, point.y, 10, 10);
}