File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Java swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic
Author

Java swing

looka dicosta
Greenhorn

Joined: Nov 22, 2012
Posts: 21


I want a Java swing program to draw non-overlapping n-sided polygon , circles and ellipse with the mouse.

The polygon should not add the last point and first point itself until user makes it.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

BeForthrightWhenCrossPostingToOtherSites (link).

Welcome!


The quieter you are, the more you are able to hear.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What are you asking for ?


[My Blog]
All roads lead to JavaRanch
looka dicosta
Greenhorn

Joined: Nov 22, 2012
Posts: 21
Kemal Sokolovic wrote:BeForthrightWhenCrossPostingToOtherSites (link).

Welcome!


I just want to use all the forums
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

looka dicosta wrote:I just want to use all the forums


You'll probably get an answer at one forum, which means people in other(s) were wasting time trying to help you.

Even if that argument of yours is valid (and it is not) you need to be more specific when posting a question, TellTheDetails and ShowSomeEffort (I can't see any of your code here); what have you tried so far, etc.? There are some guidelines here on how to aks questions, so I would recommend you read it.
looka dicosta
Greenhorn

Joined: Nov 22, 2012
Posts: 21
Kemal Sokolovic wrote:
looka dicosta wrote:I just want to use all the forums


You'll probably get an answer at one forum, which means people in other(s) were wasting time trying to help you.

Even if that argument of yours is valid (and it is not) you need to be more specific when posting a question, TellTheDetails and ShowSomeEffort (I can't see any of your code here); what have you tried so far, etc.? There are some guidelines here on how to aks questions, so I would recommend you read it.


package Circles;

//PaintDemo.java - Simple painting program.
//Illustrates use of mouse and BufferedImage.
//Fred Swartz 2002-12-02
//Possible Enhancements:
//* Clear drawing area
//* Other shapes (line, outline shapes, text, ...)
//* Color selection.
//* An eraser.
//* Create a real toobar.
//* Save/Load


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;

//////////////////////////////////////////////////////////////PaintDemo
class PaintDemo {
//============================================================= main
public static void main(String[] args) {
PaintWindow window = new PaintWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}//end main
}//endclass PaintDemo


////////////////////////////////////////////////////////////PaintWindow
class PaintWindow extends JFrame {
PaintPanel canvas = new PaintPanel();
//====================================================== constructor
public PaintWindow() {
//--- create the buttons
JButton circleButton = new JButton("Circle");
circleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.setShape(PaintPanel.CIRCLE);
}});
JButton rectangleButton = new JButton("Rectangle");
rectangleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.setShape(PaintPanel.RECTANGLE);
}});

JButton polyButton = new JButton("Polygon");
polyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.setShape(PaintPanel.POLY);
}});

//--- layout the buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2, 1));
buttonPanel.add(circleButton);
buttonPanel.add(rectangleButton);
buttonPanel.add(polyButton);

//--- layout the window
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(buttonPanel, BorderLayout.WEST);
content.add(canvas , BorderLayout.CENTER);
this.setTitle("Paint Demo");
this.pack();
}//end constructor
}//endclass PaintWindow


///////////////////////////////////////////////////////////// PaintPanel
class PaintPanel extends JPanel implements MouseListener,
MouseMotionListener {
//--- Public constants used to specify shape being drawn.
public static final int NONE = 0;
public static final int LINE = 1;
public static final int RECTANGLE = 2;
public static final int CIRCLE = 3;
public static final int POLY = 4;

//--- Variables to store the current figure info
private int _shape = NONE;
public int getShape() {
return _shape;
}
private int _currentStartX = 0; // where mouse first pressed
private int _currentStartY = 0;
private int _currentEndX = 0; // where dragged to or released
private int _currentEndY = 0;

//--- BufferedImage to store the underlying saved painting.
// Will be initialized first time paintComponent is called.
private BufferedImage _bufImage = null;
private boolean polygonIsNowComplete = false;

//--- Private constant for size of paint area.
private static final int SIZE = 600; // size of paint area

private final Point trackPoint = new Point();
private ArrayList points = new ArrayList();


//====================================================== constructor
public PaintPanel() {
setPreferredSize(new Dimension(SIZE, SIZE));
setBackground(Color.white);
//--- Add the mouse listeners.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}//endconstructor


//========================================================= setShape
public void setShape(int shape) {
//--- Provided so users can set the shape.
_shape = shape;
}//end setShape


//=================================================== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g; // downcast to Graphics2D
if (_bufImage == null) {
//--- This is the first time, initialize _bufImage
int w = this.getWidth();
int h = this.getHeight();
_bufImage = (BufferedImage)this.createImage(w, h);
Graphics2D gc = _bufImage.createGraphics();
gc.setColor(Color.white);
gc.fillRect(0, 0, w, h); // fill in background
}
g2.drawImage(_bufImage, null, 0, 0); // draw previous shapes

drawCurrentShape(g2);
}//end paintComponent


//================================================= drawCurrentShape
private void drawCurrentShape(Graphics2D g2) {
//--- Draws current shape on a graphics context, either
// on the context passed to paintComponent, or the
// context for the BufferedImage.
switch (_shape) {
case NONE :
break;

case CIRCLE:
g2.drawOval(_currentStartX, _currentStartY,
_currentEndX - _currentStartX,
_currentEndY - _currentStartY);
break;

case RECTANGLE:
g2.drawRect(_currentStartX, _currentStartY,
_currentEndX - _currentStartX,
_currentEndY - _currentStartY);
break;

case POLY:
g2.setColor(Color.red);
g2.fillOval(_currentStartX, _currentStartY, 8, 8);

default: // should never happen
g2.drawString("Huh?", 10, 20);
break;
}
}//end paintComponent



//===================================================== mousePressed
public void mousePressed(MouseEvent e) {
_currentStartX = e.getX(); // save x coordinate of the click
_currentStartY = e.getY(); // save y
_currentEndX = _currentStartX; // set end to same pixel
_currentEndY = _currentStartY;
}//end mousePressed

//===================================================== mouseDragged
public void mouseDragged(MouseEvent e) {
_currentEndX = e.getX(); // save new x and y coordinates
_currentEndY = e.getY();
this.repaint(); // show new shape
}//end mouseDragged

//==================================================== mouseReleased
public void mouseReleased(MouseEvent e) {
// This will save the shape that has been dragged by
// drawing it onto the bufferedImage where all shapes
// are written.
_currentEndX = e.getX(); // save ending coordinates
_currentEndY = e.getY();

//--- Draw the current shape onto the buffered image.
Graphics2D grafarea = _bufImage.createGraphics();
drawCurrentShape(grafarea);

this.repaint();
}//end mouseReleased

//========================================== ignored mouse listeners
public void mouseMoved (MouseEvent evt) {


}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseClicked (MouseEvent evt) {
System.out.println("lovish::::::::::"+getShape());


}
}//endclass PaintPanel

this is the code. I am trying to add polygon functionality to it and make shapes non overlapping. Please tell now if you can
looka dicosta
Greenhorn

Joined: Nov 22, 2012
Posts: 21
looka dicosta wrote:
Kemal Sokolovic wrote:
looka dicosta wrote:I just want to use all the forums


You'll probably get an answer at one forum, which means people in other(s) were wasting time trying to help you.

Even if that argument of yours is valid (and it is not) you need to be more specific when posting a question, TellTheDetails and ShowSomeEffort (I can't see any of your code here); what have you tried so far, etc.? There are some guidelines here on how to aks questions, so I would recommend you read it.


package Circles;

//PaintDemo.java - Simple painting program.
//Illustrates use of mouse and BufferedImage.
//Fred Swartz 2002-12-02
//Possible Enhancements:
//* Clear drawing area
//* Other shapes (line, outline shapes, text, ...)
//* Color selection.
//* An eraser.
//* Create a real toobar.
//* Save/Load


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;

//////////////////////////////////////////////////////////////PaintDemo
class PaintDemo {
//============================================================= main
public static void main(String[] args) {
PaintWindow window = new PaintWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}//end main
}//endclass PaintDemo


////////////////////////////////////////////////////////////PaintWindow
class PaintWindow extends JFrame {
PaintPanel canvas = new PaintPanel();
//====================================================== constructor
public PaintWindow() {
//--- create the buttons
JButton circleButton = new JButton("Circle");
circleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.setShape(PaintPanel.CIRCLE);
}});
JButton rectangleButton = new JButton("Rectangle");
rectangleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.setShape(PaintPanel.RECTANGLE);
}});

JButton polyButton = new JButton("Polygon");
polyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.setShape(PaintPanel.POLY);
}});

//--- layout the buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2, 1));
buttonPanel.add(circleButton);
buttonPanel.add(rectangleButton);
buttonPanel.add(polyButton);

//--- layout the window
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(buttonPanel, BorderLayout.WEST);
content.add(canvas , BorderLayout.CENTER);
this.setTitle("Paint Demo");
this.pack();
}//end constructor
}//endclass PaintWindow


///////////////////////////////////////////////////////////// PaintPanel
class PaintPanel extends JPanel implements MouseListener,
MouseMotionListener {
//--- Public constants used to specify shape being drawn.
public static final int NONE = 0;
public static final int LINE = 1;
public static final int RECTANGLE = 2;
public static final int CIRCLE = 3;
public static final int POLY = 4;

//--- Variables to store the current figure info
private int _shape = NONE;
public int getShape() {
return _shape;
}
private int _currentStartX = 0; // where mouse first pressed
private int _currentStartY = 0;
private int _currentEndX = 0; // where dragged to or released
private int _currentEndY = 0;

//--- BufferedImage to store the underlying saved painting.
// Will be initialized first time paintComponent is called.
private BufferedImage _bufImage = null;
private boolean polygonIsNowComplete = false;

//--- Private constant for size of paint area.
private static final int SIZE = 600; // size of paint area

private final Point trackPoint = new Point();
private ArrayList points = new ArrayList();


//====================================================== constructor
public PaintPanel() {
setPreferredSize(new Dimension(SIZE, SIZE));
setBackground(Color.white);
//--- Add the mouse listeners.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}//endconstructor


//========================================================= setShape
public void setShape(int shape) {
//--- Provided so users can set the shape.
_shape = shape;
}//end setShape


//=================================================== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g; // downcast to Graphics2D
if (_bufImage == null) {
//--- This is the first time, initialize _bufImage
int w = this.getWidth();
int h = this.getHeight();
_bufImage = (BufferedImage)this.createImage(w, h);
Graphics2D gc = _bufImage.createGraphics();
gc.setColor(Color.white);
gc.fillRect(0, 0, w, h); // fill in background
}
g2.drawImage(_bufImage, null, 0, 0); // draw previous shapes

drawCurrentShape(g2);
}//end paintComponent


//================================================= drawCurrentShape
private void drawCurrentShape(Graphics2D g2) {
//--- Draws current shape on a graphics context, either
// on the context passed to paintComponent, or the
// context for the BufferedImage.
switch (_shape) {
case NONE :
break;

case CIRCLE:
g2.drawOval(_currentStartX, _currentStartY,
_currentEndX - _currentStartX,
_currentEndY - _currentStartY);
break;

case RECTANGLE:
g2.drawRect(_currentStartX, _currentStartY,
_currentEndX - _currentStartX,
_currentEndY - _currentStartY);
break;

case POLY:
g2.setColor(Color.red);
g2.fillOval(_currentStartX, _currentStartY, 8, 8);

default: // should never happen
g2.drawString("Huh?", 10, 20);
break;
}
}//end paintComponent



//===================================================== mousePressed
public void mousePressed(MouseEvent e) {
_currentStartX = e.getX(); // save x coordinate of the click
_currentStartY = e.getY(); // save y
_currentEndX = _currentStartX; // set end to same pixel
_currentEndY = _currentStartY;
}//end mousePressed

//===================================================== mouseDragged
public void mouseDragged(MouseEvent e) {
_currentEndX = e.getX(); // save new x and y coordinates
_currentEndY = e.getY();
this.repaint(); // show new shape
}//end mouseDragged

//==================================================== mouseReleased
public void mouseReleased(MouseEvent e) {
// This will save the shape that has been dragged by
// drawing it onto the bufferedImage where all shapes
// are written.
_currentEndX = e.getX(); // save ending coordinates
_currentEndY = e.getY();

//--- Draw the current shape onto the buffered image.
Graphics2D grafarea = _bufImage.createGraphics();
drawCurrentShape(grafarea);

this.repaint();
}//end mouseReleased

//========================================== ignored mouse listeners
public void mouseMoved (MouseEvent evt) {


}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseClicked (MouseEvent evt) {
System.out.println("lovish::::::::::"+getShape());


}
}//endclass PaintPanel

this is the code. I am trying to add polygon functionality to it and make shapes non overlapping. Please tell now if you can




Now where are you why are you not putting the answer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Campbell Ritchie,
Your post was moved to a new topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Please continue any discussion on the link in the previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java swing