java程序设计图形代码 java程序设计图形代码怎么用
用JAVA编写求解几何图形的应用程序?
1. 抽象形状类
公司专注于为企业提供成都做网站、成都网站建设、微信公众号开发、商城系统网站开发,小程序开发,软件按需定制设计等一站式互联网企业服务。凭借多年丰富的经验,我们会仔细了解各客户的需求而做出多方面的分析、设计、整合,为客户设计出具风格及创意性的商业解决方案,创新互联更提供一系列网站制作和网站推广的服务。
package com;
//抽象的形状类
public abstract class Shape{
}
2. 显示接口
package com;
//接口
public interface IDisplay{
void display(); //显示图形的基本信息
double getArea(); //计算面积
double getGirth(); //计算周长
}
3. 三角形类
package com.tri;
import com.*;
//三角形类
public class Triangle extends Shape implements IDisplay{
protected double a;
protected double b;
protected double c;
public Triangle(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double getArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
@Override
public double getGirth() {
return this.a + this.b + this.c;
}
@Override
public void display() {
System.out.println("三角形");
System.out.println("边长:" + a + ", " + b + ", " + c);
}
}
4. 矩形类
package com.rec;
import com.*;
//矩形类
public class Rectangle extends Shape implements IDisplay {
protected double width;
protected double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return this.width * this.height;
}
@Override
public double getGirth() {
return 2 * ( this.width + this.height);
}
@Override
public void display() {
System.out.println("矩形");
System.out.println("宽:" + this.width + ", 高:" + this.height);
}
}
5. 圆类
package com.cir;
import com.*;
//圆类
public class Circle extends Shape implements IDisplay {
protected double radius;
public Circle(double radius){
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double getGirth() {
return 2 * Math.PI * this.radius;
}
@Override
public void display() {
System.out.println("圆");
System.out.println("半径:" + this.radius);
}
}
6. 正多边形类
package com.mul;
import com.*;
//正多边形类
public class MulSide extends Shape implements IDisplay {
protected double side; //边长
protected int n; //边数
public MulSide(double side, int n){
this.side = side;
this.n = n;
}
@Override
public double getArea() {
return n * side * side * Math.tan((n-2) * Math.PI / (2 * n)) / 4;
}
@Override
public double getGirth() {
return this.side * this.n;
}
@Override
public void display() {
System.out.println("正多边形");
System.out.println("连长:" + this.side + ", 边数:" + this.n);
}
}
7. 主类(测试类)
package com;
import java.util.Scanner;
import com.cir.Circle;
import com.mul.MulSide;
import com.rec.Rectangle;
import com.tri.Triangle;
public class Test22 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a, b, c;
double width, height;
double radius;
double side;
int n;
IDisplay s;
System.out.println("请输入三角形的基本信息");
System.out.print("边长1:");
a = scan.nextDouble();
System.out.print("边长2:");
b = scan.nextDouble();
System.out.print("边长3:");
c = scan.nextDouble();
s = new Triangle(a, b, c);
s.display();
System.out.println("三角形的面积:" + s.getArea());
System.out.println("三角形的周长:" + s.getGirth());
System.out.println("请输入矩形的基本信息");
System.out.print("宽:");
width = scan.nextDouble();
System.out.print("高:");
height = scan.nextDouble();
s = new Rectangle(width, height);
s.display();
System.out.println("矩形的面积:" + s.getArea());
System.out.println("矩的周长:" + s.getGirth());
System.out.println("请输入圆的基本信息");
System.out.print("半径:");
radius = scan.nextDouble();
s = new Circle(radius);
s.display();
System.out.println("圆的面积:" + s.getArea());
System.out.println("圆的周长:" + s.getGirth());
System.out.println("请输入正多边形的基本信息");
System.out.print("边长:");
side = scan.nextDouble();
System.out.print("边数:");
n = scan.nextInt();
s = new MulSide(side, n);
s.display();
System.out.println("正多边形的面积:" + s.getArea());
System.out.println("正多边形的周长:" + s.getGirth());
}
}
运行测试:
请输入三角形的基本信息
边长1:3
边长2:4
边长3:5
三角形
边长:3.0, 4.0, 5.0
三角形的面积:6.0
三角形的周长:12.0
请输入矩形的基本信息
宽:3
高:4
矩形
宽:3.0, 高:4.0
矩形的面积:12.0
矩的周长:14.0
请输入圆的基本信息
半径:2
圆
半径:2.0
圆的面积:12.566370614359172
圆的周长:12.566370614359172
请输入正多边形的基本信息
边长:2
边数:6
正多边形
连长:2.0, 边数:6
正多边形的面积:10.39230484541326
正多边形的周长:12.0
用Java编写一个绘制图形的小程序
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例
Point aPoint;
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制"); //调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件
setSize(300, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g; //获取图形环境
g2D.draw(gPath); //绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath = new GeneralPath(); //重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y); //设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
}
}
}
java编程 完成下图所示的图形用户界面设计(窗口标题为你的真实学号及姓名,例如:11930670
好像是作业, 源代码发给你参考, 注意学习下代码. 了解下布局 和按钮的工作原理
参考代码
import java.awt.*;
import java.awt.event.*;
public class MyRandomFrame extends Frame implements ActionListener {
private final TextField tf1;
private final TextField tf2;
private final TextField tf3;
public static void main(String[] args) {
new MyRandomFrame().setVisible(true);
}
public MyRandomFrame() {
setSize(600, 100);
setLocationRelativeTo(null);
setTitle("学号:XXXXX 姓名:张三");
Button but1 = new Button("获取1~100之间的随机数");
but1.setActionCommand("随即");
but1.addActionListener(this);
Label label1 = new Label("第一个数:");
tf1 = new TextField(5);
Label label2 = new Label("第二个数:");
tf2 = new TextField(5);
Button but2 = new Button("求和");
but2.setActionCommand("求和");
but2.addActionListener(this);
tf3 = new TextField(5);
setLayout(new FlowLayout());
add(but1);
add(label1);
add(tf1);
add(label2);
add(tf2);
add(but2);
add(tf3);
addWindowListener(new WindowAdapter() {// 关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("随即")) {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
tf1.setText(String.valueOf(a));
tf2.setText(String.valueOf(b));
} else if (command.equals("求和")) {
int x = Integer.parseInt(tf1.getText());
int y = Integer.parseInt(tf2.getText());
tf3.setText(String.valueOf((x + y)));
}
}
}
Java程序设计图形类
public class Rectangle{ private int width; private int height; public Rectangle(){ this.width = 10; this.height = 10; } public Rectangle(int width, int height){ this.width = width; this.height = height; } public int area(){ return width * height; } //省略getter/setter }
求一个简单的java代码:(图形界面)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系统登录");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 录");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("关 闭");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:数据校验 success = executeQuery(sql);
if (success) {
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面
JOptionPane.showMessageDialog(this, "成功登录", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密码框选中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 输入密码后回车相当于点击了登录按钮
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望对你有帮助
文章标题:java程序设计图形代码 java程序设计图形代码怎么用
文章链接:http://abwzjs.com/article/doooiso.html