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临时图层。
当鼠标抬起时将两个图层合并就可以了。
Mineserver(ASN142586|UK CompanyNumber 1351696),已经成立一年半。主营香港日本机房的VPS、物理服务器业务。Telegram群组: @mineserver1 | Discord群组: https://discord.gg/MTB8ww9GEA7折循环优惠:JP30(JPCN2宣布产品可以使用)8折循环优惠:CMI20(仅1024M以上套餐可以使用)9折循...
欧路云 主要运行弹性云服务器,可自由定制配置,可选加拿大的480G超高防系列,也可以选择美国(200G高防)系列,也有速度直逼内地的香港CN2系列。所有配置都可以在下单的时候自行根据项目 需求来定制自由升级降级 (降级按天数配置费用 退款回预存款)。由专业人员提供一系列的技术支持!官方网站:https://www.oulucloud.com/云服务器(主机测评专属优惠)全场8折 优惠码:zhuji...
专心做抗投诉服务器的VirtVPS上线瑞士机房,看中的就是瑞士对隐私的保护,有需要欧洲抗投诉VPS的朋友不要错过了。VirtVPS这次上新的瑞士服务器采用E-2276G处理器,Windows/Linux操作系统可选。VirtVPS成立于2018年,主营荷兰、芬兰、德国、英国机房的离岸虚拟主机托管、VPS、独立服务器、游戏服务器和外汇服务器业务。VirtVPS 提供世界上最全面的安全、完全受保护和私...
java画图板为你推荐
易pc华硕易PC怎么样?性价比到底怎么样?安装程序配置服务器失败win10安装程序配置服务器失败怎么办什么是电子邮件 什么是电子邮件快速美白好方法有什么变白的好方法依赖注入什么是侵入性?还有依赖注入?网站联盟网盟跟b2b平台有什么区别今日热点怎么删除youku今日热点怎么卸载qq空间装扮qq空间怎么装扮硬盘人什么叫“软盘人”和“硬盘人”?网站优化方案网站优化方案如何写?
如何申请域名 vps代理 域名备案流程 lnmp 360抢票助手 免费ddos防火墙 云全民 国外免费全能空间 柚子舍官网 合租空间 admit的用法 100m空间 流量计费 metalink 360云服务 512mb 美国盐湖城 帽子云排名 双线空间 免费个人网页 更多