java画笔默认颜色_java初学者求教,目的是编写一个可以切换画笔颜色的直线绘画界面(在线等,急)...

importjava.awt.event.*;importjava.awt.*;importjava.applet.*;importjava.util.Vector;classDrawTestextendsAppletimplementsActionListener{staticintTimes=0;DrawPanelpanel;stat...

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

请帮忙找出原因,并指出改正方法。

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以的。以下是 Java 创建一个实现 Turtle 图形 API 的类的代码示例: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JPanel; public class Turtle extends JFrame { // 画板 private DrawPanel drawPanel; // 乌龟的位置和方向 private Point position; private double direction; // 画笔颜色和粗细 private Color penColor; private int penWidth; /** * 构造函数 */ public Turtle(int width, int height, Color penColor, int penWidth) { setSize(width, height); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); drawPanel = new DrawPanel(); add(drawPanel); this.penColor = penColor; this.penWidth = penWidth; direction = 0; position = new Point(width / 2, height / 2); } /** * 将乌龟移动到指定位置 */ public void moveTo(int x, int y) { position.x = x; position.y = y; } /** * 设置画笔颜色 */ public void setPenColor(Color color) { this.penColor = color; } /** * 设置画笔粗细 */ public void setPenWidth(int width) { this.penWidth = width; } /** * 向前移动指定距离 */ public void forward(int distance) { Point newPosition = new Point( position.x + (int) Math.round(distance * Math.cos(Math.toRadians(direction))), position.y - (int) Math.round(distance * Math.sin(Math.toRadians(direction))) ); if (drawPanel.isDrawing()) { drawPanel.drawLine(position, newPosition, penColor, penWidth); } position = newPosition; } /** * 向左旋转指定角度 */ public void left(double angle) { direction += angle; } /** * 向右旋转指定角度 */ public void right(double angle) { direction -= angle; } private class DrawPanel extends JPanel { private boolean isDrawing; public DrawPanel() { isDrawing = true; } public boolean isDrawing() { return isDrawing; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); } public void drawLine(Point start, Point end, Color color, int width) { Graphics g = getGraphics(); g.setColor(color); g.fillRect(end.x, end.y, width, width); g.drawLine(start.x, start.y, end.x, end.y); } } } ``` 调用示例: ```java Turtle turtle = new Turtle(800, 600, Color.BLACK, 1); turtle.setVisible(true); turtle.moveTo(400, 300); turtle.forward(100); turtle.left(90); turtle.setPenColor(Color.RED); turtle.setPenWidth(5); turtle.forward(50); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值