如何在jPanel中绘制? (swing / graphics Java)
我正在制作一个项目,我正在试图制作一个绘画程序。 到目前为止,我已经使用netbeans来创build一个GUI并设置程序。
到目前为止,我可以调用所有必要的协调来绘制它,但是我很困惑如何真正地在里面绘画。
在我的代码的最后,我有一个失败的尝试在面板内绘图。
任何人都可以解释/显示如何在这样的例子中使用graphics?
我find的所有示例都使用JPanel
创build了一个类并将其扩展,但是我不知道是否可以这样做,因为它是在netbeans中生成的。
我需要在JFrame
内部绘制一个JPanel
。 我不知道把graphics类放在哪里。
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
,这是我想要的。
请注意额外的评论。
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); } } }
屏幕截图
其他的例子 – 更多的针对多个线条和多个线段
HFOE提供了一个很好的链接作为第一个评论在这个线程。 Camickr还在“ 自定义绘画方法”文章中描述了活动绘画与绘制到“ BufferedImage
”的情况。
另请参阅此方法在BufferedImage
使用绘画 。
使用graphics用户界面时,需要记住在Java AWT / Swing事件队列中完成在窗格上的绘图。 你不能在paint()
/ paintComponent()
/ etc之外使用Graphics
对象。 方法。
但是,您可以使用一种称为“ 帧缓冲 ”的技术。 基本上,你需要有一个BufferedImage,并直接在它上面绘制(请参阅它的createGraphics()
方法; 该graphics上下文可以保留和重用在同一个BufferedImage
实例上的多个操作,不需要一直重新创build它,只有在创build一个新的实例)。 然后,在你的JPanel
的paintComponent()
,你只需要将BufferedImage
实例绘制到JPanel
。 使用这种技术,您可以通过仿射变换相当轻松地执行缩放,平移和旋转操作。
这是一个简单的例子。 我想这很容易理解:
import java.awt.*; import javax.swing.JFrame; import javax.swing.JPanel; public class Graph extends JFrame { JFrame f = new JFrame(); JPanel jp; public Graph() { f.setTitle("Simple Drawing"); f.setSize(300, 300); f.setDefaultCloseOperation(EXIT_ON_CLOSE); jp = new GPanel(); f.add(jp); f.setVisible(true); } public static void main(String[] args) { Graph g1 = new Graph(); g1.setVisible(true); } class GPanel extends JPanel { public GPanel() { f.setPreferredSize(new Dimension(300, 300)); } @Override public void paintComponent(Graphics g) { //rectangle originates at 10,10 and ends at 240,240 g.drawRect(10, 10, 240, 240); //filled Rectangle with rounded corners. g.fillRoundRect(50, 50, 100, 100, 80, 80); } }
}
输出如下所示:
Bijaya Bidari对代码的修改,在没有关于在构造函数中可重复的方法调用的警告的情况下,被Java 8接受:
public class Graph extends JFrame { JPanel jp; public Graph() { super("Simple Drawing"); super.setSize(300, 300); super.setDefaultCloseOperation(EXIT_ON_CLOSE); jp = new GPanel(); super.add(jp); } public static void main(String[] args) { Graph g1 = new Graph(); g1.setVisible(true); } class GPanel extends JPanel { public GPanel() { super.setPreferredSize(new Dimension(300, 300)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); //rectangle originated at 10,10 and end at 240,240 g.drawRect(10, 10, 240, 240); //filled Rectangle with rounded corners. g.fillRoundRect(50, 50, 100, 100, 80, 80); } } }