Skip to content

Instantly share code, notes, and snippets.

@CvutFelStudentAccount
Created June 2, 2011 13:19
Show Gist options
  • Select an option

  • Save CvutFelStudentAccount/1004404 to your computer and use it in GitHub Desktop.

Select an option

Save CvutFelStudentAccount/1004404 to your computer and use it in GitHub Desktop.
Polygon (Java)
package Polygon;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class Polygon extends JFrame{
JButton b1, b2;
JPanel buttonFrame;
SpaceFrame spaceFrame;
public Polygon(String title) throws HeadlessException {
createButtons();
this.setTitle("Polygon Drawer");
buttonFrame = new JPanel();
buttonFrame.setBackground(Color.BLACK);
buttonFrame.add(b1);
buttonFrame.add(b2);
spaceFrame = new SpaceFrame();
spaceFrame.addMouseListener(new MouseInnerClassListener());
this.setLayout(new BorderLayout());
getContentPane().add(buttonFrame, BorderLayout.NORTH);
getContentPane().add(spaceFrame, BorderLayout.CENTER);
this.setSize(300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
}
private class MouseInnerClassListener implements MouseListener{
public MouseInnerClassListener() {
}
public void mouseClicked(MouseEvent e) {
int xPos = e.getX();
int yPos = e.getY();
MouseClickPoint mcp = new MouseClickPoint(xPos, yPos);
spaceFrame.List.add(mcp);
spaceFrame.draw(xPos, yPos);
}
public void mousePressed(MouseEvent e) {
int xPos = e.getX();
int yPos = e.getY();
MouseClickPoint mcp = new MouseClickPoint(xPos, yPos);
spaceFrame.List.add(mcp);
spaceFrame.draw(xPos, yPos);
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
private void createButtons() {
b1 = new JButton();
b2 = new JButton();
b1.addActionListener(new b1Listener());
b2.addActionListener(new b2Listener());
}
class b1Listener implements ActionListener {
public b1Listener() {
}
public void actionPerformed(ActionEvent e) {
spaceFrame.linkPoints();
}
}
class b2Listener implements ActionListener {
public b2Listener() {
}
public void actionPerformed(ActionEvent e) {
spaceFrame.clear();
}
}
public static void main(String[] args) {
Polygon omo = new Polygon("Test");
omo.setVisible(true);
}
}
class MouseClickPoint {
int xPos, yPos;
MouseClickPoint(int xPos, int yPos) {
this.xPos = xPos;
this.yPos = yPos;
}
}
class SpaceFrame extends JPanel{
ArrayList<MouseClickPoint> List = new ArrayList<MouseClickPoint>();
int xPos,yPos;
boolean lineDrawing = false;
public SpaceFrame() {
this.setBackground(Color.LIGHT_GRAY);
}
void draw(int x, int y){
xPos = x;
yPos = y;
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if(List.isEmpty()==true){
return;
}
if (!lineDrawing==true) {
for (int i = 0; i < List.size(); i++) {
g.fillRect(List.get(i).xPos, List.get(i).yPos, 5, 5);
}
g.drawRect(xPos, yPos, 4, 4);
}
else{
for (int i = 1; i < List.size(); i++) {
g.drawLine(List.get(i).xPos+2, List.get(i).yPos+2, List.get(i-1).xPos+2, List.get(i-1).yPos+2);
}
g.drawLine(List.get(List.size()-1).xPos+2, List.get(List.size()-1).yPos+2, List.get(0).xPos+2, List.get(0).yPos+2);
}
lineDrawing = false;
}
void linkPoints(){
lineDrawing = true;
repaint();
}
void clear(){
List.clear();
repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment