当前位置: 主页 > 彩票资讯 >

4在Applet中画矩形,大小自定,边框红色,填充绿

发布时间:2020-02-12 | 来源:未知 | 作者:admin | 点击: 次 | 3d画
4在Applet中画矩形,大小自定,边框红色,填充绿色;画一个凸出3D矩形,大小、颜色自定。

class myapplet extends Applet{
public void init(){}
public void paint(Graphics g){
//定颜色
g.setColor(Color.red);
//画矩形
g.drawRect(0,0,100,100);
//画3D矩形
g.draw3DRect(110,0,100,100,true);
}
}

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

你把整个画布都填充了,这样不也等于填充了圆么?还有你的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();

吉彩网彩票预测专家(整理搜集)

Powered by http://www.jitajianpu.com © 2004-2009 DesDev Inc.
Copyright © 2002-2020 吉彩网彩票预测专家 版权所有