Drag the curve of Java drawing

Source: Internet
Author: User
Tags gety

Objective: To draw a curve between pressing and dragging by pressing the mouse button on the form and then dragging the mouse
Event mechanism
Event Source object: Form
Event Monitoring method: Addmouselistener (MouseListener L); Addmousemotionlistener (Mousemotionlistener L);
Event interface (event handler Class): Mouselistener,mousemotionlistener

Implementation steps:
1. Define the Drawlistener event handler class that implements the MouseListener and Mousemotionlistener mouse event interfaces to implement abstract methods in the interface
2. Instantiate the object of the Drawlistener event processing class, and then add the mouse action listening method and the mouse Move action listener method to the event source object this form object.
3. Get the coordinate value pressed by pressing the release method, get the coordinate value of the drag in the drag method, and draw the curve based on the coordinate values of the pressed and dragged

Practice
1. Realization of curves, brushes, erasers, spray guns

The implementation code is as follows:

/* 1. Create a new Drawlistener event handler class that implements the MouseListener mouse event interface to implement an abstract method in the interface.
2. Define four variables to get the pressed and released coordinate values in the press and release methods.
3. Define an object that grpahics the Brush canvas class and call the method that draws the drawing to paint.
Our components are drawn, and then you draw a graphic on which component, and your brush canvas object is taken from that component.
4. Instantiate the object of the Drawlistener event processing class, object name DL
5. Add the Addmouselistener () mouse listener method to the event source form object to specify the event-handling class object DL.
*/
Package wenya63;

Import Java.awt.Color;
Import Java.awt.FlowLayout;
Import Java.awt.Graphics;

Import Javax.swing.JButton;
Import Javax.swing.JFrame;

/**
* Defines a drawing interface class that inherits from the JFrame form class
*/
public class Drawframe extends JFrame {
/**
* Program Entry main function
*/
public static void Main (string[] args) {
Instantiating an object of a form class, invoking a method that initializes the interface
Drawframe df = new Drawframe ();
Df.initui ();
}
/**
* Methods for customizing the initialization interface
*/
public void Initui () {
Settitle ("drawing");
SetSize (600, 500);
Setdefaultcloseoperation (3);
Setlocationrelativeto (NULL);
SetLayout (New FlowLayout ());
Getcontentpane (). SetBackground (Color.cyan);//command to change background color
JButton butline = new JButton ("curve");
Add (Butline);
Butline.setbackground (Color.orange);
JButton butsharp = new JButton ("brush");
Add (BUTSHARP);
Butsharp.setbackground (Color.orange);
JButton butangle = new JButton ("Eraser");
Add (Butangle);
Butangle.setbackground (Color.orange);
JButton Buthair = new JButton ("airbrush");
Add (Buthair);
Buthair.setbackground (Color.orange);
SetVisible (TRUE);
Gets the brush canvas object on the form (note: You must not get the Brush canvas object until the form is visible, otherwise you get null)
Graphics g = getgraphics ();
4. Instantiate the object of the Drawlistener event processing class, object name DL
Drawlistener dl = new Drawlistener ();
5. Add the Addmouselistener () mouse listener method to the event source form object to specify the event-handling class object DL.
Addmouselistener (DL);
Addmousemotionlistener (DL);
Call Drawing Graphics
Dl.setgraphics (g);
button action Monitoring, the button is the event source, that is, only when the button is pressed to perform the action of drawing, you can refer to the Login interface authentication Login
Butline.addactionlistener (DL);
Butsharp.addactionlistener (DL);
Butangle.addactionlistener (DL);
Buthair.addactionlistener (DL);

}
}

Package wenya63;

Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import java.awt.RenderingHints;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.event.MouseMotionListener;
Import Java.util.Random;

/**
* 1. Create a new Drawlistener and ActionListener event processing class,
* This class implements MouseListener mouse event interface, Mousemotionlistener and ActionListener interfaces to implement an abstract method in the interface.
*/
public class Drawlistener implements MouseListener, Mousemotionlistener,
ActionListener {
private int x1, y1, x2, y2;//declare four integer variables to record the coordinate values at press and release
Private Graphics g;//declares an object name for a brush canvas class
Private String type;

Borrow a brush canvas class object to Drawframe
public void Setgraphics (Graphics gra) {
g = (graphics2d) gra;//the GRA to the G in Drawframe
((GRAPHICS2D) g). Setrenderinghint (Renderinghints.key_antialiasing,
RENDERINGHINTS.VALUE_ANTIALIAS_ON);

}

public void mousedragged (MouseEvent e) {
x2 = E.getx ();
y2 = E.gety ();

if (Type.equals ("curve")) {
G.setcolor (Color.green);
Set line thickness
((GRAPHICS2D) g). Setstroke (New Basicstroke (5));
G.drawline (x1, y1, x2, y2);//if written (X1,x2,y1,y2), the curve of the drawing can only be a fixed-direction curve
G.setcolor (Color.Black);//Restore the original color
}
x1 = x2;
y1 = y2;
if (type.equals ("brush")) {
G.setcolor (Color.pink);
((GRAPHICS2D) g). Setstroke (New Basicstroke (10));
G.drawline (x1, y1, x2, y2);
G.setcolor (Color.Black);//Restore the original color

}
if (Type.equals ("eraser")) {
Note To set the color before painting
G.setcolor (Color.White);
((GRAPHICS2D) g). Setstroke (New Basicstroke (30));
G.drawline (x1, y1, x2, y2);
G.setcolor (Color.Black);//Restore the original color
}
if (Type.equals ("Gun"))
{
Set line thickness
((GRAPHICS2D) g). Setstroke (New Basicstroke (1));
Random rand = new Random ();//Instantiate an object of a random number class
int size = Rand.nextint (50);//randomly determines the number of points to draw
for (int i=0;i<size;i++)
{
Can be taken 50 times between 0-7
int x = Rand.nextint (8);
int y = rand.nextint (8);

G.drawline (X1+x, Y1+y, X1+x, y1+y);//changing the coordinate value of (x1,y1) to achieve the point of drawing around (x1,y1)

}
}

}
/**
* Invoked when the mouse cursor have been moved onto a component but no
* Buttons has been pushed.
*/
public void mousemoved (MouseEvent e) {

}

/**
* The method to be executed when you have a mouse click action on the event source. (Press and release in the same location to perform a click)
*/
public void mouseclicked (MouseEvent e) {

}

/**
* The method that is executed when you press the mouse action on the event source.
*/
public void mousepressed (MouseEvent e) {
X1 = E.getx ();
Y1 = E.gety ();
}

/**
* The method to be executed when you have a mouse release action on the event source.
*/
public void mousereleased (MouseEvent e) {

}

/**
* The method that executes when your mouse enters the event source is a row.
*/
public void mouseentered (MouseEvent e) {

}

/**
* The method to execute when your mouse leaves the event source is the row.
*/
public void mouseexited (MouseEvent e) {
}

An abstract way to implement ActionListener
public void actionperformed (ActionEvent e) {
Type = E.getactioncommand ();//Actioncommand Gets the text on the button or gets the event source object
}

}

Drag the curve of Java drawing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.