JAVA 编程 “画圆小程序”
我想要一个程序,程序时,填入圆的直径,提交后便会出现直径相符的圆。请不要搞得太复杂,我是一个JAVA初学者。诚挚谢谢~!...
我想要一个程序,程序时,填入圆的直径,提交后便会出现直径相符的圆。
请不要搞得太复杂,我是一个JAVA初学者。
诚挚谢谢~! 展开
请不要搞得太复杂,我是一个JAVA初学者。
诚挚谢谢~! 展开
2个回答
展开全部
import java.awt.*;
public class DrawCircle extends Canvas
{
public static void main(String[] args)
{
new DrawCircle();
}
public DrawCircle()
{
setSize(50, 50);
}
public void paint(Graphics g)
{
g.drawOval(5,5,10,10);
}
}
drawOval是画椭圆的,而Graphics类中并没有真正画圆的工具,只能借助画椭圆的方法画圆.JAVA中画圆的方式比较像先画矩形然后在矩形中画一个内置圆.当然矩形是正方形是为圆否则为椭圆.
drawOval有四个参数为别为:矩形左上角的坐标值(两个参数),矩形长,矩形宽.如g.drawOval(5,5,10,10)所画圆的外接矩形左上角坐标为(5,5),长10,宽10
public class DrawCircle extends Canvas
{
public static void main(String[] args)
{
new DrawCircle();
}
public DrawCircle()
{
setSize(50, 50);
}
public void paint(Graphics g)
{
g.drawOval(5,5,10,10);
}
}
drawOval是画椭圆的,而Graphics类中并没有真正画圆的工具,只能借助画椭圆的方法画圆.JAVA中画圆的方式比较像先画矩形然后在矩形中画一个内置圆.当然矩形是正方形是为圆否则为椭圆.
drawOval有四个参数为别为:矩形左上角的坐标值(两个参数),矩形长,矩形宽.如g.drawOval(5,5,10,10)所画圆的外接矩形左上角坐标为(5,5),长10,宽10
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
class DrawPanel extends JPanel{
int diameter;
public DrawPanel() {
super();
}
public void updateDia(int dia){
this.diameter = dia ;
this.updateUI();
}
public void paint(Graphics g){
g.setColor(Color.RED);
int x = Math.max(0, (getWidth() - diameter)/2);
int y = Math.max(0, (getWidth() - diameter)/2);
g.drawOval(x,y,diameter,diameter);
}
}
public class CircleFrame extends JFrame{
public static void main(String[] args){
new CircleFrame().setVisible(true);
}
JTextField diameterField = new JTextField();
JButton submit = new JButton("提交");
JLabel diameterLabel = new JLabel("直径 :");
DrawPanel drawPanel = new DrawPanel();
public static final int BLANK = 30;
public static final int INIT_SIZE = 200;
public static final int INFO_HEIGHT = 30;
public CircleFrame() {
super("Draw Circle");
initPanel();
this.setSize(INIT_SIZE ,INIT_SIZE + INFO_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initPanel() {
diameterField.setText("0");
drawPanel.setSize(INIT_SIZE,INIT_SIZE);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
updateDrawPanel();
}
});
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
infoPanel.add(diameterLabel,BorderLayout.WEST);
infoPanel.add(diameterField,BorderLayout.CENTER);
infoPanel.add(submit,BorderLayout.EAST);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(infoPanel,BorderLayout.NORTH);
getContentPane().add(drawPanel,BorderLayout.CENTER);
}
private void updateDrawPanel(){
int dia;
try {
dia = Integer.parseInt(diameterField.getText());
} catch(Exception e){
JOptionPane.showMessageDialog(this,"wrong diameter !","wrong",JOptionPane.OK_OPTION);
return;
}
if(dia < 0 ){
JOptionPane.showMessageDialog(this,"wrong diameter !","wrong",JOptionPane.OK_OPTION);
return;
}
int nowSize = Math.max(dia+BLANK, INIT_SIZE);
drawPanel.updateDia(dia);
drawPanel.setSize(nowSize,nowSize);
setSize(new Dimension(nowSize,nowSize+INFO_HEIGHT));
this.validate();
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
class DrawPanel extends JPanel{
int diameter;
public DrawPanel() {
super();
}
public void updateDia(int dia){
this.diameter = dia ;
this.updateUI();
}
public void paint(Graphics g){
g.setColor(Color.RED);
int x = Math.max(0, (getWidth() - diameter)/2);
int y = Math.max(0, (getWidth() - diameter)/2);
g.drawOval(x,y,diameter,diameter);
}
}
public class CircleFrame extends JFrame{
public static void main(String[] args){
new CircleFrame().setVisible(true);
}
JTextField diameterField = new JTextField();
JButton submit = new JButton("提交");
JLabel diameterLabel = new JLabel("直径 :");
DrawPanel drawPanel = new DrawPanel();
public static final int BLANK = 30;
public static final int INIT_SIZE = 200;
public static final int INFO_HEIGHT = 30;
public CircleFrame() {
super("Draw Circle");
initPanel();
this.setSize(INIT_SIZE ,INIT_SIZE + INFO_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initPanel() {
diameterField.setText("0");
drawPanel.setSize(INIT_SIZE,INIT_SIZE);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
updateDrawPanel();
}
});
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
infoPanel.add(diameterLabel,BorderLayout.WEST);
infoPanel.add(diameterField,BorderLayout.CENTER);
infoPanel.add(submit,BorderLayout.EAST);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(infoPanel,BorderLayout.NORTH);
getContentPane().add(drawPanel,BorderLayout.CENTER);
}
private void updateDrawPanel(){
int dia;
try {
dia = Integer.parseInt(diameterField.getText());
} catch(Exception e){
JOptionPane.showMessageDialog(this,"wrong diameter !","wrong",JOptionPane.OK_OPTION);
return;
}
if(dia < 0 ){
JOptionPane.showMessageDialog(this,"wrong diameter !","wrong",JOptionPane.OK_OPTION);
return;
}
int nowSize = Math.max(dia+BLANK, INIT_SIZE);
drawPanel.updateDia(dia);
drawPanel.setSize(nowSize,nowSize);
setSize(new Dimension(nowSize,nowSize+INFO_HEIGHT));
this.validate();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询