java实现画图和清屏功能.
要求做一个界面,界面上有画图按钮和清屏按钮,单击画图按钮可以显示出一个圆,单击清屏功能可以将画出的圆清除掉.NOTE:希望给出详细的程序过程,并能够编译通过的....
要求做一个界面,界面上有画图按钮和清屏按钮,单击画图按钮可以显示出一个圆,单击清屏功能可以将画出的圆清除掉. NOTE:希望给出详细的程序过程,并能够编译通过的.
展开
2个回答
2013-05-31
展开全部
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class DrawTest extends JFrame { final DrawPanel pnlImg = new DrawPanel();
public DrawTest() {
init();
}
private void init() {
setLayout(new GridLayout(2, 1));
add(pnlImg);
final JButton btnDraw = new JButton("画图");
btnDraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pnlImg.setDrawRadius(200);
pnlImg.setDrawOval(true);
}
});
final JButton btnClear = new JButton("清屏");
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pnlImg.setDrawOval(false);
}
});
final JPanel pnlButton = new JPanel(new GridLayout(1, 2));
pnlButton.add(btnDraw);
pnlButton.add(btnClear);
add(pnlButton);
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args) throws Exception {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new DrawTest().setVisible(true);
}
});
}
class DrawPanel extends JPanel {
boolean flag;
int radius = 100;
public void setDrawOval(boolean flag) {
this.flag = flag;
repaint();
}
public void setDrawRadius(int radius) {
this.radius = radius;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Rectangle rect = getBounds();
if (flag && rect.width > radius && rect.height > radius) {
g.drawOval((rect.width - radius) / 2, (rect.height - radius) / 2, radius, radius);
}
}
}
}
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class DrawTest extends JFrame { final DrawPanel pnlImg = new DrawPanel();
public DrawTest() {
init();
}
private void init() {
setLayout(new GridLayout(2, 1));
add(pnlImg);
final JButton btnDraw = new JButton("画图");
btnDraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pnlImg.setDrawRadius(200);
pnlImg.setDrawOval(true);
}
});
final JButton btnClear = new JButton("清屏");
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pnlImg.setDrawOval(false);
}
});
final JPanel pnlButton = new JPanel(new GridLayout(1, 2));
pnlButton.add(btnDraw);
pnlButton.add(btnClear);
add(pnlButton);
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args) throws Exception {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new DrawTest().setVisible(true);
}
});
}
class DrawPanel extends JPanel {
boolean flag;
int radius = 100;
public void setDrawOval(boolean flag) {
this.flag = flag;
repaint();
}
public void setDrawRadius(int radius) {
this.radius = radius;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Rectangle rect = getBounds();
if (flag && rect.width > radius && rect.height > radius) {
g.drawOval((rect.width - radius) / 2, (rect.height - radius) / 2, radius, radius);
}
}
}
}
推荐于2018-05-15
展开全部
清屏很简单的,只要用背景色画一个和屏一样大的矩形就可以了。
另一方法: 清屏的办法在AWT和swing中可以这样实现:g.clearRect(0, 0, width, height); g 就是 Graphics 对象
另一方法: 清屏的办法在AWT和swing中可以这样实现:g.clearRect(0, 0, width, height); g 就是 Graphics 对象
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询