如何在JPanel中绘制?(Swing/graphics Java)

2022-08-31 21:02:48

我正在做一个项目,我试图制作一个油漆程序。到目前为止,我已经使用 Netbeans 创建了一个 GUI 并设置了程序。

截至目前,我能够调用所有必要的协调来绘制它,但我对如何在内部实际绘画感到非常困惑。

在我的代码末尾,我试图在面板内部绘制失败。

任何人都可以解释/展示如何在这样的示例中使用图形吗?

我发现的所有示例都创建了一个类并对其进行扩展,但我不知道我是否可以做到这一点,因为它是在netbeans中生成的。JPanel

我需要在JPanel中绘制,在我的JFrame中。我不知道在哪里放置图形类。

JavaPaintUI 类

package javapaint;

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

public class JavaPaintUI extends javax.swing.JFrame {

public JavaPaintUI() {
    initComponents();
}


private void initComponents() {


    jPanel2 = new javax.swing.JPanel();

    jPanel2.setBackground(new java.awt.Color(255, 255, 255));
    jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
    jPanel2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mousePressed(java.awt.event.MouseEvent evt) {
            jPanel2MousePressed(evt);
        }
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            jPanel2MouseReleased(evt);
        }
    });
    jPanel2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseDragged(java.awt.event.MouseEvent evt) {
            jPanel2MouseDragged(evt);
        }
    });
    pack();
}// </editor-fold>                        

int currentX, currentY, oldX, oldY;

private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {                                     
    if (tool == 1) {
        currentX = evt.getX();
        currentY = evt.getY();
        oldX = currentX;
        oldY = currentY;
        System.out.println(currentX + " " + currentY);
        System.out.println("PEN!!!!");
    }

}                                    

private void jPanel2MousePressed(java.awt.event.MouseEvent evt) {                                     
    oldX = evt.getX();
    oldY = evt.getY();
    System.out.println(oldX + " " + oldY);
}                                    


//mouse released//
private void jPanel2MouseReleased(java.awt.event.MouseEvent evt) {                                      
    if (tool == 2) {
        currentX = evt.getX();
        currentY = evt.getY();
        System.out.println("line!!!! from" + oldX + "to" + currentX);
    }
}                                     

//set ui visible//
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new JavaPaintUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JPanel jPanel2;
// End of variables declaration                   

class jPanel2 extends JPanel {

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

        g.drawString("BLAH", 20, 20);
        g.drawRect(200, 200, 200, 200);
    }
}
}

屏幕截图

整个事情是一个,中间的白色部分是我想画的。JFramejPanel2screen shot of some code that is not this


答案 1

请注意额外的注释。

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

class JavaPaintUI extends JFrame {

    private int tool = 1;
    int currentX, currentY, oldX, oldY;

    public JavaPaintUI() {
        initComponents();
    }

    private void initComponents() {
        // we want a custom Panel2, not a generic JPanel!
        jPanel2 = new Panel2();

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        jPanel2.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                jPanel2MousePressed(evt);
            }
            public void mouseReleased(MouseEvent evt) {
                jPanel2MouseReleased(evt);
            }
        });
        jPanel2.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent evt) {
                jPanel2MouseDragged(evt);
            }
        });

        // add the component to the frame to see it!
        this.setContentPane(jPanel2);
        // be nice to testers..
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }// </editor-fold>

    private void jPanel2MouseDragged(MouseEvent evt) {
        if (tool == 1) {
            currentX = evt.getX();
            currentY = evt.getY();
            oldX = currentX;
            oldY = currentY;
            System.out.println(currentX + " " + currentY);
            System.out.println("PEN!!!!");
        }
    }

    private void jPanel2MousePressed(MouseEvent evt) {
        oldX = evt.getX();
        oldY = evt.getY();
        System.out.println(oldX + " " + oldY);
    }


    //mouse released//
    private void jPanel2MouseReleased(MouseEvent evt) {
        if (tool == 2) {
            currentX = evt.getX();
            currentY = evt.getY();
            System.out.println("line!!!! from" + oldX + "to" + currentX);
        }
    }

    //set ui visible//
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JavaPaintUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private JPanel jPanel2;
    // End of variables declaration

    // This class name is very confusing, since it is also used as the
    // name of an attribute!
    //class jPanel2 extends JPanel {
    class Panel2 extends JPanel {

        Panel2() {
            // set a preferred size for the custom panel.
            setPreferredSize(new Dimension(420,420));
        }

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

            g.drawString("BLAH", 20, 20);
            g.drawRect(200, 200, 200, 200);
        }
    }
}

屏幕截图

enter image description here

其他示例 - 更适合多条线段和多线段

HFOE将一个很好的链接作为此线程的第一条评论。Camickr还在自定义绘画方法一文中描述了主动绘画与绘画BufferedImage

另请参阅在缓冲图像中使用绘画的此方法。


答案 2

使用图形用户界面时,您需要记住,在窗格上的绘制是在 Java AWT/Swing 事件队列中完成的。您不能只在 //etc. 方法之外使用该对象。Graphicspaint()paintComponent()

但是,您可以使用一种称为“帧缓冲”的技术。基本上,你需要有一个BufferedImage并直接在其上绘制(请参阅它的方法;该图形上下文可以保留并重用于同一实例上的多个操作,无需始终重新创建它,只需在创建新实例时)。然后,在您的 中,您只需要将实例绘制到 .使用这种技术,您可以通过仿射变换非常轻松地执行缩放、平移和旋转操作。createGraphics()BufferedImageJPanelpaintComponent()BufferedImageJPanel


推荐