如何在JPanel中绘制?(Swing/graphics Java)
我正在做一个项目,我想做一个绘画程序。 到目前为止,我已经使用Netbeans创建了GUI并设置了程序 到目前为止,我能够调用所有必要的协调来在里面画画,但我对如何在里面画画感到非常困惑 在代码的末尾,我尝试在面板内部绘制时失败了 有人能解释/演示如何在这样的示例中使用图形吗 我发现的所有示例都创建了一个类,并使用如何在JPanel中绘制?(Swing/graphics Java),java,swing,jpanel,draw,paintcomponent,Java,Swing,Jpanel,Draw,Paintcomponent,我正在做一个项目,我想做一个绘画程序。 到目前为止,我已经使用Netbeans创建了GUI并设置了程序 到目前为止,我能够调用所有必要的协调来在里面画画,但我对如何在里面画画感到非常困惑 在代码的末尾,我尝试在面板内部绘制时失败了 有人能解释/演示如何在这样的示例中使用图形吗 我发现的所有示例都创建了一个类,并使用JPanel对其进行扩展,但我不知道是否可以这样做,因为它是在netbeans中生成的 我需要在JPanel中,在我的JFrame中绘制。我不知道把图形课放在哪里 JavaPaintU
JPanel
对其进行扩展,但我不知道是否可以这样做,因为它是在netbeans中生成的
我需要在JPanel
中,在我的JFrame
中绘制。我不知道把图形课放在哪里
JavaPaintUI类
包javapaint;
导入java.awt.*;
导入javax.swing.*;
公共类JavaPaintUI扩展了javax.swing.JFrame{
公共JavaPaintUI(){
初始化组件();
}
私有组件(){
jPanel2=newjavax.swing.JPanel();
setBackground(新java.awt.Color(255、255、255));
setboorder(javax.swing.BorderFactory.createBevelOrder(javax.swing.border.BevelOrder.RAISED));
addMouseListener(新java.awt.event.MouseAdapter(){
public void mousePressed(java.awt.event.MouseEvent evt){
JPANEL2MOUSE按下(evt);
}
public void mouseereleased(java.awt.event.MouseEvent evt){
jPanel2MouseReleased(evt);
}
});
jPanel2.addMouseMotionListener(新java.awt.event.MouseMotionAdapter(){
public void mouseDragged(java.awt.event.MouseEvent evt){
JPANEL2MOUSE(evt);
}
});
包装();
}//
int currentX,currentY,oldX,oldY;
私有void jpanel2mousedrable(java.awt.event.MouseEvent evt){
如果(工具==1){
currentX=evt.getX();
currentY=evt.getY();
oldX=当前x;
oldY=当前;
System.out.println(currentX+“”+currentY);
System.out.println(“笔!!!!”);
}
}
私有void jPanel2MousePressed(java.awt.event.MouseEvent evt){
oldX=evt.getX();
oldY=evt.getY();
System.out.println(oldX+“”+oldY);
}
//释放鼠标//
私有void jPanel2MouseReleased(java.awt.event.MouseEvent evt){
如果(工具==2){
currentX=evt.getX();
currentY=evt.getY();
System.out.println(“line!!!!从“+oldX+”到“+currentX”);
}
}
//将ui设置为可见//
公共静态void main(字符串参数[]){
invokeLater(new Runnable()){
公开募捐{
新建JavaPaintUI().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JPanel jPanel2;
//变量结束声明
类jPanel2扩展了JPanel{
@凌驾
公共组件(图形g){
超级组件(g);
g、 抽绳(“废话”,20,20);
g、 drawRect(200200200200200);
}
}
}
截屏
整个东西是一个JFrame
,中间的白色部分是jPanel2
,这就是我想要画的。
使用图形用户界面时,您需要记住,在窗格上绘图是在中完成的。不能仅在
paint()
/paintComponent()
/等方法之外使用Graphics
对象
但是,您可以使用名为“”的技术。基本上,您需要有一个缓冲区并直接在其上绘制(请参见它的
createGraphics()
方法;您可以在同一buffereImage
实例上保留并重用该图形上下文进行多个操作,无需一直重新创建它,仅在创建新实例时)。然后,在JPanel
的paintComponent()
中,只需将buffereImage
实例绘制到JPanel
。使用此技术,您可以非常轻松地执行缩放、平移和旋转操作。请注意额外的注释
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);
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
类JavaPaintUI扩展了JFrame{
专用int工具=1;
int currentX,currentY,oldX,oldY;
公共JavaPaintUI(){
初始化组件();
}
私有组件(){
//我们想要一个定制的Panel2,而不是通用的JPanel!
jPanel2=新面板2();
setBackground(新java.awt.Color(255、255、255));
jPanel2.setboorder(BorderFactory.createBevelOrder(BevelOrder.RAISED));
jPanel2.addMouseListener(新的MouseAdapter(){
公共无效鼠标按下(MouseEvent evt){
JPANEL2MOUSE按下(evt);
}
公共无效MouseEvent evt(MouseEvent evt){
jPanel2MouseReleased(evt);
}
});
addMouseMotionListener(新的MouseMotionAdapter(){
公共无效鼠标标记(鼠标事件evt){
JPANEL2MOUSE(evt);
}
});
//将组件添加到框架以查看它!
这个.setContentPane(jPanel2);
//善待测试人员。。
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
包装();
}//
私有void JPanel2MouseDrawed(MouseEvent evt){
如果(工具==1){
currentX=evt.getX();
currentY=evt.getY();
oldX=当前x;
oldY=当前;
System.out.println(currentX+“”+currentY);
System.out.println(“笔!!!!”);
}
}
私有无效jPanel2MousePressed(MouseEvent evt){
oldX=evt.getX();
oldY=evt.getY();
System.ou
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);
}
}
}
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);
}
}
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);
}
}
}