Free Essay

Cse142

In:

Submitted By weili
Words 293
Pages 2
import java.awt.*; public class <ClassName> { public static void main (String[ ] args) {
DrawingPanel <name> = new DrawingPanel (<width>, <height>); //to create a graphical window <panelname>.setBackground (Color.BLUE);

Graphics g = <panel name>. getGraphics ( );
g.setColor (Color.BLUE);
g. drawLine (<x1>, <y1>, <x2>, <y2>); //to draw a line from point (x1,y1) to (x2,y2) g.drawRect (<x>, <y>, <width>, <height>); //to draw a rectangle with the upper-left point (x,y) g.fillRect (<x>, <y>, <width>, <height>); //to fill a rectangle with color g.drawOval (<x>, <y>, <width>, <height>); //to draw the largest circle or oval that can fit into the rectangle with given width, height and the upper-left point (x,y) g.fillOval (<x>, <y>, <width>, <height>); //to fill a circle/oval with color

Drawing panel keeps track of the overall image, but the actual drawing will be done with an object f type Graphics/ to draw shapes and lines, you don’t talk directly to the DrawingPanel, but rather to Graphisc所以说如果要“作画”的话,应该用g. 而不是 p./ 画画本来就是graphics的事,与panel无关
Rules of g.setColor: calling “setColor” is like dipping your paintbrush into a different color of paint. From that point on, all drawing and filling will be done in that specified color until set it to another color next time./ 先set颜色,再用该颜色去draw things. Order matters

如何用loop来drawdiagram: 看draw贴合格
颜色的名字全部要用大写
注意蓝色字体的大小

Parameter in DrawingPanel
<methodName> (g,…); ------method call public static void <methodName> (Graphics g,…) {… g 和 Graphics g 不可少,其他的parameter再加在它们后面

Similar Documents