Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MtFrame.java
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class MyFrame extends JFrame implements ActionListener
- {
- private final int DEFAULT_WIDTH = 640;
- private final int DEFAULT_HEIGHT = 480;
- private MyPanel drawingPanel;
- public static void main(String args[])
- {
- JFrame frame = new JFrame();
- frame.setTitle("DrawPoly");
- frame.setSize(350, 250);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- Container contentPane = frame.getContentPane();
- contentPane.add(new MyPanel(1));
- //frame.show();
- frame.setVisible(true);
- JFrame frm1 = new MyFrame();
- frm1.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frm1.setVisible(true);
- }
- private void makeButton(JPanel p , String name , ActionListener target)
- {
- JButton b = new JButton(name);
- p.add(b);
- b.addActionListener(target);
- }
- public MyFrame()
- {
- drawingPanel = new MyPanel(3);
- setTitle("my frame");
- setSize(DEFAULT_WIDTH, DEFAULT_WIDTH);
- JPanel panel = new JPanel(new GridLayout(8,1,0,30));
- for (int i =3;i<10;i++)
- {
- makeButton(panel, String.valueOf(i), this);
- }
- makeButton(panel, "Exit", this);
- add(panel,BorderLayout.EAST);
- add(drawingPanel, BorderLayout.CENTER);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- String buttonPressed = e.getActionCommand();
- switch (buttonPressed) {
- case "3":
- drawingPanel.setPolyLines(3);
- break;
- case "4":
- drawingPanel.setPolyLines(4);
- break;
- case "5":
- drawingPanel.setPolyLines(5);
- break;
- case "6":
- drawingPanel.setPolyLines(6);
- break;
- case "7":
- drawingPanel.setPolyLines(7);
- break;
- case "8":
- drawingPanel.setPolyLines(8);
- break;
- case "9":
- drawingPanel.setPolyLines(9);
- break;
- case "Exit":
- System.exit(1);
- break;
- default:
- break;
- }
- }
- }
- //MyPanel.java
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Polygon;
- import javax.swing.*;
- public class MyPanel extends JPanel
- {
- private Polygon p = null;
- private Color color = Color.RED;
- private int _number;
- public int get_number() {
- return _number;
- }
- public void set_number(int _number) {
- this._number = _number;
- }
- @Override
- public void paintComponents(Graphics g)
- {
- Graphics2D g2 = (Graphics2D)g;
- g2.setStroke(new BasicStroke(10));
- g2.setPaint(color);
- //setPolyLines(get_number());
- Polygon p = new Polygon();
- for (int i = 0; i < 5; i++)
- p.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 5)),
- (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 5)));
- g2.drawPolygon(p);
- /*
- if(p == null)
- return;
- g2.setStroke(new BasicStroke(10));
- g2.setPaint(color);
- g2.draw(p);
- */
- }
- public void setPolyLines(int sides)
- {
- p = new Polygon();
- int radius = 100;
- for(int i=0;i<sides;i++)
- {
- int x =(int) (radius * Math.cos(i * 2 * Math.PI /5 ));
- int y = (int) (radius * Math.sin(i*2*Math.PI/5));
- p.addPoint(x, y);
- }
- repaint();
- }
- public MyPanel(int sides)
- {
- //set_number(sides);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment