Java Programming (12.2)----polymorphism, listener Beginner application: Draw a graphic in a window, you can select a circular square line

Source: Internet
Author: User
Tags event listener gety

1. Add 3 buttons to the 800 *800 window to select the current brush to draw what kind of shape has a circle square straight line three kinds

JFrame Listener uses internal class anonymous internal class interface polymorphism

1. Abstract class for all graphs first

Package Com.lovo.homework;import Java.awt.color;import java.awt.graphics;/** * Abstract class: Graphics * @author Abe * Properties: Start coordinates end coordinates color */PU  Blic abstract class Figure {protected int startX, starty;protected int endx, endy;protected Color color;/** * constructor */public Figure () {This.color = This.radomcolor ();} /** * Method: Random color */public color Radomcolor () {int r = (int) (Math.random () * n); int g = (int) (Math.random () * n); int b = (int) (Math.random () * 256); Color color = new Color (r, G, b); return color;} /** * Method: Get and set//Do not know to use the first addition to say * @return */public int getstartx () {return startX;} public void Setstartx (int startX) {this.startx = StartX;} public int Getstarty () {return starty;} public void Setstarty (int starty) {this.starty = Starty;} public int getendx () {return endx;} public void setendx (int endx) {this.endx = EndX;} public int Getendy () {return endY;} public void Setendy (int endY) {this.endy = EndY;} Public Color GetColor () {return Color;} /** * Abstract Method Drawing * @param g */public abstract void Draw (Graphics g);} 

2. Class: Round

Package Com.lovo.homework;import java.awt.graphics;/** * Class: Circle * Parent class: Graphics * @author Abe  */public class Circle extends Figu re{@Overridepublic void Draw (Graphics g) {G.setcolor (This.radomcolor ());//Rewrite drawing method  inherits the teacher's xy judgment int x = StartX < EndX ? Startx:endx;int y = starty < EndY? Starty:endy;g.drawoval (x, Y, Math.Abs (ENDX-STARTX), Math.Abs (Endy-starty));}}

3. Class: Square

Package com.lovo.homework;/** * Class: Square * @author Abe * Parent class: Figrue Graphics  */import java.awt.graphics;public class Ractangle Extends Figure {@Overridepublic void Draw (Graphics g) {//Rewrite the drawing method, the result of plotting with a method of swapping size feels a lot more trouble G.setcolor (This.radomcolor ()) ;//Optimized the coordinates of the next teacher to determine the method int x = Math.min (StartX, endx); int y = Math.min (starty, EndY); G.drawrect (x, Y, Math.Abs (endx-startx), Ma Th.abs (Endy-starty));}}

4. Class: Straight Line

Package Com.lovo.homework;import java.awt.graphics;/** * Class: Straight line * Parent class: Figure * @author Abe */public class lines extends figure {@Overridepublic void Draw (Graphics g) {G.setcolor (This.radomcolor ()); G.drawline (StartX, Starty,endx,endy);}}

5. Draw window, add event listener, add Mouse listener (adapter)

adapters, for inheritance, only with the functionality required for rewriting, other automatic soy sauce

Package Com.lovo.homework;import Java.awt.button;import Java.awt.graphics;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.mouseadapter;import java.awt.event.mouseevent;/** * class: Drawing window * Parent class: JFrame * @author Abe */import javax.swing.JFrame; @SuppressWarnings ("Serial")//Don't know what the goods mean MyFrame automatically fix public class MyFrame extends JFrame {private Button B1 = New button ("Circle");p rivate button b2 = New button ("Rectangle");p rivate button B3 = new  Button ("line segment");p rivate Figure fig = null;private Boolean Boo = true;/** * Inner class: Event listener * @author Abe */public class Mylistenner Implements ActionListener {//interface event listener @overridepublic void actionperformed (ActionEvent e) {Object obj = E.getsource (); if ( obj = = B1) {fig = new Circle (),} else if (obj = = b2) {fig = new Ractangle ();} else if (obj = = B3) {fig = new line ();}}} /** * Inner class: Mouse adapter * @param e */public class Mymouseadapter extends Mouseadapter {//inherit mouse adapter @overridepublic void mousepressed (MouseEvent e) {//Override Click event if (Fig! = null) {Fig. Setstartx (E.getx ()); Fig.setstarty (E.gety ()); Fig.setendx (E.getx ()); Fig.setendy (E.gety ());}} @Overridepublic void mousedragged (MouseEvent e) {//Override drag event if (fig! = null) {Fig.setendx (E.getx ()); Fig.setendy (E.gety ()) ; repaint ();}}} /** * Constructor */public MyFrame () {This.settitle ("selection tool painting"); This.setsize (+) this.setdefaultcloseoperation (exit_on_ CLOSE); this.setresizable (false); This.setlocationrelativeto (null); This.setlayout (null); B1.setbounds (220, 10, 75, (b2.setbounds); B3.setbounds (480, ten, ()), This.add (B1); This.add (B2); This.add (b3); Mymouseadapter adapter = new Mymouseadapter (); This.addmouselistener (adapter); This.addmousemotionlistener (adapter); Mylistenner L = new Mylistenner (); B1.addactionlistener (L); B2.addactionlistener (L); B3.addactionlistener (l);} /** * Rewrite drawing method */@Overridepublic void Paint (Graphics g) {if (boo! = True) {g.clearrect (0, 0, +); boo = true;} Super.paint (g);//enable the window without a shadow to see only the last drawn graphic if (fig! = null) {Fig.draw (g);}} public static void Main (string[] args){New MyFrame (). setvisible (True);}} 

The drawing effect looks unreal, but when you refresh the window, only the last graphic is displayed.

I wanted to save it with a number of groups, but once the array is instantiated, it doesn't add up ... Or would you like to save 3 graphs with 3 arrays?  Feel Good trouble ... There should be a better way ...




Java Programming (12.2)----polymorphism, listener Beginner application: Draw a graphic in a window, you can select a circular square line

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.