java画图板关于java实现画图板的问题: 画图板的边画图,图形就能显示出来的功能怎么实现?

java画图板  时间:2021-01-16  阅读:()

java编制一个具有如下界面的画板, 能进行基本图形的绘制。

package bdzhidao; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DrawingBoard extends JFrame{ int[] xPoint={200,300,500}; int[] yPoint={100,180,240}; static int m=15; static Color color=new Color(255,0,0); private JComboBox jcb2=new JComboBox(new Object[]{ " 20"," 28"," 36"," 44"," 52"," 72 " }); private JComboBox jcb3=new JComboBox(new Object[]{ "红色","黑色","蓝色","绿色" }); public DrawingBoard(){ JMenuBar Bar=new JMenuBar(); JMenu jmFile=new JMenu("文件"); JMenuItem jmi1=new JMenuItem("新建"); JMenuItem jmi2=new JMenuItem("打开"); JMenuItem jmi3=new JMenuItem("保存"); JMenuItem jmi4=new JMenuItem("退出"); jmFile.add(jmi1); jmFile.add(jmi2); jmFile.add(jmi3); jmFile.add(jmi4); JLabel jlb1=new JLabel("线条"); JLabel jlb2=new JLabel("线宽"); JLabel jlb3=new JLabel("颜色"); final JComboBox jcb1=new JComboBox(new Object[]{ "实线","虚线" }); JButton jbt0=new JButton("直线"); JButton jbt1=new JButton("三角形"); JButton jbt2=new JButton("圆"); JButton jbt3=new JButton("矩形"); JPanel jp1=new JPanel(); jp1.add(jlb1); jp1.add(jcb1); jp1.add(jlb2); jp1.add(jcb2); jp1.add(jlb3); jp1.add(jcb3); jp1.add(jbt0); jp1.add(jbt1); jp1.add(jbt2); jp1.add(jbt3); final DrawingPanel jp2=new DrawingPanel(); jp2.setBackground(Color.white); jp2.setPreferredSize(new Dimension(736,300)); this.add(Bar); this.add(jp1,BorderLayout.CENTER); this.add(jp2,BorderLayout.SOUTH); jcb1.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ if(jcb1.getSelectedItem()=="实线"){ Graphics g=jp2.getGraphics(); g.drawLine(10,10,736,10); } if(jcb1.getSelectedItem()=="虚线"){ Graphics g=jp2.getGraphics(); for(int i=1;i<100;i++){ g.drawLine(6*i-6,50,6*i-3,50); } } } }); jcb2.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ if(jcb2.getSelectedItem()==" 20"){ m=20; repaint(); } if(jcb2.getSelectedItem()==" 28"){ m=28;repaint(); } if(jcb2.getSelectedItem()==" 36"){ m=36;repaint(); } if(jcb2.getSelectedItem()==" 44"){ m=44;repaint(); } if(jcb2.getSelectedItem()==" 52"){ m=52;repaint(); } if(jcb2.getSelectedItem()==" 72 "){ m=72;repaint(); } } }); jcb3.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ if(jcb3.getSelectedItem()=="红色"){ color=new Color(255,0,0); repaint(); } if(jcb3.getSelectedItem()=="蓝色"){ color=new Color(0,0,255); repaint(); } if(jcb3.getSelectedItem()=="黑色"){ color=new Color(0,0,0); repaint(); } if(jcb3.getSelectedItem()=="绿色"){ color=new Color(0,128,0); repaint(); } } }); jbt0.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Graphics g=jp2.getGraphics(); g.drawLine(10,10,736,10); } }); jbt1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Graphics grap=jp2.getGraphics();//创建所画组件对象 grap.drawPolygon(xPoint,yPoint,3); } }); jbt2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Graphics gr=jp2.getGraphics(); gr.drawOval(100,10,200,200); } }); jbt3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Graphics G= jp2.getGraphics(); G.drawRect(500,50,200,200); } }); } public static void main(String[] args){ JFrame frame=new DrawingBoard(); frame.setTitle("测试画图板"); frame.setSize(736,379); frame.setLocation(100,100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } static class DrawingPanel extends JPanel{ protected void paintComponent(Graphics g){ super.paintComponent(g); g.setFont(new Font("宋体",Font.BOLD,m)); g.setColor(color); g.drawString("Heleoeo ",100, 100); } } } 有一些问题还没有解决,因为重画,所以字符串可以重画,别的我没有重画,你自己可以修改一下, 还有一个问题,你的菜单条上的功能我暂时不会写,所以没有加上去, 希望对你有帮助,一起努力吧!!!

JAVA做WINDOWS画图板的源程序?

楼上的代码我做了一点点改正,我想现在可以正常运行了: //#8.15 //#filename: ShapeMain.java import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; //import javax.swing.Component; public class ShapeMain extends JFrame implements ActionListener,MouseListener,MouseMotionListener{ int x,y,x1,y1,x2,y2,width,height; boolean isFirstPoint = true; //初始化开始画的是线 int drawType = PaintingGround.LINE; //初始化开始不是填充 boolean isFill = false; //添加控件 ButtonGroup btg = new ButtonGroup(); Button btLine = new Button("线"); Button btRectangle = new Button("矩形"); Button btRound = new Button("圆"); Button btEllipse = new Button("椭圆"); Button tbFillState = new Button("填充"); Button button3 = new Button("文本操作"); Button button2 = new Button("清除"); Button button1 = new Button("选择颜色"); Panel buttonPanel = new Panel(); PaintingGround paintingGround = new PaintingGround(); //Main Method public static void main(String[] args) { //设置显示外观 try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) { e.printStackTrace(); } new ShapeMain(); } //构造函数 public ShapeMain() { //控件添加到控件组中 // btg.add(btLine); // btg.add(btRectangle); // btg.add(btRound); // btg.add(btEllipse); buttonPanel.add(btLine); buttonPanel.add(btRectangle); buttonPanel.add(btRound); buttonPanel.add(btEllipse); buttonPanel.add(tbFillState); //设置容器及容器的整体布局 Container cp = this; cp.setLayout(new BorderLayout()); cp.add(BorderLayout.NORTH,buttonPanel); cp.add(BorderLayout.CENTER,paintingGround); //cp.add(BorderLayout.SOUTH,jf); //jf.setJMenuBar(mb); setLocation(300,150); setSize(600,480); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); //添加鼠标触发事件 paintingGround.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent evn) { isFirstPoint = true; } }); //对鼠标的输入进行判断并调用画图程序 paintingGround.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evn) { if(isFirstPoint) { x1 = evn.getX(); y1 = evn.getY(); isFirstPoint = false; } else { x2 = evn.getX(); y2 = evn.getY(); switch(drawType) { case PaintingGround.LINE: //画线 paintingGround.drawLine(x1,y1,x2,y2); break; case PaintingGround.RECTANGLE: //画矫形 paintingGround.drawRect(x1,y1,x2-x1,y2-y1); break; case PaintingGround.ROUND: //画圆 //两点距离公式 int size = Math.abs((int)Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))); paintingGround.drawRound(x1,y1,size); break; case PaintingGround.ELLIPSE: //画椭圆 paintingGround.drawEllipse(x1,y1,x2-x1,y2-y1); break; default: break; } } } }); //各个控件的触发事件 btLine.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evn) { drawType = PaintingGround.LINE; } }); btRectangle.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evn) { drawType = PaintingGround.RECTANGLE; } }); btRound.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evn) { drawType = PaintingGround.ROUND; } }); btEllipse.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evn) { drawType = PaintingGround.ELLIPSE; } }); tbFillState.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evn) { isFill = tbFillState.isShowing(); paintingGround.setFillState(isFill); } }); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } } class PaintingGround extends JPanel { public static final int LINE = 1; public static final int RECTANGLE = 2; public static final int ROUND = 3; public static final int ELLIPSE = 4; private int x,y; private int x1,y1,x2,y2; private int width, height,size; private int drawType = 0; private boolean isFill = false; //构造函数 public PaintingGround() { setBackground(Color.black); } //判断是用实心还是空心的, public void paint(Graphics g) { super.paint(g); g.setColor(Color.white); if(isFill) { switch(drawType) { case LINE: g.drawLine(x1,y1,x2,y2); break; case RECTANGLE: g.fillRect(x,y,width,height); break; case ROUND: g.fillOval(x,y,size,size); break; case ELLIPSE: g.fillOval(x,y,width,height); break; default: break; } } else { switch(drawType) { case LINE: g.drawLine(x1,y1,x2,y2); break; case RECTANGLE: g.drawRect(x,y,width,height); break; case ROUND: g.drawOval(x,y,size,size); break; case ELLIPSE: g.drawOval(x,y,width,height); break; default: break; } } } public void drawLine(int x1, int y1, int x2,int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; drawType = LINE; repaint(); } //具体的实现方式 public void drawRect(int x,int y,int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; drawType = RECTANGLE; repaint(); } public void drawRound(int x,int y,int size) { this.x = x; this.y = y; this.size = size; drawType = ROUND; repaint(); } public void drawEllipse(int x,int y,int width,int height) { this.x = x; this.y = y; this.width = width; this.height = height; drawType = ELLIPSE; repaint(); } public void setFillState(boolean isFill) { this.isFill = isFill; } }

关于java实现画图板的问题: 画图板的边画图,图形就能显示出来的功能怎么实现?

你的问题有个不够清晰的地方。

当你鼠标按下时是否就影响了当前图像的内容?还是在鼠标抬起后才影响图像?举个例子:画笔在FreeDraw的时候按下鼠标既改变了当前图画,无论你合适抬起鼠标被覆盖的部分已经确定了。

而当你要插入一张图片的时候只有在调整完插入图片的大小和位置后才会改变当前图画。

如果你的需求是第一种,那么很简单判断鼠标按下开始画,鼠标抬起结束就可以了。

如果你的需求是第二种,那么可以使用图层的概念。

鼠标按下时建立一个临时图层的Image,并在这个临时图层上画,在画布刷新时,先draw正式图层,在draw临时图层。

当鼠标抬起时将两个图层合并就可以了。

GeorgeDatacenter:洛杉矶/达拉斯/芝加哥/纽约vps云服务器;2核/8GB/250GB/2TB流量/1Gbps端口,$84/年

georgedatacenter怎么样?GeorgeDatacenter是一家2017年成立的美国商家,正规注册公司(REG: 10327625611),其实是oneman。现在有优惠,有几款特价VPS,基于Vmware。支持Paypal付款。GeorgeDatacenter目前推出的一款美国vps,2核/8GB内存/250GB NVMe空间/2TB流量/1Gbps端口/Vmware/洛杉矶/达拉...

Sharktech($49/月),10G端口 32GB内存,鲨鱼机房新用户赠送$50

Sharktech 鲨鱼机房商家我们是不是算比较熟悉的,因为有很多的服务商渠道的高防服务器都是拿他们家的机器然后部署高防VPS主机的,不过这几年Sharktech商家有自己直接销售云服务器产品,比如看到有新增公有云主机有促销活动,一般有人可能买回去自己搭建虚拟主机拆分销售的,有的也是自用的。有看到不少网友在分享到鲨鱼机房商家促销活动期间,有赠送开通公有云主机$50,可以购买最低配置的,$49/月的...

创梦网络-四川大带宽、镇江电信服务器云服务器低至56元

达州创梦网络怎么样,达州创梦网络公司位于四川省达州市,属于四川本地企业,资质齐全,IDC/ISP均有,从创梦网络这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,一手四川托管服务商,成都优化线路,机柜租用、服务器云服务器租用,适合建站做游戏,不须要在套CDN,全国访问快,直连省骨干,大网封UDP,无视UDP攻击,机房集群高达1.2TB,单机可提供1...

java画图板为你推荐
网页解密急急急~~谁知道怎么让所有的网页都设密码?以及破解的办法啊? 谢谢了谁帮帮我啊赵雨润情人节“我们约会吧”电影DVD_我们约会吧高清DVD下载_qvod快播??淘宝收费淘宝网的收费项目有哪些人人时光机现在世界上有时光机吗公章制作在WOLD里怎样制作公章公章制作如何用photoshop制作公章公章制作如何制作公章网站运营网络运营主管的主要工作职责是什么?显卡温度多少正常显卡温度是多少才算正常的?安卓应用平台现在android平台的手机都有哪些?
ipage openv cpanel主机 info域名 evssl 卡巴斯基官方免费版 双拼域名 100m空间 微软服务器操作系统 爱奇艺会员免费试用 后门 免备案cdn加速 netvigator 腾讯云平台 美国主机侦探 cloudflare linuxvi 大硬盘分区 免费论坛空间 100m空间多少钱 更多