Java 2 programming fundamentals Chapter 14 exercises after class

  1. The characteristics of Graphics class are briefly introduced.
    Graphics class is a special abstract class in java.awt package. When drawing graphics, text and image on the screen, you don't need to use new operator directly to create an object instance of graphics class, but you can use it directly. The object generated by graphics class is the "drawing area" of the drawing environment. Unlike general classes, objects called "drawing areas" do not need to be created through the construction methods of graphics classes, but through the getGraphics() method. With this "drawing area", you can use various drawing methods provided by the graphics class to draw graphics,
  2. What are the methods provided in the Java language for displaying, updating, and redrawing graphics?
    Graphic display: public void paint(Graphics g);
    Update graphics: public void update(Graphics g);
    Redraw graph: public void retain(); public void retain (int x, int y, int width, int height);
  3. The paint() method is called automatically when something happens.
    (1) When a new window is displayed on the display or changed from hidden to displayed;
    (2) After the icon is reduced to normal display;
    (3) Changing window size.
  4. Write a drawing program, users can use radio button group or drop-down list box to select the drawing, such as lines, circles, ellipses, rectangles, etc. after selection, you can drag the mouse to draw the corresponding graphics on the screen.
import java.awt.*;
import java.awt.event.*;

public class exe14_4 extends Frame implements ItemListener, MouseMotionListener, MouseListener {
	
	static exe14_4 frm = new exe14_4();
	
	static Checkbox cb1 = new Checkbox("Line");
	static Checkbox cb2 = new Checkbox("circular");
	static Checkbox cb3 = new Checkbox("Ellipse");
	static Checkbox cb4 = new Checkbox("rectangle");
	static Choice cho = new Choice();
	
	static final int LINE = 0;
	static final int CIRCLE = 1;
	static final int OVAL = 2;
	static final int RECT = 3;
	
	int px1, py1, px2, py2;
	static int status = 0, sharp = 0;
	int rpx1, rpy1, rpx2, rpy2;
	
	public static void main(String[] args) {
		CheckboxGroup grp = new CheckboxGroup();
		cb1.setCheckboxGroup(grp);
		cb2.setCheckboxGroup(grp);
		cb3.setCheckboxGroup(grp);
		cb4.setCheckboxGroup(grp);
		cb1.addItemListener(frm);
		cb2.addItemListener(frm);
		cb3.addItemListener(frm);
		cb4.addItemListener(frm);
		
		cho.add("Line");
		cho.add("circular");
		cho.add("Ellipse");
		cho.add("rectangle");
		cho.addItemListener(new MyItemListener());
		
		frm.setTitle("exe14_4");
		frm.setLocation(200, 150);
		frm.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
		frm.setSize(400, 300);
		frm.add(cb1);
		frm.add(cb2);
		frm.add(cb3);
		frm.add(cb4);
		frm.add(cho);
		
		frm.addMouseMotionListener(frm);
		frm.addMouseListener(frm);
		
		frm.setVisible(true);
	}
	
	public void itemStateChanged(ItemEvent e) {
		Checkbox cb = (Checkbox) e.getSource();
		if (cb == cb1) {
			sharp = LINE;
		} else if (cb == cb2) {
			sharp = CIRCLE;
		} else if (cb == cb3) {
			sharp = OVAL;
		} else if (cb == cb4) {
			sharp = RECT;
		}
	}
	
	public void mouseMoved(MouseEvent e) {
		px1 = e.getX();
		py1 = e.getY();
		status = 0;
	}
	
	public void mouseDragged(MouseEvent e) {
		Graphics g = getGraphics();
		g.setColor(Color.yellow);
		g.setXORMode(Color.black);
		if (status == 1) {
			switch (sharp) {
				case LINE:
					g.drawLine(px1, py1, e.getX(), e.getY());
					break;
				case CIRCLE:
					g.drawOval(px1, py1, px2, px2);
					break;
				case OVAL:
					g.drawOval(px1, py1, px2, py2);
					break;
				case RECT:
					g.drawRect(px1, py1, px2, py2);
					break;
			}
		} else {
			px1 = e.getX();
			py1 = e.getY();
			status = 1;
		}
		px2 = Math.abs(e.getX() - px1);
		py2 = Math.abs(e.getY() - py1);
		switch (sharp) {
			case LINE:
				g.drawLine(px1, py1, e.getX(), e.getY());
				break;
			case CIRCLE:
				g.drawOval(px1, py1, px2, px2);
				break;
			case OVAL:
				g.drawOval(px1, py1, px2, py2);
				break;
			case RECT:
				g.drawRect(px1, py1, px2, py2);
				break;
		}
		rpx1 = px1;
		rpy1 = py1;
		rpx2 = px2;
		rpy2 = py2;
	}
	
	public void mouseReleased(MouseEvent e) {
		Graphics g = getGraphics();
		g.setColor(Color.red);
		switch (sharp) {
			case LINE:
				g.drawLine(rpx1, rpy1, e.getX(), e.getY());
				break;
			case CIRCLE:
				g.drawOval(rpx1, rpy1, rpx2, rpx2);
				break;
			case OVAL:
				g.drawOval(rpx1, rpy1, rpx2, rpy2);
				break;
			case RECT:
				g.drawRect(rpx1, rpy1, rpx2, rpy2);
				break;
		}
	}
	
	public void mousePressed(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	
	static class MyItemListener implements ItemListener {
		public void itemStateChanged(ItemEvent e) {
			String clr = cho.getSelectedItem();
			if (clr == "Line") {
				sharp = LINE;
			} else if (clr == "circular") {
				sharp = CIRCLE;
			} else if (clr == "Ellipse") {
				sharp = OVAL;
			} else if (clr == "rectangle") {
				sharp = RECT;
			}
		}
	}
}

Note: the exercises are all written by the blogger himself, not the standard answer. You can leave a message to discuss the possible problems.

47 original articles published, 7 praised, 20000 visitors+
Private letter follow

Tags: Java

Posted on Mon, 13 Jan 2020 02:15:05 -0800 by Snooble