站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→简单的java程序问题

简单的java程序问题

发表新主题   回复此主题

第1楼 2007-05-26 06:10 titimax 写道:

简单的java程序问题

我是java的初学者,在学习过程中遇到了些问题,这里有个 小应用程序 想要增加个功能,设置一个选项,可以设置画笔的粗细,我想对高手而言是非常简单的事情,所以请帮帮忙!

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Lizi18_5 extends Applet
implements ActionListener,MouseMotionListener
{ int x=-1,y=-1,橡皮擦通知=0,清除通知=0;
Color c=new Color(255,0,0);
int con=3;
Button b_red,b_blue,b_green,清除,b_quit;
public void init()
{ addMouseMotionListener(this);
b_red=new Button("画红色图形");
b_blue=new Button("兰色图形");
b_green=new Button("画绿色图形");
b_quit=new Button("橡皮");
清除=new Button("清除");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit);
add(清除);
b_red.addActionListener(this);
b_green.addActionListener(this);
b_blue.addActionListener(this);
b_quit.addActionListener(this);
清除.addActionListener(this);
}
public void paint(Graphics g)
{ if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0)
{ g.setColor(c);
g.fillOval(x,y,con,con);
}
else if(橡皮擦通知==1&&清除通知==0)
{ g.clearRect(x,y,10,10);
}
else if(清除通知==1&&橡皮擦通知==0)
{ g.clearRect(0,0,getSize().width,getSize().height);
}
}
public void mouseDragged(MouseEvent e)
{ x=(int)e.getX();
y=(int)e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){ }
public void update(Graphics g)
{ paint(g);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b_red)
{ 橡皮擦通知=0;清除通知=0; c=new Color(255,0,0);
}
else if(e.getSource()==b_green)
{ 橡皮擦通知=0;清除通知=0; c=new Color(0,255,0);
}
else if(e.getSource()==b_blue)
{ 橡皮擦通知=0;清除通知=0; c=new Color(0,0,255);
}
if(e.getSource()==b_quit)
{ 橡皮擦通知=1;清除通知=0 ;
}
if(e.getSource()==清除)
{ 清除通知=1; 橡皮擦通知=0;repaint();
}
}
}

第2楼 2013-08-31 12:44 Robot :

简单的java程序问题 相关


第3楼 2007-05-26 12:34 牵牛 写道:

用2D吧
Grahpics2D g2D = (Graphics2D) g;
g2D.setStroke(...); 设粗细
g2D.setRenderingHints(...);
g2D.setFont(...);
....
还有很多,可以查阅文档
说明: 参阅csdn社区 还有很多知识 很好的一个编程网站!

第4楼 2007-05-28 18:26 Kimmy★Piggy 写道:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Lizi18_5 extends Applet implements ActionListener, MouseMotionListener {
int x = -1, y = -1, 橡皮擦通知 = 0, 清除通知 = 0;

Color c = new Color(255, 0, 0);

int con = 3;

Button b_red, b_blue, b_green, 清除, b_quit, inc, dec;

public void init() {
addMouseMotionListener(this);
b_red = new Button("画红色图形");
b_blue = new Button("兰色图形");
b_green = new Button("画绿色图形");
b_quit = new Button("橡皮");
清除 = new Button("清除");
inc = new Button("加粗");
dec = new Button("变细");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit);
add(清除);
add(inc);
add(dec);
b_red.addActionListener(this);
b_green.addActionListener(this);
b_blue.addActionListener(this);
b_quit.addActionListener(this);
清除.addActionListener(this);
inc.addActionListener(this);
dec.addActionListener(this);
}

public void paint(Graphics g) {
if (x != -1 && y != -1 && 橡皮擦通知 == 0 && 清除通知 == 0) {
g.setColor(c);
g.fillOval(x, y, con, con);
} else if (橡皮擦通知 == 1 && 清除通知 == 0) {
g.clearRect(x, y, 10, 10);
} else if (清除通知 == 1 && 橡皮擦通知 == 0) {
g.clearRect(0, 0, getSize().width, getSize().height);
}
}

public void mouseDragged(MouseEvent e) {
x = (int) e.getX();
y = (int) e.getY();
repaint();
}

public void mouseMoved(MouseEvent e) {
}

public void update(Graphics g) {
paint(g);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == b_red) {
橡皮擦通知 = 0;
清除通知 = 0;
c = new Color(255, 0, 0);
} else if (e.getSource() == b_green) {
橡皮擦通知 = 0;
清除通知 = 0;
c = new Color(0, 255, 0);
} else if (e.getSource() == b_blue) {
橡皮擦通知 = 0;
清除通知 = 0;
c = new Color(0, 0, 255);
}
if (e.getSource() == b_quit) {
橡皮擦通知 = 1;
清除通知 = 0;
}
if (e.getSource() == 清除) {
清除通知 = 1;
橡皮擦通知 = 0;
repaint();
}
if (e.getSource() == inc) {
con += 2;
if(con == 19)
inc.setEnabled(false);
dec.setEnabled(true);
}
if (e.getSource() == dec) {
con -= 2;
if(con == 1)
dec.setEnabled(false);
inc.setEnabled(true);
}
}
}

在你的代码基础上改了下,增加了“加粗”和“变细”按钮来控制画笔的粗细,应该可以满足你的要求~

发表新主题   回复此主题