Java -практика использования



             

Листинг 12 4 Простейшая программа рисования



Листинг 12.4. Простейшая программа рисования 

import j ava.awt.*; 

import j ava.awt.event.*;

public class ScribbleTest extends Frame{ 

public ScribbleTest(String s){ 

super(s);

ScrollPane pane = new ScrollPane();

pane.setSize(300, 300);

add(pane, BorderLayout.CENTER);

Scribble scr = new Scribble(this, 500, 500); 

pane.add(scr);

Panel p = new Panel 0; 

add(p, BorderLayout.SOUTH);

Button bl = new Button("Красный"); 

p.add(bl);

bl.addActionListener(scr);

Button b2 = new Button("Зеленый"); 

p.add(b2);

b2.addActionListener(scr) ;

Button b3 = new Button("Синий"); 

p.add(b3);

b3.addActionListener(scr) ;

Button b4 = new Button("Черный"); 

p.add(b4);

b4.addActionListener(scr);

Button b5 = new Button("Очистить"); 

p.add(bS);

b5.addActionListener(scr);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){ 

System.exit(0);

});

pack();

setvisible(true); 

}

public static void main(String[] args){ 

new ScribbleTest(" \"Рисовалка\"");

}

}

class Scribble extends Component implements ActionListener, MouseListener, MouseMotionListener{ 

protected int lastX, lastY, w, h; 

protected Color currColor = Color.black; 

protected Frame f;

public Scribble(Frame frame, int width, int height){

f = frame; 

w = width; 

h = height;

enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);

addMouseListener(this);

addMouseMotionListener(this); } 

public Dimension getPreferredSize(){

return new Dimension(w, h); } 

public void actionPerformed(ActionEvent event){

String s = event.getActionCommand();

if (s.equals ("Очистить")) repaint();

else if (s.equals ("Красный")) currColor = CofLor.red;

else if (s.equals("Зеленый")) currColor = Coior.green;

else if (s.equals("Синий")) currColor = Color.blue;

else if (s.equals("Черный")) currColor = Color.black; } 

public void mousePressed(MouseEvent e){

if ( (e.getModifiers() & MouseEvent.BUTTON 1__MASK) = 0) return;

lastX = e.getXO; lastY = e.getYO; } 

public void mouseDragged(MouseEvent e){

if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) = 0) return;

Graphics g = getGraphics();

g.setColor(currColor);

g.drawLinedastX, lastY, e.getX(), e.getY());

lastX = e.getX(); lastY = e.getY(); }

public void mouseReleased(MouseEvent e){} 

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){} 

public void mouseExited(MouseEvent e){} 

public void mouseMoved(MouseEvent e){} 

}









Содержание  Назад  Вперед