「java鼠标画笔」java画笔类
今天给各位分享java鼠标画笔的知识,其中也会对java画笔类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用JAVA做个画笔,有画笔和橡皮功能就行。越简单越好
- 2、java 鼠标画图??
- 3、java 绘图板 如何实现画直线的功能
- 4、java图形界面 运行时如何在点击鼠标时 改变字体颜色或者大小
- 5、java画图板画笔粗细的问题,急~~~~~高手速来帮忙改改!
用JAVA做个画笔,有画笔和橡皮功能就行。越简单越好
新建一个java文件,取名叫做PaintJava.java
代码如下:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
this.x = x;
this.y = y;
this.col = col;
this.tool = tool;
this.boarder = boarder;
}
}
class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小
int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志
Vector paintInfo = null;//点信息向量组
int n = 1;
FileInputStream picIn = null;
FileOutputStream picOut = null;
ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;
// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;
paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintInfo = new Vector();
/*各工具按钮及选择项*/
//颜色选择
ColChoice = new Choice();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoice.add("5");
EraserChoice.add("9");
EraserChoice.add("13");
EraserChoice.add("17");
EraserChoice.addItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();
clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
clear.addActionListener(this);
eraser.addActionListener(this);
pen.addActionListener(this);
drLine.addActionListener(this);
drCircle.addActionListener(this);
drRect.addActionListener(this);
openPic.addActionListener(this);
savePic.addActionListener(this);
colchooser.addActionListener(this);
颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);
toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);
toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);
toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);
savePicture.setVisible(false);
openPicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicture.setVisible(false); }
});
savePicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicture.setVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfo.size();
if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; in ;i++){
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)paintInfo.elementAt(i+1);
size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setColor(p1.col);
g2d.setStroke(size);
if(p1.tool==p2.tool)
{
switch(p1.tool)
{
case 0://画笔
Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;
case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
case 6://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
String name = ColChoice.getSelectedItem();
if(name=="black")
{c = new Color(0,0,0); }
else if(name=="red")
{c = new Color(255,0,0);}
else if(name=="green")
{c = new Color(0,255,0);}
else if(name=="blue")
{c = new Color(0,0,255);}
}
else if(e.getSource()==SizeChoice)//画笔大小
{
String selected = SizeChoice.getSelectedItem();
if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="5")
{con = 5;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="7")
{con = 7;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="9")
{con = 9;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
}
else if(e.getSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoice.getSelectedItem();
if(Esize=="5")
{ Econ = 5*2; }
else if(Esize=="9")
{ Econ = 9*2; }
else if(Esize=="13")
{ Econ = 13*2; }
else if(Esize=="17")
{ Econ = 17*3; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;
case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(p1);
repaint();
break;
default :
}
}
public void mouseMoved(MouseEvent e) {}
public void update(Graphics g)
{
paint(g);
}
public void mousePressed(MouseEvent e)
{
Point p2;
switch(toolFlag){
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfo.addElement(cutflag);
break;
case 1: //eraser
paintInfo.addElement(cutflag);
break;
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
default:
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==pen)//画笔
{toolFlag = 0;}
if(e.getSource()==eraser)//橡皮
{toolFlag = 1;}
if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}
if(e.getSource()==drLine)//画线
{toolFlag = 3;}
if(e.getSource()==drCircle)//画圆
{toolFlag = 4;}
if(e.getSource()==drRect)//画矩形
{toolFlag = 5;}
if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}
if(e.getSource()==openPic)//打开图画
{
openPicture.setVisible(true);
if(openPicture.getFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInfo.removeAllElements();
File filein = new File(openPicture.getDirectory(),openPicture.getFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VIn.readObject();
VIn.close();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read file");
}
}
}
if(e.getSource()==savePic)//保存图画
{
savePicture.setVisible(true);
try{
File fileout = new File(savePicture.getDirectory(),savePicture.getFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOut.writeObject(paintInfo);
VOut.close();
}
catch(IOException IOe)
{
System.out.println("can not write object");
}
}
}
}//end paintboard
public class PaintJava
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
累啊..给分吧...!!
java 鼠标画图??
上学的时候写过,不过不在公司的电脑里面。呵呵,回去想到了给你……
package guitest.myboard;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
//the point
//impress the info of one point,the x and y
class OnePoint implements Serializable {
int x;
int y;
int tool;
Color c;
int border;
public OnePoint(int x,int y,int tool,Color cc,int border){
this.x=x;
this.y=y;
this.tool=tool;
this.c=cc;
this.border=border;
}
}
class DrawingBoard extends Frame implements MouseListener,ItemListener,ActionListener,MouseMotionListener{
Button pen;
Button line ;
Button ellipse ;
Button rect ;
Button clear ;
Button colorboard ;
Button storebutton;
Button openbutton;
Choice sizechoice ;
Choice colorchoice ;
Label pensize;
Label pencolor;
Panel panel ;
FileDialog storefile;
FileDialog openfile;
FileInputStream filein;
FileOutputStream fileout;
ObjectInputStream objectin;
ObjectOutputStream objectout;
int flagtool=0;
Color flagcolor;
int border;
BasicStroke size;
OnePoint p1,p2;
VectorOnePoint points=new VectorOnePoint();
public DrawingBoard(){
pen=new Button("画笔");
line=new Button("直线");
ellipse=new Button("圆");
rect=new Button("矩形");
clear=new Button("清除");
colorboard=new Button("调色板");
storebutton=new Button("存储文件");
openbutton=new Button("打开文件");
pensize=new Label("画笔大小");
pencolor=new Label("画笔颜色");
storefile=new FileDialog(this,"存储文件",FileDialog.SAVE);
storefile.setVisible(false);
storefile.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
storefile.setVisible(false);
}
});
openfile=new FileDialog(this,"打开文件",FileDialog.LOAD);
openfile.setVisible(false);
openfile.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
openfile.setVisible(false);
}
});
sizechoice=new Choice();
sizechoice.add("1");
sizechoice.add("2");
sizechoice.add("4");
sizechoice.add("6");
sizechoice.add("8");
sizechoice.addItemListener(this);
colorchoice=new Choice();
colorchoice.add("black");
colorchoice.add("red");
colorchoice.add("blue");
colorchoice.add("green");
colorchoice.addItemListener(this);
pen.addActionListener(this);
line.addActionListener(this);
ellipse.addActionListener(this);
rect.addActionListener(this);
clear.addActionListener(this);
colorboard.addActionListener(this);
storebutton.addActionListener(this);
openbutton.addActionListener(this);
panel=new Panel();
panel.add(storebutton);
panel.add(openbutton);
panel.add(pen);
panel.add(line);
panel.add(ellipse);
panel.add(rect);
panel.add(clear);
panel.add(sizechoice);
panel.add(pensize);
panel.add(colorchoice);
panel.add(pencolor);
panel.add(colorboard);
add(panel,BorderLayout.NORTH);
setBounds(100,100,700,600);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
/**
* 添加鼠标事件的监听器,否则,鼠标的移动和点击都将无法识别!
* */
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
if(flagtool==2){ //qing chu
g.clearRect(0,0,getSize().width,getSize().height);
}
for(int i=0;ipoints.size()-1;i++){
p1=(OnePoint)points.elementAt(i);
p2=(OnePoint)points.elementAt(i+1);
g2d.setColor(p1.c); //////////////需要使用Graphics2D从Graphics类中继承下来的方法 setColor()设置当前的颜色
size=new BasicStroke(p1.border,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setStroke(size);
if(p1.tool==p2.tool){
switch(p1.tool){
case 0:
Line2D.Double line1=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);
g2d.draw(line1);
break;
case 1:
Line2D.Double line2=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);
g2d.draw(line2);
break;
case 3:
Ellipse2D.Double ellipse=new Ellipse2D.Double(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;
case 4:
Rectangle2D.Double rect=new Rectangle2D.Double(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
default:
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) { //鼠标点下时候,将当前的点信息记录
OnePoint pp1=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp1);
//repaint();
}
public void mouseReleased(MouseEvent e) {//鼠标松开时候,如果是画笔,则当前截断,是其余状态记下一枚点信息
if(flagtool==0){
points.addElement(new OnePoint(-1,-1,22,flagcolor,border));
}
else{
OnePoint pp2=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp2);
points.add(new OnePoint(-1,-1,22,flagcolor,border));
}
repaint();
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==colorchoice){
String selected=colorchoice.getSelectedItem();
if(selected=="black"){
flagcolor=new Color(0,0,0);
}
else if(selected=="red"){
flagcolor=new Color(255,0,0);
}
else if(selected=="blue"){
flagcolor=new Color(0,0,255);
}
else if(selected=="green"){
flagcolor=new Color(0,255,0);
}
}
else if(e.getSource()==sizechoice){
String selected=sizechoice.getSelectedItem();
if (selected=="1"){
border=1;
}
else if(selected=="2"){
border=2*2;
}
else if(selected=="4"){
border=4*2;
}
else if(selected=="6"){
border=6*2;
}
else if(selected=="8"){
border=8*2;
}
}
}
public void update(Graphics g) { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
paint(g);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==pen){
flagtool=0;
}
else if(e.getSource()==line){
flagtool=1;
}
else if(e.getSource()==clear){
flagtool=2;
points.removeAllElements();
repaint(); //此语要有,否则今生无法删除!
}
else if(e.getSource()==ellipse){
flagtool=3;
}
else if(e.getSource()==rect){
flagtool=4;
}
else if(e.getSource()==colorboard){
/*
* 使用 javax.swing.×包中的 JColorChooser 类的静态方法showDialog(Component component,String title,Color color ),
* 该方法的参数,component是当前显示对话框的父框架,color是设置调色板初始的被选颜色
*
* 该方法返回被选的颜色,类型为Color
* */
Color color=JColorChooser.showDialog(this, "调色板",flagcolor);
flagcolor=color;
}
else if(e.getSource()==openbutton){
openfile.setVisible(true);
if(openfile.getFile()!=null){
int temp=flagtool;
flagtool=2;
repaint();
try{
points.removeAllElements();
File file=new File(openfile.getDirectory(),openfile.getFile());
filein=new FileInputStream(file);
objectin=new ObjectInputStream(filein);
points=(Vector)objectin.readObject();
objectin.close();
filein.close();
flagtool=temp;
repaint();
}
catch(Exception ee){
System.out.println(ee.toString());
}
}
}
else if(e.getSource()==storebutton){
storefile.setVisible(true);
if(storefile.getFile()!=null){
try {
File file=new File(storefile.getDirectory(),storefile.getFile());
fileout=new FileOutputStream(file);
objectout=new ObjectOutputStream(fileout);
objectout.writeObject(points);
objectout.close();
fileout.close();
repaint();
}
catch (FileNotFoundException e1) {
System.out.println(e1.toString());
e1.printStackTrace();
} catch (IOException ee) {
System.out.println(ee.toString());
ee.printStackTrace();
}
}
}
}
public void mouseDragged(MouseEvent e) {//鼠标拖动时候,//当且仅当 flagtool==0,或者表示为橡皮的时候
//才将拖动过程中涉及到的点全部记录下来,并且调用repain()方法,重画当前
// TODO Auto-generated method stub
if(flagtool==0){
OnePoint pp3=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp3);
repaint();
}
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public class PaintBoard{
public static void main(String[] args){
DrawingBoard oneBorder=new DrawingBoard();
}
}
java 绘图板 如何实现画直线的功能
这个简单
可以调用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分别为直线起点和终点的坐标
特意给你写了个小例子,希望能帮到你
***************************************
import javax.swing.*;
import java.awt.*;
public class DrawLine extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("DrawLine");
frame.getContentPane().add(new DrawLine());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 50, 200, 250);
}
}
****************************
将程序复制到记事本中并改名为DrawLine.java(注意大小写)
然后在命令行中用 CD+文件夹 使命令范围到DrawLine.java的文件夹中
然后执行命令javac DrawLine.java
再执行命令java DrawLine
你会看到结果
java图形界面 运行时如何在点击鼠标时 改变字体颜色或者大小
首先,必须有一个GUI的类,让它继承JPanel类(javax.swing.JPanel);
让这个类实现MouseListener接口(java.awt.event.MouseListener),并实现此接口的所有方法;
其中有一个mouseClicked(MouseEvent e)方法,就是当该JPanel被鼠标点击时所调用的方法;在此方法中写上修改画笔颜色、字体的代码;
最后,由于继承了JPanel类,故可以重写paint(Graphics g)方法,在此方法中加入一些绘制方法。
示例代码如下:
package com;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JPanel implements MouseListener {
private JFrame f;
private Color color;
private Font font;
//为了方便,可以增加一个状态变量
private int state=0;
public Client(){
f=new JFrame("Test");
//此句修改JPanel大小
setPreferredSize(new Dimension(400,300));
setBackground(Color.white);
f.add(this);
//此句让JFrame适应JPanel大小
f.pack();
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//此句为JPanel对象设置了鼠标事件监听器(MouseListener)
addMouseListener(this);
color=Color.black;
font=new Font("Arial",Font.PLAIN,12);
f.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
//这2行代码根据成员变量color与font设置了画笔的颜色与字体,可由外部方法更改
g.setColor(color);
g.setFont(font);
//以下为自定义的绘制操作
g.fillRect(50,50,60,45);
g.drawString("Hello this is the test text",50,120);
}
public static void main(String[] args){
Client c=new Client();
//此为循环重绘的代码
while(true){
try{
//每40毫秒(25分之1秒)重绘一次
Thread.sleep(40);
}catch(Exception e){
e.printStackTrace();
}
//调用重绘方法
c.repaint();
}
}
public void stateChanged(){
switch(state){
case 0://状态0
color=Color.black;
font=new Font("Arial",Font.PLAIN,12);
break;
case 1://状态1
color=Color.blue;
font=new Font("Century Gothic",Font.BOLD,12);
break;
default://此为无效状态
}
}
public void mouseClicked(MouseEvent e) {
//此方法在鼠标点击JPanel时自动调用
//此句规定了成员变量state的2种状态,即0,1
state=state=1?0:state+1;
//下面的方法修改了成员变量color和font,从而实现画笔颜色与字体的变更
stateChanged();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
效果:
如果想要让不同的部分绘制成不同的颜色或字体,可在成员变量中增加更多的类似于color与font的对象,仿照上述代码调用。
java画图板画笔粗细的问题,急~~~~~高手速来帮忙改改!
你可以另外写一个方法,鼠标点击时触发那个方法,该方法首先获取鼠标点击的那个点,然后根据那个坐标在它的四周画图就可以了(可以以那个点为圆心画圆并填充,这样一条直线下去,就实现了加粗)。像获取点的坐标,画圆什么的方法,API里都有。
java鼠标画笔的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java画笔类、java鼠标画笔的信息别忘了在本站进行查找喔。