开发者

How to do not repaint JFrame? Just paint

I need help with simple program. I would like, when I click program marks it. I would like to see my moves did in past. Like pencil in paint.

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

thanks for help.


You have to store the recent locations somewhere. A List is a good choice. Now, when you click somewhere on the screen, you add the location to the list and when you need to repaint the screen, you paint all stored location. That's something like separating model and view (the model is your list of 2D locations, the view is what you paint on the screen).

So create one list somewhere:

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

Then (thanks, oliholz!) add the listener to panelR instead of frame:

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

and write frameMouseClicked like:

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

Inside paintComponent you iterate through the list and draw all stored locations (again: very simple and reduced, improveable!)

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


Try to paint on an image in your graphPanel an paint the image to the component.
If you do not clear the image you attach every drawing.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜