java初学者求教,目的是编写一个可以切换画笔颜色的直线绘画界面(在线等,急)
importjava.awt.event.*;importjava.awt.*;importjava.applet.*;importjava.util.Vector;cl...
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.Vector;
class DrawTest extends Applet implements ActionListener
{
static int Times = 0;
DrawPanel panel;
static Frame f = new Frame();//("DrawTest");
static DrawTest drawTest = new DrawTest();
static Button
Red_Button = new Button("Red") ,
Blue_Button = new Button("Blue") ,
Green_Button = new Button("Green"),
Black_Button = new Button("Black"),
Yellow_Button = new Button("Yellow"),
Gray_Button = new Button("Gray"),
Pink_Button = new Button("Pink"),
Orange_Button = new Button("Orange");
public void init()
{
setLayout(new BorderLayout());
panel = new DrawPanel();
add("Center",panel);
}
public static void main(String args[])
{
drawTest.init();
f.setTitle("Easy Paint");
f.add("Center",drawTest);
f.setSize(1024,720);
f.show();
f.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
f.add(Black_Button);
f.add(Red_Button);
f.add(Blue_Button);
f.add(Yellow_Button);
Black_Button.addActionListener(f);
Red_Button.addActionListener(f);
Blue_Button.addActionListener(f);
Yellow_Button.addActionListener(f);
}
public void actionPerformed(ActionEvent e)
{
Times++;
if( Times%3 == 0)f.setBackground(Color.white);
else if (Times%3 == 1) f.setBackground(Color.red);
else f.setBackground(Color.gray);
}
}
class DrawPanel extends Panel implements MouseListener , MouseMotionListener
{
Vector lines = new Vector();
int x1 , y1;
int x2 , y2;
public DrawPanel()
{
setBackground (Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void SetDrawMode(int mode){}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX();
y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
x1 = e.getX();
y1 = e.getY();
x2 = -1;
}
public void mouseReleased(MouseEvent e)
{
lines.addElement(new Rectangle(x1,y1,e.getX(),e.getY()));
x2 = -1;
repaint();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void paint(Graphics g)
{
int np = lines.size();
g.setColor(Color.black);
for ( int i = 0 ;i < np ; i ++ )
{
Rectangle p = (Rectangle)lines.elementAt(i);
g.drawLine(p.x,p.y,p.width,p.height);
}
if(x2!=-1)g.drawLine(x1,y1,x2,y2);
}
}
这是把裸的画图和按钮拼接起来的,下面是编译信息:
draw_ver1.0.java:50: 错误: 无法将类 Button中的方法 addActionListener应用到给定类
型;
Black_Button.addActionListener(f);
^
需要: ActionListener
找到: Frame
原因: 无法通过方法调用转换将实际参数Frame转换为ActionListener
请帮忙找出原因,并指出改正方法。 展开
import java.awt.*;
import java.applet.*;
import java.util.Vector;
class DrawTest extends Applet implements ActionListener
{
static int Times = 0;
DrawPanel panel;
static Frame f = new Frame();//("DrawTest");
static DrawTest drawTest = new DrawTest();
static Button
Red_Button = new Button("Red") ,
Blue_Button = new Button("Blue") ,
Green_Button = new Button("Green"),
Black_Button = new Button("Black"),
Yellow_Button = new Button("Yellow"),
Gray_Button = new Button("Gray"),
Pink_Button = new Button("Pink"),
Orange_Button = new Button("Orange");
public void init()
{
setLayout(new BorderLayout());
panel = new DrawPanel();
add("Center",panel);
}
public static void main(String args[])
{
drawTest.init();
f.setTitle("Easy Paint");
f.add("Center",drawTest);
f.setSize(1024,720);
f.show();
f.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
f.add(Black_Button);
f.add(Red_Button);
f.add(Blue_Button);
f.add(Yellow_Button);
Black_Button.addActionListener(f);
Red_Button.addActionListener(f);
Blue_Button.addActionListener(f);
Yellow_Button.addActionListener(f);
}
public void actionPerformed(ActionEvent e)
{
Times++;
if( Times%3 == 0)f.setBackground(Color.white);
else if (Times%3 == 1) f.setBackground(Color.red);
else f.setBackground(Color.gray);
}
}
class DrawPanel extends Panel implements MouseListener , MouseMotionListener
{
Vector lines = new Vector();
int x1 , y1;
int x2 , y2;
public DrawPanel()
{
setBackground (Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void SetDrawMode(int mode){}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX();
y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
x1 = e.getX();
y1 = e.getY();
x2 = -1;
}
public void mouseReleased(MouseEvent e)
{
lines.addElement(new Rectangle(x1,y1,e.getX(),e.getY()));
x2 = -1;
repaint();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void paint(Graphics g)
{
int np = lines.size();
g.setColor(Color.black);
for ( int i = 0 ;i < np ; i ++ )
{
Rectangle p = (Rectangle)lines.elementAt(i);
g.drawLine(p.x,p.y,p.width,p.height);
}
if(x2!=-1)g.drawLine(x1,y1,x2,y2);
}
}
这是把裸的画图和按钮拼接起来的,下面是编译信息:
draw_ver1.0.java:50: 错误: 无法将类 Button中的方法 addActionListener应用到给定类
型;
Black_Button.addActionListener(f);
^
需要: ActionListener
找到: Frame
原因: 无法通过方法调用转换将实际参数Frame转换为ActionListener
请帮忙找出原因,并指出改正方法。 展开
1个回答
展开全部
帮你改了一下,
1.Black_Button.addActionListener(f); 这个应该是添加侦听器类的实例.也就是实现ActionListener的类, DrawTest 类实现了ActionListener那这里
Black_Button.addActionListener(f);
改成
Black_Button.addActionListener(this);
2.还有就是你继承了,Applet 还要创建窗口做什么?直接继承Frame不就可以了吗.
3.你自己实现了一个PANEL类,那么设置背景色,就是panel.setBackground
大概就是这些,再一个你点击事件里,怎么来区分按钮我没看明白,就改了一下.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
public class DrawTest extends Frame implements ActionListener {
// static int Times = 0;
DrawPanel panel;
Button Red_Button = new Button("Red");
Button Blue_Button = new Button("Blue");
Button Green_Button = new Button("Green");
Button Black_Button = new Button("Black");
Button Yellow_Button = new Button("Yellow");
Button Gray_Button = new Button("Gray");
Button Pink_Button = new Button("Pink");
Button Orange_Button = new Button("Orange");
public DrawTest() {
setLayout(new BorderLayout());
panel = new DrawPanel();
add(BorderLayout.CENTER, panel);
setTitle("Easy Paint");
// add("Center", drawTest);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Black_Button.setActionCommand("BLACK");
Red_Button.setActionCommand("RED");
Blue_Button.setActionCommand("BLUE");
Yellow_Button.setActionCommand("YELLOW");
panel.add(Black_Button);
panel.add(Red_Button);
panel.add(Blue_Button);
panel.add(Yellow_Button);
Black_Button.addActionListener(this);
Red_Button.addActionListener(this);
Blue_Button.addActionListener(this);
Yellow_Button.addActionListener(this);
setSize(800, 600);
setVisible(true);
}
public static void main(String args[]) {
new DrawTest();
}
public void actionPerformed(ActionEvent e) {
// Times++;
if ("BLACK".equals(e.getActionCommand())) {
panel.setBackground(Color.black);
} else if ("RED".equals(e.getActionCommand())) {
panel.setBackground(Color.red);
} else if ("BLUE".equals(e.getActionCommand())) {
panel.setBackground(Color.blue);
} else if ("YELLOW".equals(e.getActionCommand())) {
panel.setBackground(Color.yellow);
}
repaint();
}
}
class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
Vector lines = new Vector();
int x1, y1;
int x2, y2;
public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void SetDrawMode(int mode) {
}
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
x2 = -1;
}
public void mouseReleased(MouseEvent e) {
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void paint(Graphics g) {
int np = lines.size();
g.setColor(Color.black);
for (int i = 0; i < np; i++) {
Rectangle p = (Rectangle) lines.elementAt(i);
g.drawLine(p.x, p.y, p.width, p.height);
}
if (x2 != -1)
g.drawLine(x1, y1, x2, y2);
}
}
1.Black_Button.addActionListener(f); 这个应该是添加侦听器类的实例.也就是实现ActionListener的类, DrawTest 类实现了ActionListener那这里
Black_Button.addActionListener(f);
改成
Black_Button.addActionListener(this);
2.还有就是你继承了,Applet 还要创建窗口做什么?直接继承Frame不就可以了吗.
3.你自己实现了一个PANEL类,那么设置背景色,就是panel.setBackground
大概就是这些,再一个你点击事件里,怎么来区分按钮我没看明白,就改了一下.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
public class DrawTest extends Frame implements ActionListener {
// static int Times = 0;
DrawPanel panel;
Button Red_Button = new Button("Red");
Button Blue_Button = new Button("Blue");
Button Green_Button = new Button("Green");
Button Black_Button = new Button("Black");
Button Yellow_Button = new Button("Yellow");
Button Gray_Button = new Button("Gray");
Button Pink_Button = new Button("Pink");
Button Orange_Button = new Button("Orange");
public DrawTest() {
setLayout(new BorderLayout());
panel = new DrawPanel();
add(BorderLayout.CENTER, panel);
setTitle("Easy Paint");
// add("Center", drawTest);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Black_Button.setActionCommand("BLACK");
Red_Button.setActionCommand("RED");
Blue_Button.setActionCommand("BLUE");
Yellow_Button.setActionCommand("YELLOW");
panel.add(Black_Button);
panel.add(Red_Button);
panel.add(Blue_Button);
panel.add(Yellow_Button);
Black_Button.addActionListener(this);
Red_Button.addActionListener(this);
Blue_Button.addActionListener(this);
Yellow_Button.addActionListener(this);
setSize(800, 600);
setVisible(true);
}
public static void main(String args[]) {
new DrawTest();
}
public void actionPerformed(ActionEvent e) {
// Times++;
if ("BLACK".equals(e.getActionCommand())) {
panel.setBackground(Color.black);
} else if ("RED".equals(e.getActionCommand())) {
panel.setBackground(Color.red);
} else if ("BLUE".equals(e.getActionCommand())) {
panel.setBackground(Color.blue);
} else if ("YELLOW".equals(e.getActionCommand())) {
panel.setBackground(Color.yellow);
}
repaint();
}
}
class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
Vector lines = new Vector();
int x1, y1;
int x2, y2;
public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void SetDrawMode(int mode) {
}
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
x2 = -1;
}
public void mouseReleased(MouseEvent e) {
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void paint(Graphics g) {
int np = lines.size();
g.setColor(Color.black);
for (int i = 0; i < np; i++) {
Rectangle p = (Rectangle) lines.elementAt(i);
g.drawLine(p.x, p.y, p.width, p.height);
}
if (x2 != -1)
g.drawLine(x1, y1, x2, y2);
}
}
追问
太感谢了,我就是不知道怎么区分接口。我的本意是希望能够通过点击按钮,来改变画笔的颜色,希望能予以指导。同时,也请推荐一点适合初学者自学的参考资料,谢谢了
追答
1.怎么区分接口?
用关键字 interface 字义的类,就是接口.
需要 implements 来引用接口,并实现接口中定义的方法.
public class DrawTest extends Frame implements ActionListener
这些你在书里都能看到.
这些说多了没有太大用,怎么说都是书里那些东西,关键在于怎么用,自己来理解着用.
2.改变画面笔的颜色.
g.setColor(Color.black);
这个就是改变画面的颜色,但你这里是个固定值,
你更改一下以下内容,这里不够贴代码了.
DrawPanel类中.添加public Color color = Color.black;
DrawTest类中改更以下内容
if ("BLACK".equals(e.getActionCommand())) {
panel.setBackground(Color.black);
} else if ("RED".equals(e.getActionCommand())) {
panel.setBackground(Color.red);
} else if ("BLUE".equals(e.getActionCommand())) {
panel.setBackground(Color.blue);
} else if ("YELLOW".equals(e.getActionCommand())) {
panel.setBackground(Color.yellow);
}
变与下边的内容
if ("BLACK".equals(e.getActionCommand())) {
panel.color = Color.black;
} else if ("RED".equals(e.getActionCommand())) {
panel.color = Color.red;
} else if ("BLUE".equals(e.getActionCommand())) {
panel.color = Color.blue;
} else if ("YELLOW".equals(e.getActionCommand())) {
panel.color = Color.yellow;
}
就可以达到你改变画笔颜色的目的,
不过会出现一个问题,就是你画过一个线,改变了颜色以后,原来画过的线,也会变颜色,
你的lines里最好是存放一个BEAN对象,除了坐标以外,还有颜色.
这样可以达到每个线是不同颜色的目的.
最后你说初学者的参考资料.
如果你还没有入门,那随便找一个入门教程就可以.不要看那一个都好,入门的书不用多,一本就够了.
看你能写类了,应该是入门了,那你就看JDK的API,熟悉类库.
很多知识都是能过做东西积累的,这个真的没有太好的办法介绍,你做的越多,知道的越多,就是这样.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询