Swing画板 直线和三角形

来源:互联网 发布:软件测试的发展方向 编辑:程序博客网 时间:2024/05/01 08:23

运行 test.java

 

----------------------------------------------test.java--------------------------------------------------

package col.alex;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.plaf.FontUIResource;

 

public class Test extends JFrame implements ActionListener {

       public static final Font f12 = new Font("宋体", Font.PLAIN, 12);

       private static final int FRAME_WIDTH = 700;

       private static final int FRAME_HEIGHT = 740;

       JTextField tf1, tf2, tf3, tf4, tf5, tf6;

       JRadioButton rbt;

       Container mainPane;

       JPanel northPane, centerPane, southPane;

       JButton northLineButton, northRoundButton, northSanButton, northSMButton;

       JPanel linePane, roundPane, sanPanel, SMPanel;

       JTextField lineFieldx1, lineFieldx2, lineFieldy1, lineFieldy2, lineFieldx3,

                     lineFieldy3;

       JTextField roundField, roundField2, roundField3;

       JTextField SlineFieldx1, SlineFieldx2, SlineFieldy1, SlineFieldy2,

                     SlineFieldx3, SlineFieldy3;

       // constructor

       public Test() {

//setBackground(Color.pink);

              init();

       }

 

       private void init() {

              try {

                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

              } catch (Exception e) {

                     e.printStackTrace(System.err);

              }

              setTitle("画图!");

              setSize(FRAME_WIDTH, FRAME_HEIGHT);

              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

              // setResizable(false);

              initGlobalFontSetting(f12);

              mainPane = getContentPane();

              mainPane.setLayout(new BorderLayout());

              mainPane.add(createNorthPane(false), BorderLayout.NORTH);

              setVisible(true);

 

       }

 

       private void initGlobalFontSetting(Font fnt) {

              FontUIResource fontRes = new javax.swing.plaf.FontUIResource(fnt);

              Enumeration keys = UIManager.getDefaults().keys();

              while (keys.hasMoreElements()) {

                     Object key = keys.nextElement();

                     Object value = UIManager.get(key);

                     if (value instanceof javax.swing.plaf.FontUIResource)

                            UIManager.put(key, fontRes);

              }

       }

 

       private JPanel createNorthPane(boolean force) {

              if (northPane == null || force) {

                     northSanButton = new JButton("画三角形");

                     northSanButton.addActionListener(this);

                     northLineButton = new JButton("画直线");

                     northLineButton.addActionListener(this);

                     northRoundButton = new JButton("画圆");

                     northRoundButton.addActionListener(this);

                     northSMButton = new JButton("使用说明!");

                     northSMButton.addActionListener(this);

 

                     northPane = new JPanel();

                     northPane.add(northSanButton);

                     northPane.add(northLineButton);

                     northPane.add(northRoundButton);

                     northPane.add(northSMButton);

              }

              return northPane;

       }

 

       private JPanel createSMPanel(boolean force) {

              SMPanel = new JPanel();

              return SMPanel;

       }

 

       private JPanel createRoundPanel(boolean force) {

              if (roundPane == null || force) {

                     roundField = new JTextField(4);

                     roundField2 = new JTextField(4);

                     roundField3 = new JTextField(4);

                     JButton bt2 = new JButton("画圆");

                     bt2.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent event) {

                                   DrawRound dRound = new DrawRound(Integer

                                                 .parseInt(roundField.getText()), Integer

                                                 .parseInt(roundField2.getText()), Integer

                                                 .parseInt(roundField3.getText()));

                                   dRound.setSize(400, 420);

                                   updateMainPane(createNorthPane(false), dRound,

                                                 createRoundPanel(false));

                                   DrawLine dLine2 = new DrawLine(10, 200, 500, 200);

                            }

                     });

                     roundPane = new JPanel();

                     roundPane.add(new JLabel("半径:"));

                     roundPane.add(roundField);

                     roundPane.add(new JLabel("原点坐标:"));

                     roundPane.add(new JLabel("O("));

                     roundPane.add(roundField2);

                     roundPane.add(new JLabel(", "));

                     roundPane.add(roundField3);

                     roundPane.add(new JLabel(") "));

                     roundPane.add(bt2);

              }

              return roundPane;

 

       }

 

       private JPanel createLinePane(boolean force) {

              if (linePane == null || force) {

                     lineFieldx1 = new JTextField(4);

                     lineFieldy1 = new JTextField(4);

                     lineFieldx2 = new JTextField(4);

                     lineFieldy2 = new JTextField(4);

                     JButton bt = new JButton("画直线");

                     bt.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent event) {

                                   DrawLine dLine = new DrawLine(Integer.parseInt(lineFieldx1

                                                 .getText()), Integer

                                                 .parseInt(lineFieldy1.getText()), Integer

                                                 .parseInt(lineFieldx2.getText()), Integer

                                                 .parseInt(lineFieldy2.getText()));

                                   dLine.setSize(400, 420);

                                   updateMainPane(createNorthPane(false), dLine,

                                                 createLinePane(false));

                                   DrawLine dLine2 = new DrawLine(10, 200, 500, 200);

                            }

                     });

                     linePane = new JPanel();

                     linePane.add(new JLabel("直线的端点坐标:"));

                     linePane.add(new JLabel("A("));

                     linePane.add(lineFieldx1);

                     linePane.add(new JLabel(", "));

                     linePane.add(lineFieldy1);

                     linePane.add(new JLabel(") "));

                     linePane.add(new JLabel("B("));

                     linePane.add(lineFieldx2);

                     linePane.add(new JLabel(", "));

                     linePane.add(lineFieldy2);

                     linePane.add(new JLabel(")"));

                     linePane.add(bt);

              }

              return linePane;

       }

 

       private JPanel createSanPane(boolean force) {

              if (linePane == null || force) {

                     SlineFieldx1 = new JTextField(4);

                     SlineFieldy1 = new JTextField(4);

                     SlineFieldx2 = new JTextField(4);

                     SlineFieldy2 = new JTextField(4);

                     SlineFieldx3 = new JTextField(4);

                     SlineFieldy3 = new JTextField(4);

                     JButton bt1 = new JButton("画三角形");

                     bt1.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent event) {

                                   DrawSan dSan = new DrawSan(Integer.parseInt(SlineFieldx1

                                                 .getText()), Integer.parseInt(SlineFieldy1

                                                 .getText()), Integer.parseInt(SlineFieldx2

                                                 .getText()), Integer.parseInt(SlineFieldy2

                                                 .getText()), Integer.parseInt(SlineFieldx3

                                                 .getText()), Integer.parseInt(SlineFieldy3

                                                 .getText()));

                                   dSan.setSize(400, 420);

                                   updateMainPane(createNorthPane(false), dSan,

                                                 createSanPane(false));

                                   DrawLine dSan2 = new DrawLine(10, 200, 500, 200);

                            }

                     });

                     sanPanel = new JPanel();

                     sanPanel.add(new JLabel("三顶点坐标:"));

                     sanPanel.add(new JLabel("A("));

                     sanPanel.add(SlineFieldx1);

                     sanPanel.add(new JLabel(","));

                     sanPanel.add(SlineFieldy1);

                     sanPanel.add(new JLabel(") ;"));

 

                     sanPanel.add(new JLabel("B("));

                     sanPanel.add(SlineFieldx2);

                     sanPanel.add(new JLabel(", "));

                     sanPanel.add(SlineFieldy2);

                     sanPanel.add(new JLabel(") ;"));

 

                     sanPanel.add(new JLabel("C("));

                     sanPanel.add(SlineFieldx3);

                     sanPanel.add(new JLabel(","));

                     sanPanel.add(SlineFieldy3);

                     sanPanel.add(new JLabel(") ;"));

 

                     sanPanel.add(bt1);

              }

              return sanPanel;

       }

 

       public void actionPerformed(ActionEvent event) {

              Object source = event.getSource();

              if (source == northSanButton) {

                     drawSan();

 

              } else if (source == northLineButton) {

                     drawLine();

              } else if (source == northSMButton) {

                     drawSM();

              } else if (source == northRoundButton)

                     drawRound();

       }

 

       private void drawSM() {

              updateMainPane(createNorthPane(false), new JPanelSM(),

                            createSMPanel(true));

 

       }

 

       private void drawLine() {

              updateMainPane(createNorthPane(false), new JPanelsan(),

                            createLinePane(true));

       }

 

       private void drawSan() {

 

              updateMainPane(createNorthPane(false), new JPanelsan(),

                            createSanPane(true));

       }

 

       private void drawRound() {

              updateMainPane(createNorthPane(false), new JPanelsan(),

                            createRoundPanel(true));

       }

 

       private void updateMainPane(JPanel north, JPanel center, JPanel south) {

              mainPane.removeAll();

              mainPane.add(north, BorderLayout.NORTH);

              mainPane.add(center, BorderLayout.CENTER);

              mainPane.add(south, BorderLayout.SOUTH);

              mainPane.validate();

 

              mainPane.repaint();

 

       }

 

       public static void main(String[] arg) {

 

              Test test = new Test();

       }

}

 

----------------------------------------------Localtion.java-------------------------------------------

package col.alex;

 

 

public class Location {

       public int x;

       public int y;

 

       public Location(int x, int y) {

              this.x = x;

              this.y = y;

       }

}

 

--------------------------------------------JPanelsan.java-------------------------------------------

package col.alex;

 

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

 

import javax.swing.JPanel;

 

public class JPanelsan extends JPanel {

 

       public void paintComponent(Graphics g) {

              super.paintComponent(g);

              setBackground(Color.gray);

              Dimension panelSize = this.getSize();

              Location center = new Location(panelSize.width / 2,

                            panelSize.height / 2);

              int radius = (int) ((Math.min(panelSize.width, panelSize.height) / 2) * 0.9);

              int[] x = new int[2 * radius + 1];

              int[] y = new int[2 * radius + 1];

              g.setColor(Color.black);

              // 画坐标轴

              g.drawLine(0, center.y, 2 * center.x, center.y);// x

              g.drawLine(center.x, 0, center.x, 2 * center.y);// y

              for (int i = -center.x / 10; i < center.x / 10; i++) {

                     g.drawLine(center.x + 10 * i, center.y, center.x + 10 * i,

                                   center.y - 2);// x

                     g.drawLine(center.x, center.y + 10 * i, center.x - 2, center.y + 10

                                   * i);// y

              }

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y - 10);

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y + 10);

              g.drawLine(center.x, 0, center.x - 10, 10);

              g.drawLine(center.x, 0, center.x + 10, 10);

 

              g.drawPolyline(x, y, 2 * radius + 1);

              g.setColor(Color.red);

              g.setFont(new Font("ScanSerif", Font.BOLD, 12));

              g.drawString("单位长度为:10个单位", 60, 60);

              g.drawString("O", center.x - 10, center.y + 10);

              g.drawString("X/" + center.x, 2 * center.x - 30, center.y - 50);

              g.drawString("Y/" + center.y, center.x + 30, 30);

 

       }

 

}

 

--------------------------------------------DrawLine.java-------------------------------------------

package col.alex;

 

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import javax.swing.JPanel;

 

public class DrawLine extends JPanel {

 

       int a, b, c, d, i;

 

       DrawLine() {

       }

 

       DrawLine(int a, int b, int c, int d) {

              this.a = a;

              this.b = b;

              this.c = c;

              this.d = d;

       }

 

       public void paintComponent(Graphics g) {

 

              super.paintComponent(g);

              setBackground(Color.gray);

              Dimension panelSize = this.getSize();

              System.out.println(panelSize.width + "   " + panelSize.height);

              Location center = new Location(panelSize.width / 2,

                            panelSize.height / 2);

              int radius = (int) ((Math.min(panelSize.width, panelSize.height) / 2) * 0.9);

 

              int[] x = new int[2 * radius + 1];

              int[] y = new int[2 * radius + 1];

 

              g.setColor(Color.black);

              // 画坐标轴

 

              g.drawLine(0, center.y, 2 * center.x, center.y);// x

              g.drawLine(center.x, 0, center.x, 2 * center.y);// y

              for (int i = -center.x / 10; i < center.x / 10; i++) {

                     g.drawLine(center.x + 10 * i, center.y, center.x + 10 * i,

                                   center.y - 2);// x

                     g.drawLine(center.x, center.y + 10 * i, center.x - 2, center.y + 10

                                   * i);// y

              }

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y - 10);

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y + 10);

              g.drawLine(center.x, 0, center.x - 10, 10);

              g.drawLine(center.x, 0, center.x + 10, 10);

 

              g.drawPolyline(x, y, 2 * radius + 1);

 

              g.setColor(Color.red);

              g.setFont(new Font("ScanSerif", Font.BOLD, 12));

              g.drawString("单位长度为:10个单位", 60, 60);

 

              g.drawString("A点坐标:" + a + "," + b + ")", 60, 90);

              g.drawString("B点坐标:" + c + "," + d + ")", 60, 120);

              g.drawString("O", center.x - 10, center.y + 10);

 

              g.drawString("X/" + center.x, 2 * center.x - 30, center.y - 50);

              g.drawString("Y/" + center.y, center.x + 30, 30);

 

              g.drawLine(a + center.x, center.y - b, center.x + c, center.y - d);

              String m = "A(" + a + "," + b + ")";

              g.drawString(m, (a + center.x) + 5, (center.y - b) + 5);

              m = "B(" + c + "," + d + ")";

              g.drawString(m, (c + center.x) + 5, (center.y - d) + 5);

       }

 

}

--------------------------------------------DrawRound.java-----------------------------------------

package col.alex;

 

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

 

import javax.swing.JPanel;

 

public class DrawRound extends JPanel {

 

       int a, b, c, d, i;

 

       DrawRound() {

       }

 

       DrawRound(int a, int b, int c) {

              this.a = a;

              this.b = b;

              this.c = c;

 

       }

 

       public void paintComponent(Graphics g) {

 

              super.paintComponent(g);

              setBackground(Color.gray);

              Dimension panelSize = this.getSize();

              System.out.println(panelSize.width + "   " + panelSize.height);

              Location center = new Location(panelSize.width / 2,

                            panelSize.height / 2);

              int radius = (int) ((Math.min(panelSize.width, panelSize.height) / 2) * 0.9);

 

              int[] x = new int[2 * radius + 1];

              int[] y = new int[2 * radius + 1];

 

              g.setColor(Color.black);

              // 画坐标轴

 

              g.drawLine(0, center.y, 2 * center.x, center.y);// x

              g.drawLine(center.x, 0, center.x, 2 * center.y);// y

              for (int i = -center.x / 10; i < center.x / 10; i++) {

                     g.drawLine(center.x + 10 * i, center.y, center.x + 10 * i,

                                   center.y - 2);// x

                     g.drawLine(center.x, center.y + 10 * i, center.x - 2, center.y + 10

                                   * i);// y

              }

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y - 10);

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y + 10);

              g.drawLine(center.x, 0, center.x - 10, 10);

              g.drawLine(center.x, 0, center.x + 10, 10);

 

              g.drawPolyline(x, y, 2 * radius + 1);

 

              g.setColor(Color.red);

              g.setFont(new Font("ScanSerif", Font.BOLD, 12));

              g.drawString("单位长度为:10个单位", 60, 60);

              g.drawString("O点坐标:" + b + "," + c + ")", 60, 90);

              g.drawString("半径为:" + a, 60, 120);

              g.drawString("O", center.x - 10, center.y + 10);

 

              g.drawString("X/" + center.x, 2 * center.x - 30, center.y - 50);

              g.drawString("Y/" + center.y, center.x + 30, 30);

 

              g.drawOval(center.x + b - a, center.y - c - a, 2 * a, 2 * a);

 

              g.fillOval(center.x + b - 3 / 2, center.y - c - 3 / 2, 3, 3);

              g.drawLine(center.x + b, center.y - c, center.x + b, center.y - c);

              String m = "O(" + b + "," + c + ")";

              g.drawString(m, (b + center.x) + 5, (center.y - c) + 5);

       }

 

}

 

-------------------------------------------DrawSan.java-----------------------------------------

 

package col.alex;

 

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import javax.swing.JPanel;

 

public class DrawSan extends JPanel {

       int a, b, c, d, e, f;

       DrawSan(int a, int b, int c, int d, int e, int f) {

              this.a = a;

              this.b = b;

              this.c = c;

              this.d = d;

              this.e = e;

              this.f = f;

       }

       public void paintComponent(Graphics g) {

              super.paintComponent(g);

              setBackground(Color.gray);

              Dimension panelSize = this.getSize();

              Location center = new Location(panelSize.width / 2,

                            panelSize.height / 2);

              int radius = (int) ((Math.min(panelSize.width, panelSize.height) / 2) * 0.9);

              int[] x = new int[2 * radius + 1];

              int[] y = new int[2 * radius + 1];

              g.setColor(Color.black);

              // 画坐标轴

              g.drawLine(0, center.y, 2 * center.x, center.y);// x

              g.drawLine(center.x, 0, center.x, 2 * center.y);// y

              for (int i = -center.x / 10; i < center.x / 10; i++) {

                     g.drawLine(center.x + 10 * i, center.y, center.x + 10 * i,

                                   center.y - 2);// x

                     g.drawLine(center.x, center.y + 10 * i, center.x - 2, center.y + 10

                                   * i);// y

              }

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y - 10);

              g.drawLine(2 * center.x, center.y, 2 * center.x - 10, center.y + 10);

              g.drawLine(center.x, 0, center.x - 10, 10);

              g.drawLine(center.x, 0, center.x + 10, 10);

              g.drawPolyline(x, y, 2 * radius + 1);

              g.setColor(Color.red);

              g.setFont(new Font("ScanSerif", Font.BOLD, 12));

              g.drawString("单位长度为:10个单位", 60, 60);

              g.drawString("A点坐标:" + a + "," + b + ")", 60, 90);

              g.drawString("B点坐标:" + c + "," + d + ")", 60, 120);

              g.drawString("C点坐标:" + e + "," + f + ")", 60, 150);

              g.drawString("O", center.x - 10, center.y + 10);

 

              g.drawString("X/" + center.x, 2 * center.x - 30, center.y - 50);

              g.drawString("Y/" + center.y, center.x + 30, 30);

 

              g.drawLine(a + center.x, center.y - b, center.x + c, center.y - d);

              String m = "A(" + a + "," + b + ")";

              g.drawString(m, (a + center.x), (center.y - b) + 10);

 

              g.drawLine(c + center.x, center.y - d, center.x + e, center.y - f);

              m = "B(" + c + "," + d + ")";

              g.drawString(m, (c + center.x), (center.y - d) + 10);

 

              g.drawLine(e + center.x, center.y - f, center.x + a, center.y - b);

              m = "C(" + e + "," + f + ")";

              g.drawString(m, (e + center.x), (center.y - f) + 10);

 

       }

}

 

 

------------------------------------------JPanelSM.java-----------------------------------------

 

package col.alex;

import java.awt.*;

import javax.swing.*;

 

public class JPanelSM extends JPanel {

 

       public void paintComponent(Graphics g) {

              super.paintComponent(g);

              setBackground(Color.gray);

              Image img = Toolkit.getDefaultToolkit().getImage("1.gif");

              Dimension panelSize = this.getSize();

              Location center = new Location(panelSize.width / 2,

                            panelSize.height / 2);

              int radius = (int) ((Math.min(panelSize.width, panelSize.height) / 2) * 0.9);

              int[] x = new int[2 * radius + 1];

              int[] y = new int[2 * radius + 1];

              g.setColor(Color.red);

              String a = "本画图程序为一个简易的画图程序,包括画直线,画三角形。";

              g.drawString(a, 60, 60);

 

              a = "①画三角形时候,在方框中输入三个顶点的坐标A,B,C  点击“画三角形”按钮 即可在坐标轴中画出所需的三角形。";

              g.drawString(a, 60, 120);

 

              a = "②画直线的时候,在方框中输入起始点和终点的坐标 A,B 点击“画直线”按钮 即可在坐标轴中画出所需的直线。 ";

              g.drawString(a, 60, 180);

 

              a = "③画圆的时候,在方框中输入半径和圆心所在的坐标点O ,点击”画圆“按钮 即可在坐标轴中画出需要的圆。";

              g.drawString(a, 60, 240);

 

              a = "④所填的坐标必须在 面板的范围内,必要的信息会在右上角显示!";

              g.drawString(a, 60, 300);

 

 

              g.setFont(new Font("宋体", Font.BOLD, 20));

              a = "使用说明书";

              g.drawString(a, center.x - 20, 20);

 

              g.drawImage(img, 200, 200, 200, 200, this);

}

}

 

 

原创粉丝点击