gpt4 book ai didi

java - 如何在 JPanel 中绘图? ( Swing/图形Java)

转载 作者:IT老高 更新时间:2023-10-28 20:33:47 26 4
gpt4 key购买 nike

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

到目前为止,我可以调用所有必要的协调来在其中绘制,但我对如何在其中实际绘制感到非常困惑。

在我的代码接近尾声时,我尝试在面板内绘图失败。

谁能解释/展示如何在这样的示例中使用图形?

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

我需要在我的 JFrame 内的 JPanel 内绘图。我不知道把图形类放在哪里。

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

屏幕截图

整个东西是一个JFrame,中间的白色部分是jPanel2,这是我想画的。 screen shot of some code that is not this

最佳答案

注意额外的注释。

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 还在 Custom Painting Approaches 中描述了主动绘制与绘制到 BufferedImage 的关系。文章。

另请参阅使用 painting in a BufferedImage 的方法.

关于java - 如何在 JPanel 中绘图? ( Swing/图形Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com