概述
三角形:
package 软件工程项目1;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Triangle extends JFrame implements ActionListener{
JTextField text1,text2;
JLabel label1,label2;
JPanel panel1,panel2;
JButton bt1;
JTextArea area;
Font f;
public Triangle() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500,500);
}
void init() {
f=new Font("宋体",Font.BOLD,16);
setLayout(new FlowLayout());
label1=new JLabel("三角形的底长:");
label2=new JLabel("三角形的厚度:");
text1=new JTextField(5);
text2=new JTextField(5);
panel1=new JPanel();
panel2=new JPanel();
area=new JTextArea(20,30);
bt1=new JButton("确定");
add(label1);
add(text1);
add(panel1);
add(label2);
add(text2);
add(panel2);
add(bt1);
add(new JScrollPane(area));
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
bt1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String height1=text1.getText();
String thickness1=text2.getText();
int i=0,j=0;
area.setFont(f);
if(height1.equals("")||thickness1.equals("")||(Integer.parseInt(height1)<Integer.parseInt(thickness1)))
{
area.append("输入数字有误,请重新输入!");
text2.setText("");
}
else {
area.setText("");
int layer=Integer.parseInt(text1.getText().trim());
int thickness=Integer.parseInt(text2.getText().trim());
for(i=layer;i>=1;i--)
{
if(i<=thickness)
{
for(j=1;j<=thickness;j++)
area.append("#");
}
else if(i>layer-thickness){
for(j=1;j<=i;j++)
area.append("#");
}
else{
for(j=1;j<=i;j++)
{
if(j<thickness+1)
area.append("#");
else if(j>i-thickness)
area.append("#");
else
area.append(" ");
}
}
area.append("n");
}
}
}
public static void main(String[] args) {
new Triangle();
}
}
**
截图
**
**
正方形:
package 软件工程项目1;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Square extends JFrame implements ActionListener{
JTextField text1,text2;
JLabel label1,label2;
JPanel panel1,panel2;
JTextArea area;
JButton bt1;
Font f;
public Square() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,500,500);
}
void init() {
f=new Font("宋体",Font.BOLD,16);
setLayout(new FlowLayout());
text1=new JTextField(5);
text2=new JTextField(5);
label1=new JLabel("输入正方形高度:");
label2=new JLabel("输入正方形厚度:");
area=new JTextArea(20,20);
panel1=new JPanel();
panel2=new JPanel();
bt1=new JButton("确定");
add(label1);
add(text1);
add(panel1);
add(label2);
add(text2);
add(panel2);
add(bt1);
add(new JScrollPane(area));
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
bt1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
String height1=text1.getText();
String thickness1=text2.getText();
int i=0,j=0;
area.setFont(f);
//StringBuffer space= new StringBuffer();
//String space="";
if(height1.equals("")||thickness1.equals("")||(Integer.parseInt(height1)<Integer.parseInt(thickness1)))
{
area.append("输入数字有误,请重新输入!");
text2.setText("");
}
else {
area.append("");
int height=Integer.parseInt(text1.getText().trim());
int thickness=Integer.parseInt(text2.getText().trim());
for(i=1;i<=height;i++)
{
if(i<=thickness)
{
for(j=1;j<=height;j++)
area.append("#");
}
else if(i>height-thickness){
for(j=1;j<=height;j++)
area.append("#");
}
else{
for(j=1;j<=height;j++)
{
if(j<thickness+1)
area.append("#");
else if(j>height-thickness)
area.append("#");
else
area.append(" ");
}
}
area.append("n");
}
}
}
public static void main(String[] args) {
new Square();
}
}
截图:
**
长方形:
package 软件工程项目1;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rectangle extends JFrame implements ActionListener{
JTextField text1,text2,text3;
JLabel label1,label2,label3;
JPanel panel1,panel2,panel3;
JTextArea area;
JButton bt1;
Font f;
public Rectangle() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,600,600);
}
void init() {
f=new Font("宋体",Font.BOLD,16);
setLayout(new FlowLayout());
text1=new JTextField(5);
text2=new JTextField(5);
text3=new JTextField(5);
label1=new JLabel("输入长方形高:");
label2=new JLabel("输入长方形厚度:");
label3=new JLabel("输入长方形宽:");
area=new JTextArea(30,30);
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
bt1=new JButton("确定");
add(label1);
add(text1);
add(panel1);
add(label2);
add(text2);
add(panel2);
add(label3);
add(text3);
add(panel3);
add(bt1);
add(new JScrollPane(area));
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
panel3.add(label3);
panel3.add(text3);
bt1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
String height1=text1.getText();
String thickness1=text2.getText();
String w=text3.getText();
int i=0,j=0;
area.setFont(f);
if(height1.equals("")||thickness1.equals("")||w.equals("")||(Integer.parseInt(height1)<Integer.parseInt(thickness1)))
{
area.append("输入数字有误,请重新输入!");
text2.setText("");
}
else {
int longg=Integer.parseInt(text1.getText().trim());
int thickness=Integer.parseInt(text2.getText().trim());
int wide=Integer.parseInt(text3.getText().trim());
for(i=1;i<=longg;i++)
{
if(i<=thickness)
{
for(j=1;j<=wide;j++)
area.append("#");
}
else if(i>longg-thickness){
for(j=1;j<=wide;j++)
area.append("#");
}
else{
for(j=1;j<=wide;j++)
{
if(j<thickness+1)
area.append("#");
else if(j>wide-thickness)
area.append("#");
else
area.append(" ");
}
}
area.append("n");
}
}
}
public static void main(String[] args) {
new Rectangle();
}
}
截图:
**
金字塔:
package 软件工程项目1;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Pyramid extends JFrame implements ActionListener{
JTextField text1,text2;
JLabel label1,label2;
JPanel panel1,panel2;
JButton bt1;
JTextArea area;
Font f;
public Pyramid() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500,500);
}
void init() {
f=new Font("宋体",Font.BOLD,16);
setLayout(new FlowLayout());
label1=new JLabel("金字塔的层数:");
label2=new JLabel("金字塔的高度:");
text1=new JTextField(5);
text2=new JTextField(5);
panel1=new JPanel();
panel2=new JPanel();
area=new JTextArea(20,20);
bt1=new JButton("确定");
add(label1);
add(text1);
add(panel1);
add(label2);
add(text2);
add(panel2);
add(bt1);
add(new JScrollPane(area));
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
bt1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
int level=Integer.parseInt(text1.getText().trim());
int size=Integer.parseInt(text2.getText().trim());
area.setFont(f);
int i=0,j=0,k=0;
k=level*size;
for(i=1;i<=k;i++){
for(j=1;j<=i+k;j++){
if((j>=k-i)&&(i+j-k-1)%(2*size)==0){
area.append("/");
}
else if((j>k-i)&&((k+i-j)%(2*size))==0){
area.append("\");
}
else if((j>k-i)&&(j<k+i)&&(i%size==0)){
area.append("_");
}
else area.append(" ");
}
area.append("n");
}
}
public static void main(String[] args) {
new Pyramid();
}
}
截图:
**
菱形:
package 软件工程项目1;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Lingxing extends JFrame implements ActionListener{
JTextField text1;
JLabel label1;
JPanel panel1;
JButton bt1;
JTextArea area;
Font f;
public Lingxing() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500,500);
}
void init() {
f=new Font("宋体",Font.BOLD,16);
setLayout(new FlowLayout());
label1=new JLabel("菱形的高度:");
text1=new JTextField(5);
panel1=new JPanel();
area=new JTextArea(20,20);
bt1=new JButton("确定");
add(label1);
add(text1);
add(panel1);
add(bt1);
add(new JScrollPane(area));
panel1.add(label1);
panel1.add(text1);
bt1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text1.getText().trim());
int i=0,j=0;
area.setFont(f);
for ( i = 0; i <= num; i++) {
for (j = 0; j < num - i; j++) {
area.append(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
area.append("#");
}
else {
area.append(" ");
}
}
area.append("n");
}
for ( i = num - 1; i > 0; i--) {
for ( j = 0; j < num - i; j++) {
area.append(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
area.append("#");
}
else {
area.append(" ");
}
}
area.append("n");
}
}
public static void main(String[] args) {
new Lingxing();
}
}
截图:
**
图形选择:
package 软件工程项目1;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SelectGraph extends JFrame implements ActionListener{
JLabel label;
JPanel panel;
JComboBox<String> combox;
JButton bt;
public SelectGraph() {
init();
setVisible(true);
setBounds(100,100,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
String string[]= {"长方形","正方形","三角形","菱形","金字塔"};
label=new JLabel("请选择图形");
combox=new JComboBox<String>(string);
bt=new JButton("确定");
panel=new JPanel();
add(label);
add(combox);
add(panel);
add(bt);
panel.add(label);
panel.add(combox);
bt.addActionListener(this);
}
public static void main(String[] args) {
new SelectGraph();
}
@Override
public void actionPerformed(ActionEvent e) {
String strItem=combox.getSelectedItem().toString(); //获取选中的值
if(strItem.equals("长方形")) {
new Rectangle();
}
if(strItem.equals("三角形")) {
new Triangle();
}
if(strItem.equals("正方形")) {
new Square();
}
if(strItem.equals("菱形")) {
new Lingxing();
}
if(strItem.equals("金字塔")) {
new Pyramid();
}
}
}
最后
以上就是坦率鸭子为你收集整理的用Java绘制图形的全部内容,希望文章能够帮你解决用Java绘制图形所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复