如何不重新绘制JFrame?只是画画

发布于 2024-12-03 17:07:24 字数 2275 浏览 2 评论 0原文

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

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

    // 




}

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

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2024-12-10 17:07:24

您必须将最近的位置存储在某处。 List 是一个不错的选择。现在,当您单击屏幕上的某个位置时,您可以将该位置添加到列表中,当您需要重新绘制屏幕时,您可以绘制所有存储的位置。这类似于分离模型和视图模型是您的二维位置列表,视图是您在屏幕上绘制的内容)。

因此,在某处创建一个列表:

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

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);
}
内心旳酸楚 2024-12-10 17:07:24

尝试在 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 );
    }
}

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 );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文