/* The code used in the article is only a part of the source can be contacted by email me [email protected]*/
During this time learning the Java Swing interface, I tried to make a drawing board. realizes the line, the curve, the gun, the rectangle, the circle, the text, the eraser and so on operation, the feeling harvest is very big.
Since the drawing board is to be done, the best reference is of course the Windows system comes with the drawing! Although the technology is limited can not do the same, but the feeling can still look (manual funny). Let's talk about how it's going to work.
First of all do not think, it must be the interface done first (this is my interface, emmmmmm .... function and interface are still to be perfected)
Take a closer look and you'll probably think about how to do this, first create a Drawmain class inheritance (extends) JFrame class
public class Drawmain extends JFrame {public static void main (string[] args) {drawmain dm = new Drawmain ();d m.setlookandfe El ();d m.initui ();} /** * Set the skin for the main panel, this is my casual looking for one, specifically to study */public void Setlookandfeel () {try {Uimanager.setlookandfeel (" Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel ");} catch (Exception e) {e.printstacktrace ();}} public void Initui () {this.settitle ("Paint program"); This.setsize (+); this.setdefaultcloseoperation (3); This.setlocationrelativeto (null); This.setlayout (new BorderLayout ()); This.setvisible (TRUE);}
This is, of course, the main interface, so how do you get behind it? The above can have so many partitions of course need to put several container types of components. Is the component in the component, then the layout of the choice is very important, first use of the main interface is Broderlayout, in the north to put up a jpanel up slightly
JPanel Northjpanel = new JPanel (); Northjpanel.setlayout (New FlowLayout (Flowlayout.left, 1, 0)); Northjpanel.setbackground (new color (240, 240, 240));//Set Background color
Northjpanel.setborder (Borderfactory.createetchedborder (new color (0, 0, 0), new color (0, 255, 0)), set border, Can see what the difference is This.add (Northjpanel, Borderlayout.north);
Run it, pull the border again, what do you see? The size of the component you just posted will change as you pull the border, so we should put another jpanel into this jpanel, and then set the size to prevent changes.
JPanel Innorthjpanel = new JPanel () innorthjpanel.setlayout (New FlowLayout (Flowlayout.left, 1, 0)); Nnorthjpanel.setpreferredsize (new Dimension), Innorthjpanel.setbackground (new Color (240, 240, 240)); Northjpanel.add (Innorthjpanel);
And then what to do? Set partition, naturally, each partition is a JPanel component
/* * Shape area * * @param shapejpanel Shape area Panel, interface layout * * @param inshapejpanel shape area to place Shape options panel, placed in Shapejpanel, flow layout * * @param in Shapelabel the label of the area identified in the shape area, placed in Shapejpanel */jpanel shapejpanel = null; Shapejpanel = Createjpanel (Innorthjpanel); Shapejpanel.setpreferredsize (New Dimension (300, 150)); JPanel Inshapejpanel = new JPanel () inshapejpanel.setlayout (New FlowLayout (Flowlayout.left, 5, 5)); Nshapejpanel.setbackground (null);//Set background color transparent Inshapejpanel.setopaque (false); Inshapejpanel.setpreferredsize (new Dimension (300, 110)); Shapejpanel.add (Inshapejpanel, Borderlayout.north); JLabel Inshapelabel = Null;inshapelabel = Createjlabel ("shape", shapejpanel);/* * Color Area * * @param colorjpanel Color area Panel, interface layout * * @param the panel where color options are placed in the Incolorjpanel color area, placed in Colorjpanel, flow layout * * @param label of the area identified in the Incolorlabel color area, placed in Colorjpanel */jpanel C Olorjpanel = null; Colorjpanel = Createjpanel (Innorthjpanel); JPanel Incolorjpanel = new JPanel () incolorjpanel.setpreferredsize (new Dimension); Ncolorjpanel.setbackground (null);//SetBackground color transparent Incolorjpanel.setopaque (false); Incolorjpanel.setlayout (New FlowLayout (Flowlayout.left, 5, 5)); Colorjpanel.add (Incolorjpanel, Borderlayout.north); JLabel Incolorlabel = Null;incolorlabel = Createjlabel ("Color", colorjpanel);/* * Weight Setting area * * @param strokejpanel Weight set area panel, interface Layout * * @param instrokejpanel weight in the Settings area of the panel, placed in the Strokejpanel, flow layout * * @param instrokelabel Weight Set the label of the area, placed in Strokejpanel */ JPanel strokejpanel = null; Strokejpanel = Createjpanel (Innorthjpanel); Strokejpanel.setpreferredsize (New Dimension (50, 150)); JPanel Instrokejpanel = new JPanel () instrokejpanel.setpreferredsize (new Dimension); Nstrokejpanel.setbackground (null); Instrokejpanel.setopaque (false); Instrokejpanel.setlayout (New FlowLayout ( Flowlayout.left, 5, 5)); Strokejpanel.add (Instrokejpanel, Borderlayout.north); JLabel Instrokelabel = Null;instrokelabel = Createjlabel ("thickness", strokejpanel);
You may find that I use Createjlabel () and Createjpanel () in it, which is the way I write it, because many of the code in the creation process is repetitive, and writing two of them in the code looks much more comfortable, and it can write a lot less code. Two concrete implementations of the methods
Private JPanel Createjpanel (JPanel innorthjpanel) {JPanel JP = new JPanel (); Jp.setborder ( Borderfactory.createetchedborder (new color (0, 0, 0), new color (0, 255, 0)); Jp.setpreferredsize (new Dimension (200, 150) Jp.setbackground (new Color), Innorthjpanel.add (JP), return JP; Private JLabel Createjlabel (String S, JPanel JP) {JLabel JL = new JLabel (s); jl.sethorizontalalignment (jlabel.center);//Set Jl.setfont (New Font ("Italic", Font.Bold, 20)) in the format of the play;//Set font style size Jp.add (JL, Borderlayout.south); return JL;}
So the top border is done, and then the button and the text box are pasted.
/* * Place button */string[] Typearray = {"line", "Oval", "Rect", "RoundRect", "Fill3drect", "Fillarc", "Image", "Text" , "Pencil", "Iso_tri", "Polygon", "Airbrush", "Erase"}; Color[] Colorarray = {color.red, color.black, Color.green, Color.Blue, New Color (255, 255, 255)}; String[] Widtharray = {"1", "3", "5"}; JTextField Text = new JTextField (); Text.setpreferredsize (new Dimension (+));D rawlistener dl = new Drawlistener (this , text, list), for (int i = 0; i < typearray.length; i++) {JButton button = new JButton (Typearray[i]); Inshapejpanel.add ( button), Button.addactionlistener (DL), if (i>=12) {JButton button1 = new JButton (Typearray[i]); Innorthjpanel.add ( button); Button1.addactionlistener (DL);}} for (int i = 0; i < colorarray.length; i++) {JButton button = new JButton (); Button.setbackground (Colorarray[i]); button. Setpreferredsize (New Dimension (), Incolorjpanel.add (button); Button.addactionlistener (DL);} for (int i = 0; i < widtharray.length; i++) {JButton button = new JButton (WIDtharray[i]); Instrokejpanel.add (button); Button.addactionlistener (DL);} Innorthjpanel.add (text);
In this way, our interface is ready.
JAVA Drawing Board implementation (Basic drawing function + UI UI), interface implementation