storing and accessing mouse coordintates in an array

M

mannie

Hello!!

I have a problem, i have wrote a drawing package using java AWT and
swing comonents in which i can draw shapes on to a canvas such as
lines rectangles ovals, i have wrote each shape on to a BufferedImage
this enabled me to keep the any drawn images on the screen by
repainting the buffer everytime a new shapes is drawn.

But now i have relized that i need to store the shapes in an array
because i need direct access to drawn shapes in which the buffer does
not allow

i have tried using the Point class and storing these points in an
array and redrawing them when another shape has been sent to the
Paint() but to no avail

has anyone got any tips in hpw i may use the array correctly so i can
replace the use of the buffer

//this is the code for the working program its using the buffer so you
get an idea of what im doing there is a (main) method in it so i will
run directly when called

The code was nealty formatted but when but when pasted here it
unformatted
Thanks mannie




// import awt & swing packages
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.applet.*;



public class Draw extends JFrame // Extends the JFrame class
{
//-- shape buttons --//

private JButton line;
private JButton rect;
private JButton oval;
private JButton threeDRect;
private JButton exit;
private JButton canvasText;
private int width;
private String writeText ="";

//-- color buttons --//

private JButton black;
private JButton cyan;
private JButton red;
private JButton blue;
private JButton green;
private JButton yellow;
private JButton orange;
private JButton white;
private JButton pink;
private JButton colorPalette;

//-- text fonts --//

private Font plainFont;
private Font boldFont;
private Font italicFont;
private Font boldItalicFont;


//-- radio buttons --//

private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;

//-- line appearence --//

private JComboBox Line;
final String name[] = {"Plain","Dashed"};


//-- alternitive shape selection --//

private JComboBox altSelection;
final String altShape[] = {"Select
Shape","Line","Rectangle","Oval","threeDRect"};

//-- mouse coordinate variables --//

private int mx = 0;
private int my = 0;
private int mx1 = 0;
private int my1 = 0;

//-- component panels --//

private JPanel palette;
private JPanel tools;
private JPanel toppanel;
private JPanel bottomPanel;

private Font currentFont; // stores the selected font
private Color currentColor; // stores selected color
private int currentTool = 0; // assosiates event with a number
private JCheckBox fillShapes; // indicates shapes to be filled
private JLabel statusBar; // mouse coordinate label
private BufferedImage bufferedImage = null; // stores predrawn
images
private MyCanvas drawarea; // canvas drawing area


public Draw()
//-- start class constuctor --//
{
super("CAD Drawing package");


//-- used for the color and font of drawing objects --//
currentColor = new Color(0,0,0); // create currentColor object
currentFont = new Font("",0,0); // create currentFont object

boldFont = new Font("Serif", Font.BOLD, 14);// create bold font
object


//--- window to add panals and drawing canvas ---//
Container container = getContentPane(); // initialize
container
container.setLayout(new BorderLayout()); // set container layout

palette = new JPanel(); //-- create palette panel --//
palette.setLayout(new GridLayout(10,1,0,6));// set panel layout
palette.setBackground(Color.ORANGE); // set background color

colorPalette =
new JButton("Advanced Color Options"); // create color palette
button
palette.add(colorPalette); // add color palette button

black = new JButton("black"); // create black button
black.setBackground(Color.black); // set button back ground color
black.setForeground(Color.white); // set text color on button
palette.add(black); // add black button

cyan = new JButton("cyan"); // create cyan button
cyan.setBackground(Color.cyan); // set button back ground color
palette.add(cyan); // add cyan button

red = new JButton("red"); // create red button
red.setBackground(Color.red); // set button back ground color
palette.add(red); // add red button

blue = new JButton("blue"); // create blue button
blue.setBackground(Color.blue); // set button back ground color
palette.add(blue); // add blue button

green = new JButton("green"); // create green button
green.setBackground(Color.green); // set button back ground color
palette.add(green); // ad green button

yellow = new JButton("yellow"); // create yellow button
yellow.setBackground(Color.yellow); // set button back ground
color
palette.add(yellow); // add yellow button

orange = new JButton("orange"); // create orange button
orange.setBackground(Color.orange); // set button back ground
color
palette.add(orange); // add orange button

white = new JButton("white"); // create white button
white.setBackground(Color.white); // set button back ground color
palette.add(white); // add whit button

pink = new JButton("pink"); // create pink button
pink.setBackground(Color.pink); // set button back ground color
palette.add(pink); // add pink button
container.add(palette,"East"); //-- add palette to container
--//


tools = new JPanel(); //-- create tools panel --//
tools.setLayout(new GridLayout(8,5,5,5)); // set panel layout
tools.setBackground(Color.ORANGE); // set background color


//-- create a combobox for alternitive selection --//
altSelection = new JComboBox(altShape); // create altSelection
altSelection.setMaximumRowCount(4); // set row count
altSelection.setEditable(false); // set editable region to false
tools.add(altSelection);

line = new JButton("Line"); // create line button
tools.add(line); // add exit button

rect = new JButton("Rectangle"); // create rectangle button
tools.add(rect); // add rectanle button

oval = new JButton("Oval"); // create oval button
tools.add(oval); // add oval button

threeDRect = new JButton("3D Rectangle"); // create 3D rectangle
button
tools.add(threeDRect); // add 3D Rectangle button

canvasText = new JButton
("Write Text on Screen"); // create canvasText button
tools.add(canvasText); // add canvasText button


fillShapes = new
JCheckBox("Fill Shapes",false); // create fillshapes button
tools.add(fillShapes); // add fill shapes button
container.add(tools,"West"); //-- add panel to container --//


bottomPanel = new JPanel(); //-- create bottompanel --//
bottomPanel.setLayout
(new GridLayout(1,1,0,1)); // set layout of bottomPanel
bottomPanel.setBackground(Color.BLACK); // set background color of
panel

statusBar = new JLabel(); // create a statusBar label
statusBar.setForeground(Color.white); // make text font yellow
bottomPanel.add(statusBar); // add staus bar to the bottomPanel

bottomPanel.add
(statusBar, BorderLayout.SOUTH); // add statusBar to bottomPanel
container.add(bottomPanel,"South"); //-- add bottomPanel to
container --//


toppanel = new JPanel(); //-- create top panel --//
toppanel.setLayout(new GridLayout(1,5,2,2));// set panel layout
toppanel.setBackground(Color.ORANGE); // set background color

exit = new JButton("Exit Application"); // create exit button
toppanel.add(exit); // add exit button


container.add(toppanel,"North"); //-- add top panel to
container--//

//-- creates an area to draw graphic objects --//
drawarea = new MyCanvas();

container.add(drawarea,"Center"); // fix on center of container
validate(); // Ensures that this component has a valid
layout.
setSize( 800,600); // set the canvas size
setVisible(true); // set visible



//-- event handler object to listen for events --//
ActionEventHandler handler = new ActionEventHandler();

//-- add listener to all color buttons --//
cyan.addActionListener(handler); // add actionlistener to cyan
button
red.addActionListener(handler); // add actionlistener to red
button
yellow.addActionListener(handler); // add actionlistener to yellow
button
black.addActionListener(handler); // add actionlistener to black
button
blue.addActionListener(handler); // add actionlistener to blue
button
green.addActionListener(handler); // add actionlistener to green
button
orange.addActionListener(handler); // add actionlistener to orange
button
white.addActionListener(handler); // add actionlistener to white
button
pink.addActionListener(handler); // add actionlistener to pink
button
colorPalette.addActionListener(handler); // add actionlistener to
color palette button

//-- add listener to tool buttons --//
oval.addActionListener(handler); // add actionlistener to oval
button
rect.addActionListener(handler); // add actionlistener to rect
button
line.addActionListener(handler); // add actionlistener to pink
button
threeDRect.addActionListener(handler); // add actionlistener to
pink button
altSelection.addActionListener(handler); // add actionlistener to
pink button
exit.addActionListener(handler); // add actionlistener to pink
button
canvasText.addActionListener(handler); // add actionlistener to
clear button


}// end Drawings constructor
public void keyPressed(KeyEvent evt)
{
}

private class ActionEventHandler implements ActionListener
// handles the events of application components
{
public void actionPerformed(ActionEvent evt)
// if the user clicks a button on the toolbar then change the value
// of currentTool and call filled.setEnable appropriately
{
if (evt.getSource() == line)
{
currentTool = 0;
fillShapes.setEnabled(false);

}
else if (evt.getSource() == rect)
{
currentTool = 1;
fillShapes.setEnabled(true);

}
else if (evt.getSource() == oval)
{
currentTool = 2;

fillShapes.setEnabled(true);
}
else if (evt.getSource() == threeDRect)
{
currentTool = 3;
fillShapes.setEnabled(true);
}
else if (evt.getSource() == threeDRect)
{
currentTool = 3;
fillShapes.setEnabled(true);
}
else if (evt.getSource() == exit)
{
System.exit(0);
}
else if (evt.getSource() == canvasText)
{
//-- capture text string to display --//

writeText =
JOptionPane.showInputDialog( "Enter a text string, then click on
the canvas" );
currentTool = 4;
}


else if (evt.getSource() == colorPalette)
// JColorChosser for more spectrum colors
currentColor = JColorChooser.showDialog(Draw.this,
"Choose a color", currentColor);

else if( (evt.getSource() == altSelection))
// envoked when color combobox options are selected
{
String output = (String)altSelection.getSelectedItem();
// store the selection string from the combobox

// if the output matches "Line" we can draw a Line
if (output.equals("Line"))
currentTool = 0;

// if the output matches "Rect" we can draw a Rectangle
if (output.equals("Rectangle"))
currentTool = 1;

// if the output matches "oval" we can draw a Oval
if(output.equals("Oval"))
currentTool = 2;

// if the output matches "3dRect"we can draw a 3dRect
if(output.equals("threeDRect"))
currentTool = 3;
}

// if a color button is pressed set
// currentColor to the required color

else if (evt.getSource() == black)
currentColor = new Color(0,0,0);

else if (evt.getSource() == cyan)
currentColor = new Color(0,255,255);

else if (evt.getSource() == red)
currentColor = new Color(255,0,0);

else if (evt.getSource() == blue)
currentColor = new Color(0,0,255);

else if (evt.getSource() == green)
currentColor = new Color(0,255,0);

else if (evt.getSource() == yellow)
currentColor = new Color(255,255,0);

else if (evt.getSource() == orange)
currentColor = new Color(255,200,0);

else if (evt.getSource() == white)
currentColor = new Color(255,255,255);

else if (evt.getSource() == pink)
currentColor = new Color(255,175,175);

}//end actionPerformed

}//ActionEventHandler


// Create a new class called MyCanvas which extends the standard
class Canvas
class MyCanvas extends JPanel implements MouseListener,
MouseMotionListener
{
public MyCanvas()
// The classes constructor
{
addMouseListener(this);
addMouseMotionListener(this);

}// end constructor

public void mousePressed(MouseEvent e)
// actions performed when pressing the left mouse button
{
mx = e.getX();
my = e.getY();
mx1 = e.getX();
my1 = e.getY();

}// mousePressed


public void mouseReleased(MouseEvent e)
// actions are performed when dragging the mouse
{

// this will save the shape that has been dragged by
// drawing it onto the bufferedImage where all shapes
// are written

mx1 = e.getX();
my1 = e.getY();

// draw the current shape onto the buffered image
Graphics2D graphic = bufferedImage.createGraphics();
paint(graphic);
this.repaint();

}// mouseReleased

public void mouseDragged(MouseEvent e)
{
mx1 = e.getX();
my1 = e.getY();




this.repaint();

}// mouseDragged

public void mouseMoved( MouseEvent e )
{
statusBar.setText( String.format( "Mouse moved at [%d, %d]",
e.getX(), e.getY() ) );

} // end method mouseMoved


public void update(Graphics g)
{
// wont clear screen when mouse moves.
paint(g);
}


//-- unused mouse event methods --//
public void mouseClicked (MouseEvent e){}
public void mouseEntered (MouseEvent e){}
public void mouseExited (MouseEvent e){}


public void paintComponent(Graphics g)
// paints graphics
{

super.paintComponent(g);
// call to super class constructor
Graphics2D g2D = (Graphics2D)g; // downcast g to Graphics2D

if (bufferedImage == null)
{
//-- if this is the first time, initialize bufImage
int width = this.getWidth();
int height = this.getHeight();
bufferedImage = (BufferedImage)this.createImage(width,
height);
}

g2D.drawImage(bufferedImage, null, 0, 0); // draw previous
shapes
g2D.setColor(currentColor); // changes colour to that
selected
drawCurrentShape(g2D); // send g to be drawn

}//end paint


public void drawCurrentShape(Graphics2D g2D)
// objects are draw from paint method or from the bufferedReader
{
switch(currentTool)
{
case 0: g2D.drawLine(mx,my,mx1,my1);

case 1: if (fillShapes.isSelected()==true)
g2D.fillRect(mx,my,mx1-mx,my1-my);
else g2D.drawRect(mx,my,mx1-mx,my1-my);
break;

case 2: if (fillShapes.isSelected()==true)
g2D.fillOval(mx,my,mx1-mx,my1-my);
else g2D.drawOval(mx,my,mx1-mx,my1-my);
break;

case 3: if (fillShapes.isSelected()==true)
g2D.fill3DRect(mx,my,mx1-mx,my1-my,false);
else g2D.draw3DRect(mx,my,mx1-mx,my1-my,false);
break;

case 4: g2D.drawString(writeText,mx,my);

break;

default: g2D.drawString("Draw inside the screen!!", 20, 20);
break;

}// end switch

}//end drawcurrentshape

} // MyCanvas



public static void main( String args[])
// create new Draw object and initialize
{

Draw application = new Draw();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
}

}// end draw
 
B

Boudewijn Dijkstra

mannie said:
Hello!!

I have a problem, i have wrote a drawing package using java AWT and
swing comonents in which i can draw shapes on to a canvas such as
lines rectangles ovals, i have wrote each shape on to a BufferedImage
this enabled me to keep the any drawn images on the screen by
repainting the buffer everytime a new shapes is drawn.

But now i have relized that i need to store the shapes in an array
because i need direct access to drawn shapes in which the buffer does
not allow

What do you mean? Does the buffer give an access violation error?
i have tried using the Point class and storing these points in an
array and redrawing them when another shape has been sent to the
Paint() but to no avail

What was the problem?
has anyone got any tips in hpw i may use the array correctly so i can
replace the use of the buffer

Which array?
//this is the code for the working program its using the buffer so you
get an idea of what im doing there is a (main) method in it so i will
run directly when called

[...]
 
M

mannie

Boudewijn Dijkstra said:
mannie said:
Hello!!

I have a problem, i have wrote a drawing package using java AWT and
swing comonents in which i can draw shapes on to a canvas such as
lines rectangles ovals, i have wrote each shape on to a BufferedImage
this enabled me to keep the any drawn images on the screen by
repainting the buffer everytime a new shapes is drawn.

But now i have relized that i need to store the shapes in an array
because i need direct access to drawn shapes in which the buffer does
not allow

What do you mean? Does the buffer give an access violation error?
i have tried using the Point class and storing these points in an
array and redrawing them when another shape has been sent to the
Paint() but to no avail

What was the problem?
has anyone got any tips in hpw i may use the array correctly so i can
replace the use of the buffer

Which array?
//this is the code for the working program its using the buffer so you
get an idea of what im doing there is a (main) method in it so i will
run directly when called

[...]

The problem is that storing the shapes in a buffer does not allow me
to delete the last drawn shape onto the canvas. I also need direct
access to each shape stored in the buffer so i can move shapes about
on the screen ths requires access to the shapes (x,y) co-ordinates

This is not possible using a bufferedimages as no methods exist to
access each shape held in it.

public void mouseReleased(MouseEvent e)
// actions are performed when dragging the mouse
{

// this will save the shape that has been dragged by
// drawing it onto the bufferedImage where all shapes
// are written

mx1 = e.getX();
my1 = e.getY();

// draw the current shape onto the buffered image
Graphics2D graphic = bufferedImage.createGraphics();
paint(graphic);
this.repaint();

}// mouseRelease


when the mouse button has been released after draging the area off a
shape, on the canvas the the coordinates ((x,y)) are writen to the
buffer then sent to the paint method below

public void paintComponent(Graphics g)
// paints graphics
{

super.paintComponent(g);
// call to super class constructor
Graphics2D g2D = (Graphics2D)g; // downcast g to Graphics2D

if (bufferedImage == null)
{
//-- if this is the first time, initialize bufImage
int width = this.getWidth();
int height = this.getHeight();
bufferedImage = (BufferedImage)this.createImage(width,
height);
}

g2D.drawImage(bufferedImage, null, 0, 0); // draw previous
shapes
g2D.setColor(currentColor); // changes colour to that
selected
drawCurrentShape(g2D); // send g to be drawn

}//end paint


public void drawCurrentShape(Graphics2D g2D)
// objects are draw from paint method or from the bufferedReader
{
switch(currentTool)
{
case 0: g2D.drawLine(mx,my,mx1,my1);

case 1: if (fillShapes.isSelected()==true)
g2D.fillRect(mx,my,mx1-mx,my1-my);
else g2D.drawRect(mx,my,mx1-mx,my1-my);
break;

case 2: if (fillShapes.isSelected()==true)
g2D.fillOval(mx,my,mx1-mx,my1-my);
else g2D.drawOval(mx,my,mx1-mx,my1-my);
break;

case 3: if (fillShapes.isSelected()==true)
g2D.fill3DRect(mx,my,mx1-mx,my1-my,false);
else g2D.draw3DRect(mx,my,mx1-mx,my1-my,false);
break;

case 4: g2D.drawString(writeText,mx,my);

break;

default: g2D.drawString("Draw inside the screen!!", 20, 20);
break;

}// end switch

}//end drawcurrentshape

} // MyCanvas

this works fine but i now want to use the same process but using an
array instead of a buffer i have tried various ways but nothing works
my main problem is keep the images off shapes on the screen after one
has been draw my mouse drags all i need is a point in the right
direction

Thank for your time
 
B

Boudewijn Dijkstra

The problem is that storing the shapes in a buffer does not allow me
to delete the last drawn shape onto the canvas. I also need direct
access to each shape stored in the buffer so i can move shapes about
on the screen ths requires access to the shapes (x,y) co-ordinates

You can give each shape it's own buffer. Or better, an object that describes
the shape, allowing it to be drawn repeatedly.
 
M

mannie

You can give each shape it's own buffer.

how could i give a shape its own buffer?


Or better, an object that describes
the shape, allowing it to be drawn repeatedly.

how could i create an object that describles the shape?

i have tried to using arrays many ways, i even tried to store multiple
copies of the buffer in an array but this did not work.

Thanks
 
B

Boudewijn Dijkstra

mannie said:
how could i give a shape its own buffer?

Create multiple buffers, and assign them to your shapes.
how could i create an object that describles the shape?

Try object-oriented design!
i have tried to using arrays many ways, i even tried to store multiple
copies of the buffer in an array but this did not work.

What went wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top