Using eclipse, I wrote a program trying to copy mspaint in Windows,there is no errors when build or run, I only complete several functions to test if it can be paint.
I wrote the class named "drawings" to sotre drawings which has been paint,
if I set current=0,it shows nothing ; if I set current=1, a circle shows up but when I release mouse,the circle disappear.
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDraw extends JFrame{
static int current = 0; // choice of different drawings, 0 means pencil
// 1 means circle
static int index=0; // the number of drawings
static int R,G,B; // the color of drawings
static float Stroke=17.0f; // the stroke of drawings
JLabel statusBar = new JLabel();// show the state of mouse
drawings []indexList=new drawings[5000];// store drawings to paint
DrawArea drawArea = new DrawArea();
public MyDraw(){
R=G=B=0;// initialize color
//add components to main window
add(drawArea);
setVisible(true);
setSize(1000,1000);
createNewItem(); // new a drawing
add(statusBar, BorderLayout.SOUTH);
show();
}
// new a drawing
public void createNewItem(){
switch(current){
case 0 :indexList[index]=new pencil();break;
case 1 :indexList[index]=new circle();break;
}
}
public static void main(String args[]){
new MyDraw();
}
// the panel to paint on
class DrawArea extends JPanel{
public DrawArea(){
setBackground(Color.white);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
addMouseListener(new MousePolice1());
addMouseMotionListener(new MousePolice2());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
for(int j=0;j<=index;j++){
pass(indexList[index],g2d);
}
}
public void pass(drawings i,Graphics2D g2d){
i.draw(g2d);
}
}
class MousePolice2 extends MouseAdapter{
public void mouseDragged(MouseEvent e){
statusBar.setText(" Mouse Dragged #:[" + e.getX() +
", " + e.getY() + "]");
if (current == 0) {
indexList[index - 1].x1 = indexList[index].x2 =
indexList[index].x1 = e.getX();
indexList[index - 1].y1 = indexList[index].y2 =
indexList[index].y1 = e.getY();
index++;
createNewItem();
} else {
indexList[index].x2 = e.getX();
indexList[index].y2 = e.getY();
}
repaint();
}
}
class MousePolice1 extends MouseAdapter{
public void mousePressed(MouseEvent e){
statusBar.setText(" Mouse Pressed #:[" + e.getX() +
", " + e.getY() + "]");
indexList[index].x1 = indexList[index].x2 = e.getX();
indexList[index].y1 = indexList[index].y2 = e.getY();
if (current == 0 ) {
indexList[index].x1 = indexList[index].x2 = e.getX();
indexList[index].y1 = indexList[index].y2 = e.getY();
index++;
createNewItem();
}
}
public void mouseReleased(MouseEvent e){
statusBar.setText(" Mouse Released #:[" + e.getX() +
", " + e.getY() + "]");
if (current ==0) {
indexList[index].x1 = e.getX();
indexList[index].y1 = e.getY();
}
indexList[index].x2 = e.getX();
indexList[index].y2 = e.getY();
repaint();
index++;
createNewItem();
}
}
}
//the father
class drawings implements Serializable{
int x1,x2,y1,y2;
void draw(Graphics2D g2d){}
}
// the drawing pencil
class pencil extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(MyDraw.R,MyDraw.G,MyDraw.B));
g2d.setStroke(new BasicStroke(MyDraw.Stroke,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1, y1, x2, y2);
}
}
class circle extends drawings
{
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(MyDraw.R,MyDraw.G,MyDraw.B));
g2d.setStroke(new BasicStroke(MyDraw.Stroke));
g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)));
}
}
Didn't I see it or where is the method show()?
Otherwise I just saw some programs which they need to do something like this with the windows:
setVisible(true);
Related
I want to implement a Rectangle shape feature exactly as in Paint in JAVA. I have built a program as following. I have built a class MyPaint where buttons and frame are defined. I have built another class inside the same program PadDraw, where a drawing pad is created where I can draw with pencil like in Paint. Then I have another class outside the program DrawRect where the rectangle shape feature is created.
I want to know if there is a way to integrate the rectangle in a way that if I click a button "Rectangle", the way of drawing should change and instead of drawing with pencil, I should draw rectangle shapes exactly like in Paint when the rectangle shape is pressed.
The piece of code for PadDraw class is as following:
class PadDraw extends JComponent {
private Image image;
private Graphics2D graphics2D;
private int currentX , currentY , oldX , oldY ;
public PadDraw(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentX = e.getX();
currentY = e.getY();
if(graphics2D != null)
graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
public void paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D)image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 5, 5, null);
}
While the piece of code of DrawRect class that I want to integrate in the program where MyPaint and PadDraw class are located is as following:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawRect extends JPanel {
int x, y, x2, y2;
public static void main(String[] args) {
JFrame f = new JFrame("Draw Box Mouse 2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new DrawRect());
f.setSize(300, 300);
f.setVisible(true);
}
DrawRect() {
x = y = x2 = y2 = 0; //
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
}
public void setStartPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setEndPoint(int x, int y) {
x2 = (x);
y2 = (y);
}
public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
int px = Math.min(x,x2);
int py = Math.min(y,y2);
int pw=Math.abs(x-x2);
int ph=Math.abs(y-y2);
g.drawRect(px, py, pw, ph);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
drawPerfectRect(g, x, y, x2, y2);
}
}
I am not very experimented with listeners. I have a Vector of Line2D (representating a floorplan walls). I draw them in a JFrame.
I try to set a listener on those lines to be able to set them (change color etc) on click.
I am lost because I don't really know where to put the listener. I tried many things I found on many forums but nothing works really.
Thanks.
Edit1:
hi thank you for your answer it's a kind of big projects with many classes so i will post the code and describe how it wokrs . First . I import an image with a floorplan. I have a class with Actions that extends AbstractAction. I use opencv for for lines extraction . I store the extracted lines in a Vector of Walls (class i created).
Vector<wall> walls
And here is my wall class
//a wall is represented by 2 points (pInitial and pFInal)
private Point pInitial, pFinal;
private double wallAttenuation;
private int wallType;
public wall(Point p1, Point p2) {
// p1 is lower by the height than p2
if (p1.getY() > p2.getY()) {
pInitial = p2;
pFinal = p1;
} else {
pInitial = p1;
pFinal = p2;
}
}
//method to set the attenuation of the wall
public void setWallAttenuation(double a) {
wallAttenuation = a;
}
//methods to set both ends of the wall
public void setPInitial(Point P1) {
pInitial.x=(int)P1.getX();
pInitial.x=(int)P1.getY();
}
public void setPFinal(Point P2) {
pFinal.x=(int)P2.getX();
pFinal.x=(int)P2.getY();
}
//methods to get wall attributes (attenuation , ends and type)
public double getWallAttenuation() {
return wallAttenuation;
}
public Point getPInitial() {
return pInitial;
}
public Point getPFinal() {
return pFinal;
}
public void setWallType(int type) {
wallType = type;
}
public int getWallType() {
return wallType;
}
now in my importAction class, and after processing I have this vector of walls (each wall defiened by start and end point . After that , I open a new Frame in which i draws the lines. I want to be able to click on each wall for further modifications.
First i tried to create a Frame class with mouse event handling like this :
public class CreateJFrameWindowWithMouseEventHandling extends JFrame implements MouseMotionListener,MouseListener {
private static final long serialVersionUID = 1L;
public CreateJFrameWindowWithMouseEventHandling() {
setTitle("Walls calibration");
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Clicked at X: " + x + " - Y: " + y);
}
#Override
public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);
}
#Override
public void mouseExited(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);
}
#Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Pressed at X: " + x + " - Y: " + y);
}
#Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Released at X: " + x + " - Y: " + y);
}
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse at X: " + x + " - Y: " + y);
repaint();
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new CreateJFrameWindowWithMouseEventHandling();
//Display the window.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
but i guess it wasnt the right solution . next thing i tried was to create JPanel browse the vector , draw the current line and add a listener. to check if it works i change the cursor if the mouse is over a line :
JFrame wallsEdit = new JFrame("Walls Calibration");
wallsEdit.add(new JPanel() {
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for(int i=0;i<walls.size();i++)
{
Point p1 = walls.get(i).getPInitial();
Point p2 = editor.walls.get(i).getPFinal();
g2.setColor(Color.black);
g2.setStroke(new BasicStroke(10));
g2.drawLine((int)p1.getX(),(int)p1.getY(), (int)p2.getX(),(int)p2.getY());
walls.get(i).addMouseListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
Point CursorPoint= MouseInfo.getPointerInfo().getLocation();
double x = CursorPoint.getX();
double y = CursorPoint.getY();
Line2D.Double current = new Line2D.Double(p1.getX(),p1.getY(),p2.getX(),p2.getY()) ;
if(current.intersectsLine(x, y, x, y))
{ setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
} else { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
repaint();
});
}
}
}, BorderLayout.CENTER);
wallsEdit.setAlwaysOnTop(true);
wallsEdit.setVisible(true);
wallsEdit.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
and of course this doesnt work. so tried another thing. i declared my Wall class extends Component and implements mouseMotionListener and MouseListener. inside I wrote the mouseMoved event like for the panel .
hope that i gave all needed details . any help would be appreciated thank you very much
So right now I have a program that draws a circle when I right click, and a square when I left click, and when I shift click it clears the screen.
• What I want to do is to be able to drag and make the mouse leave a trail of figures as I drag. How do I do that? Here is my program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;`
public class SimpleStamper extends JPanel implements MouseListener {
public static void main(String[] args) {
JFrame window = new JFrame("Simple Stamper");
SimpleStamper content = new SimpleStamper();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.setSize(400,300);
window.setVisible(true);
}
// ------------------------------------------------------------------
public SimpleStamper() {
setBackground(Color.BLACK);
addMouseListener(this);
}
public void mousePressed(MouseEvent evt) {
if ( evt.isShiftDown() ) {
repaint();
return;
}
int x = evt.getX();
int y = evt.getY();
Graphics g = getGraphics();
if ( evt.isMetaDown() ) {
g.setColor(Color.RED);
g.fillOval( x - 30, y - 30, 60, 60 );
g.setColor(Color.RED);
g.drawOval( x - 30, y - 30, 60, 60 );
}
else {
g.setColor(Color.PINK);
g.fillRect( x - 15, y - 15, 30, 30 );
g.setColor(Color.PINK);
g.drawRect( x - 15, y - 15, 30, 30 );
}
g.dispose();
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
Basically you need to add a MouseMotionListener, same as a MouseListener.
public class SimpleStamper
extends JPanel implements MouseListener, MouseMotionListener {
List<Shape> shapes = new ArrayList<>();
public void mouseDragged(MouseEvent evt) {
int x = evt.getX();
int y = evt.getY();
//... create another shape and add it to the list of shapes
// (see Adrian's comment)
}
public void mouseMoved(MouseEvent evt) {
}
//...
}
To continue the same shape, you must store the "current" shape in a class variable, to be done in mousePressed.
Note that this produces nothing but a wide line tracking the mouse movement. If you want to have distinct shapes drawn, you have to keep track of the last x and y, and create another shape only if the distance is greater than some parameter DMIN.
Below is a full rewrite.
public class Drag
extends JPanel implements MouseListener, MouseMotionListener {
public static void main(String[] args) {
JFrame window = new JFrame("Simple Stamper");
Drag content = new Drag();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.setSize(400,300);
window.setVisible(true);
}
public Drag() {
setBackground(Color.BLACK);
addMouseListener(this);
addMouseMotionListener( this );
}
// ------------------------------------------------------------------
private java.util.List<Shape> shapes = new ArrayList<>();
private enum Form {
CIRCLE, SQUARE;
}
private Form lastForm;
private int lastX;
private int lastY;
private static final int DMIN = 20;
private void addShape( Form form, int x, int y, int a ){
switch( form ){
case CIRCLE:
shapes.add( new Ellipse2D.Double( x-a/2.0, y-a/2.0, a, a ) );
break;
case SQUARE:
shapes.add( new Rectangle2D.Double( x-a/2.0, y-a/2.0, a, a ) );
break;
}
repaint();
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
for( Shape s: shapes ){
if( s instanceof Rectangle2D ){
g2.setColor(Color.PINK);
} else {
g2.setColor(Color.RED);
}
g2.fill( s );
g2.draw( s );
}
}
public void mouseDragged(MouseEvent evt) {
int x = evt.getX();
int y = evt.getY();
int dx = lastX - x;
int dy = lastY - y;
if( dx*dx + dy*dy > DMIN*DMIN ){
lastX = x;
lastY = y;
addShape( lastForm, lastX, lastY, 60 );
}
}
public void mouseMoved(MouseEvent evt) {
}
public void mousePressed(MouseEvent evt) {
if ( evt.isShiftDown() ) {
shapes.clear();
repaint();
return;
}
lastX = evt.getX();
lastY = evt.getY();
if ( evt.isMetaDown() ) {
lastForm = Form.CIRCLE;
} else {
lastForm = Form.SQUARE;
}
addShape( lastForm, lastX, lastY, 60 );
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
Working on an applet that draws 2 eyes and uses MouseMotionListener to move the they eyes. Also when the mouse exits the content pane, the eyes look straight. The one thing
I'm struggling with is I can't figure out how to restrict the pupils movements to stay within the eye. Any suggestions you guys have would be great.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WatchMe extends JApplet
{
private int leftMouseX = 130;
private int leftMouseY = 155;
private int rightMouseX = 305;
private int rightMouseY = 155;
private boolean mouseEntered;
//init method
public void init()
{
//set background to green
getContentPane().setBackground(Color.green);
//add mouse listener
addMouseListener(new MyMouseListener());
//add a motion listener
addMouseMotionListener(new MyMouseMotionListener());
}
public void paint(Graphics g)
{
//call superclass paint method
super.paint(g);
//draw left eye
g.setColor(Color.yellow);
g.fillOval(75, 100, 150, 150);
//draw left pupil
g.setColor(Color.black);
g.fillOval(leftMouseX, leftMouseY, 40, 40);
//draw right eye
g.setColor(Color.yellow);
g.fillOval(250, 100, 150, 150);
//draw right pupil
g.setColor(Color.black);
g.fillOval(rightMouseX, rightMouseY, 40, 40);
//checks to see if the mouse is in the pane, if not
//sets the x,y coordinates to look straight
if (! mouseEntered)
{
leftMouseX = 130;
leftMouseY = 155;
rightMouseX = 305;
rightMouseY = 155;
repaint();
}
}
private class MyMouseListener implements MouseListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
mouseEntered = true;
repaint();
}
public void mouseExited(MouseEvent e)
{
mouseEntered = false;
repaint();
}
}
private class MyMouseMotionListener implements MouseMotionListener
{
public void mouseDragged(MouseEvent e)
{
leftMouseX = e.getX();
leftMouseY = e.getY();
rightMouseX = e.getX();
rightMouseY = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
leftMouseX = e.getX();
leftMouseY = e.getY();
rightMouseX = e.getX();
rightMouseY = e.getY();
repaint();
}
}
}
As a start I would recommend putting your eye dimensions in variables as shown below. This way you reduce the chance of errors when entering the same numbers more than once, and if you later decide to change the eye size or position, you only have to change it once.
public static final int LEFT_X = 75;
public static final int LEFT_Y = 100;
public static final int EYE_SIZE = 150;
We also need the left pupil to be independent of the mouse (so it doesn't follow the mouse out of the eye) so we'll do this:
private leftPupilX = 130;
private leftPupilY = 155;
Next you need to determine whether or not the mouse is currently in the left eye. This is what will restrict the pupil to the bounds of the eye. I've done this in a method for convenience.
public void setLeftEye() {
//Set the X Coord for the pupil
//Mouse is to the left of the eye
if(leftMouseX < LEFT_X) {
leftPupilX = LEFT_X;
//Mouse is to the right of the eye
} else if(leftMouseX > LEFT_X + EYE_SIZE) {
leftPupilX = LEFT_X + EYE_SIZE;
//Mouse is in eye
} else {
leftPupilX = leftMouseX;
}
//Set the Y Coord for the pupil
//Mouse is above the eye
if(leftMouseY < LEFT_Y) {
leftPupilY = LEFT_X;
//Mouse is below the eye
} else if(leftMouseY > LEFT_Y + EYE_SIZE) {
leftPupilY = LEFT_Y + EYE_SIZE;
//Mouse is in eye
} else {
leftPupilY = leftMouseY;
}
}
Finally you'll need to update the code that draws the left pupil to reflect the variable change, and actually call our new method.
//draw left pupil
setLeftEye();
g.setColor(Color.black);
g.fillOval(leftPupilX, leftPupilY, 40, 40);
These changes should make the left eye track your mouse the way you described. You'll need to do something similar for the right eye, since it has different coordinates. If you have any problems, let me know and I will try to help. :)
I was given the assignment to make a simple paint program in java that utilizes a GUI and has basic I/O capabilities. That was all I was told to do by my professor. However, I've only made one GUI program before, so jumping straight into this paint program has been a headache. Now I'm nearly done, but the program isn't really behaving as I expected. When new objects are drawn on the Panel, they draw invisible white rectangles on the objects underneath them that erases those objects. I think this is the result of the repaint(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1); method in DrawShapes, but can't think of a way to fix it.
On the other hand, the objects are also not saving properly. I can get it to export a jpg as I intended, however, it will only export the last image drawn and not everything on the paintComponent canvas.
Lastly, the clear method in DrawShapes is working in a very similar way. When the clear method is activated, it will clear everything but the last image drawn.
Is there anyone more familiar than me with these tools that can see a way to fix these? This is only the first program I've utilized draw on, and I/O.
Here is the class for the panel that the shapes are supposed to drawn on:
/**
* #author me
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class DrawShapes extends JPanel{
Point startPoint = null;
Point endPoint = null;
public int drawType = 1;
BufferedImage image;
Graphics2D g2d;
public DrawShapes(){
setBackground(Color.WHITE);
MyMouseListener m1 = new MyMouseListener();
addMouseListener(m1);
addMouseMotionListener(m1);
}//end constructor
//sets draw type, which is the decider of what is being drawn.
public void setType(int type){
if(type == 1)
{
drawType = 1;
}
else if(type == 2)
{
drawType = 2;
}
else if(type == 3)
{
drawType = 3;
}
}//end setType
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image == null)
{
createEmptyImage();
}
g.drawImage(image, 0, 0, null);
if (startPoint != null && endPoint != null)
{
int x = Math.min(startPoint.x, endPoint.x);
int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(startPoint.x - endPoint.x);
int height = Math.abs(startPoint.y - endPoint.y);
switch (drawType)
{
case 1:
g.drawRect(x, y, width, height);
break;
case 2:
g.drawOval(x, y, width, height);
break;
case 3:
g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
break;
}
}
}//end paintComponent
public void save()
{
BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
this.paint(g);
g.dispose();
try{ImageIO.write(bi, "png",new File("test.png"));
}catch (Exception e){}
}
private void createEmptyImage()
{
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
g2d = (Graphics2D)image.getGraphics();
g2d.setColor(Color.BLACK);
g2d.drawString("Add a shape by clicking and dragging.", 40, 15);
}
public void addRect(int x, int y, int width, int height, Color color)
{
g2d.setColor( color );
g2d.drawRect(x, y, width, height);
repaint();
}
public void addOval(int x, int y, int width, int height, Color color)
{
g2d.setColor( color );
g2d.drawOval(x, y, width, height);
repaint();
}
public void addLine(int x1, int y1, int x2, int y2, Color color)
{
g2d.setColor(color);
g2d.drawLine(x1, y1, x2, y2);
repaint();
}
public void clear()
{
createEmptyImage();
repaint();
}
class MyMouseListener extends MouseInputAdapter
{
private int xMin;
private int xMax;
private int yMin;
private int yMax;
public void mousePressed(MouseEvent e)
{
startPoint = e.getPoint();
endPoint = startPoint;
xMin = startPoint.x;
xMax = startPoint.x;
yMin = startPoint.y;
yMax = startPoint.y;
}
public void mouseDragged(MouseEvent e)
{
//This is code I found that should make it so the only area affected by the dragging is repainted.
endPoint = e.getPoint();
xMin = Math.min(xMin, endPoint.x);
xMax = Math.max(xMax, endPoint.x);
yMin = Math.min(yMin, endPoint.y);
yMax = Math.max(yMax, endPoint.y);
repaint(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
}
public void mouseRelease(MouseEvent e)
{
//This code paints the shapes on the Buffered Image created as a canvas
int x = Math.min(startPoint.x, endPoint.x);
int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(startPoint.x - endPoint.x);
int height = Math.abs(startPoint.y - endPoint.y);
if (width != 0 || height != 0)
{
g2d.setColor( e.getComponent().getForeground() );
// g2d.drawRect(x, y, width, height);
switch (drawType)
{
case 1:
addRect(x, y, width, height, e.getComponent().getForeground());
break;
case 2:
addOval(x, y, width, height, e.getComponent().getForeground());
break;
case 3:
addLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, e.getComponent().getForeground());
break;
}//end switch statement.
}
startPoint = null;
// repaint();
}
}
}//end class
And here is the code for the UI:
/*#author Me*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDrawUI extends JFrame
{
private DrawShapes draw = new DrawShapes();
private JPanel ButtonPanel = new JPanel();
private JFrame window = new JFrame("Draw!");
//constructor
MyDrawUI(){
buildUI();
}
void buildUI()
{
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(2,2));
window.add(draw);
window.add(ButtonPanel, BorderLayout.SOUTH);
ButtonPanel.setBackground(Color.LIGHT_GRAY);
draw.setBackground(Color.WHITE);
//define buttons
JButton rectangle = new JButton("Rectangle");
JButton oval = new JButton("Oval");
JButton line = new JButton("Line");
JButton exit = new JButton("Exit");
JButton save = new JButton("Save");
JButton clear = new JButton("Clear");
//add buttons
ButtonPanel.add(rectangle, BorderLayout.SOUTH);
ButtonPanel.add(oval, BorderLayout.SOUTH);
ButtonPanel.add(line, BorderLayout.SOUTH);
ButtonPanel.add(clear, BorderLayout.SOUTH);
ButtonPanel.add(save, BorderLayout.SOUTH);
ButtonPanel.add(exit, BorderLayout.SOUTH);
ButtonPanel.setSize(100, 100);
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
draw.save();
}
});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
draw.clear();
}
});
rectangle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
draw.setType(1);
}
});
oval.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
draw.setType(2);
}
});
line.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
draw.setType(3);
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
window.setVisible(true);
window.setSize(1024, 800);
}
}
There are a few issues I can see, the main one is the fact that you "think" you've overridden a method in the MouseAdaptor class, but haven't
mouseRelease is not method that will cause any events to trigger it. The method you're after is mouseReleased.
When overriding methods, make use the #Override annotation, it will cause a compiler error if the method you "think" you're overriding doesn't exist in any of the parent classes.
#Override
public void mouseReleased(MouseEvent e) {
Several other things pop out.
You're MyDrawUI classes extends from JFrame, but you create a instance of another JFrame called window, onto which you create your UI. In this case, drop the extends JFrame from the MyDrawUI class, as it just adds confusion...
Maintaining a reference to a Graphics context, even one you created, is ill advised in this context. On some systems, until you call dispose it's possible that nothing will be committed to the underlying implementation. Instead, simply use image.getGraphics when you need it and call g2d.dispose when you're done with it.