4、设计一个Applet程序,创建2个按钮和一个文本框,分别用来控制画红色和绿色的填充圆,文本框中输入半径

importjava.awt.*;importjava.awt.event.*;classmycanvasextendsCanvas{intx,y,r;mycanvas(... import java.awt.*;
import java.awt.event.*;
class mycanvas extends Canvas
{int x,y,r;
mycanvas()
{setBackground(Color.cyan);}
public void setX(int x){this.x=x;
}
public void setY(int y){this.y=y;
}
public void setR(int r){this.r=r;
}
public void paint(Graphics g)
{g.drawOval(x,y,2*r,2*r);
g.setColor(Color.gray);
}
}

class canvaswindow extends Frame implements ActionListener
{Button button1,button2,button3;
mycanvas canvas;
canvaswindow(String s)
{//setLayout(null);
button1=new Button("red");
button2=new Button("blue");
button3=new Button("green");
canvas=new mycanvas();
Panel panel=new Panel();
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
panel.add(button1);panel.add(button2);
panel.add(button3);
add(panel);
//panel.setBounds(0,0,50,50);
add(canvas);
//canvas.setBounds(0,50,300,200);
add(panel,BorderLayout.NORTH);
add(canvas,BorderLayout.CENTER);
setBounds(0,0,300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{//int x=100,y=50,r=30;
if(e.getSource()==button1)
{//Graphics g=getGraphics();
canvas.setX(100);
canvas.setY(50);
canvas.setR(30);
canvas.repaint();
canvas.setBackground(Color.red);
}
else if(e.getSource()==button2)
{Graphics g=getGraphics();
canvas.setX(100);
canvas.setY(50);
canvas.setR(50);
canvas.repaint();
canvas.setBackground(Color.blue);
}
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
}
}
public class hong
{public static void main (String args[])
{canvaswindow win=new canvaswindow("change the color");
}
}
这是我写的,画的圆没颜色
高手帮忙!
展开
 我来答
匿名用户
2009-07-05
展开全部
你把整个画布都填充了,这样不也等于填充了圆么?还有你的paint方法里面如果要填充圆应该是fillOval,但是在这之前又不设置颜色,在之后设置灰色是没用的。总而言之,不太明白你的意思,就根据题目需求自己写了一点,题目对坐标没有要求,所以我就画在正中间了,文本框是要按下回车才有作用

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class PaintingPanel extends JPanel {
private static final long serialVersionUID = -2398169663737549831L;
int radius;
Color color;

PaintingPanel(int radius) {
this.radius = radius;
}

public void paint(Graphics g) {
super.paint(g);
if (color != null) {
g.setColor(color);
g.fillOval((getWidth() >> 1) - radius, (getHeight() >> 1) - radius,
radius << 1, radius << 1);
}
}
}

public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
final PaintingPanel paintingPanel = new PaintingPanel(50);
paintingPanel.setPreferredSize(new Dimension(300, 300));
JButton button1 = new JButton("Red");
JButton button2 = new JButton("Green");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
paintingPanel.color = Color.RED;
paintingPanel.repaint();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
paintingPanel.color = Color.GREEN;
paintingPanel.repaint();
}
});
final JTextField field = new JTextField(5);
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
paintingPanel.radius = Integer.parseInt(field.getText());
paintingPanel.repaint();
} catch (NumberFormatException e) {
}
}
});
JPanel controlPanel = new JPanel();
controlPanel.add(button1);
controlPanel.add(button2);
controlPanel.add(field);
frame.add(paintingPanel, BorderLayout.CENTER);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式