Use of drawing tools in the drawing board

Source: Internet
Author: User

Redrawing refers to the drawing of the picture to save, redraw divided into three kinds: remember coordinates, points, notes steps.

We are here to focus on the method of the coordinates of the workshop;

If we want to draw a straight line, the main thing is to record its two coordinates, so we should create an array to record the two coordinates.

The code is as follows:

public class Shape {

int x1,y1,x2,y2;

String name;

Color color;

public Shape (int x1, int y1, int x2, int y2) {

this.x1 = x1;

This.y1 = y1;

this.x2 = x2;

This.y2 = y2;

}

}

There is a need to re-build a class to hold the coordinates. This step should be the first step, you should first create a class

public class Drawframe extends JPanel {

Shape[] shape = new SHAPE[20];

This creates a shape array, and extends to rewrite the method in the JPanel;

Layout on the left side of the design

JPanel JP2 = new JPanel ();

This.setpreferredsize (New Dimension (850, 700));

This.setbackground (Color.White);

Jf.add (this);

/***********/

This.addmouselistener (LG);

This.addmousemotionlistener (LG);

Here this refers to our panel, we have to rewrite the method of the drawing of the JPanel to use this method.

public void Paint (Graphics g) {

Super.paint (g);

for (int i = 0; i < shape.length; i++) {

if (shape[i]! = null) {

if ("line". Equals (Shape[i].name)) {

G.setcolor (Shape[i].color);

G.drawline (shape[i].x1, Shape[i].y1, SHAPE[I].X2,

SHAPE[I].Y2);

}

if ("Round". Equals (Shape[i].name)) {

G.setcolor (Shape[i].color);

G.drawoval (Math.min (shape[i].x1, shape[i].x2), Math.min (Shape[i].y1, Shape[i].y2), Math.Abs (Shape[i].x1-shape[i]. x2),

Math.Abs (Shape[i].y1-shape[i].y2));

}

if ("Square". Equals (Shape[i].name)) {

G.setcolor (Shape[i].color);

G.drawrect (Math.min (SHAPE[I].X1,SHAPE[I].X2), Math.min (Shape[i].y1, Shape[i].y2), Math.Abs (Shape[i].x1-shape[i]. x2),

Math.Abs (Shape[i].y1-shape[i].y2));

}

}

}

}

Here is the most important step to rewrite, is to override the method in JPanel, the Paint method is automatically called in the JPanel. So do not intentionally add.

Private shape[] Shape;

private int index;

public void Setshape (shape[] Shape) {

This.shape = shape;

}

Here's how to add a method to the listener class, which is the class that passes shape to the listening mechanism.

Shape line = new shape (x1, y1, X4, y4);

Line.name = "Straight line";

Line.color = CO;

Shape[index] = line;

index++;

The next is to add that array to each method to hold the coordinates.

This rewrite is complete, thank you!

Use of paint tools in the drawing board

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.