hoofdstuk 12.5 toepassing: bitmap-editor. nklik punten op scherm nplaatje verschuiven left, right,...
Post on 20-Dec-2015
213 views
TRANSCRIPT
Toepassing: Bitmap-editor
Klik punten op scherm
Plaatje verschuivenleft, right, up, down
Plaatje bewerkenclear, invert, bold, outline
Plaatje inlezen “Game of life”
Opdeling in klassen
BitEditgebruikersinterface, menu’s, akties, files
BitCanvtekenen van het plaatje op het scherm
BitMapplaatje in geheugen, operaties
vergelijk:Editor - TextArea - StringBitEdit - BitCanv - BitMap
Objectvariabelen
class BitEdit BitCanv scherm; Thread ani;
class BitCanv BitMap model;
class BitMap boolean [ ] [ ] waarde; int breed, hoog;
extends Frame
extends Canvas
implements ActionListener, WindowListenerimplements MouseListener, MouseMotionListener
zelfgemaakt
extends BufferedImage
Methoden BitEdit
class BitEdit extends Frame BitEdit
initMenumaakMenu
actionPerformed windowClosing run
Methoden BitCanv
class BitCanv extends Canvas BitCanv
setModel
paintupdatediameter
mousePressed doeOperatie
Methoden BitMap
class BitMap constructor BitMap, BitMap punten zetten maakZwart, isZwart schuiven left, right, up, down bewerken clear, invert, bold, outline
or, xor, androtate
game of life life, buren
Methoden BitMap (versie 2)
class BitMap extends BufferedImage constructor BitMap, BitMap punten zetten maakZwart, isZwart schuiven left, right, up, down bewerken clear, invert, bold, outline
or, xor, androtate
game of life life, buren
class BitEdit
public BitEdit( ){
}
this . setSize (800,600);this . setTitle (“Bitmap Editor”);this . addWindowListener ( this );
this . initMenu( );
int w, int h );
uitvoer = new BitCanv(w, h);this . setLayout( new BorderLayout( ) );this . add( uitvoer, BorderLayout.CENTER );
class BitEdit
private void initMenu( ){ String [ ] [ ] items =
{ { “File”, “Open”, “Save”, “Quit” }, { “Move”, “Left”, “Right”, “Up”, “Down” }, { “Edit”, “Clear”, “Invert”, “Bold”, “Outline” }, { “Life”, “Step”, “Start”, “Stop” }};
MenuBar bar;bar = new MenuBar ( );
this . setMenuBar (bar);
bar . add ( );maakMenu (items[t] ), thisfor (int t=0; t<items.length; t++)
class BitEdit
private maakMenu( ){
}
MenuString [] keus, ActionListener al
Menu menu;menu = new Menu (keus[0]);
return menu;
for (int t=1; t<keus.length; t++){
}
MenuItem item;item = new MenuItem (keus[t]);menu . add (item);item . addActionListener (al);
{ “File”, “Open”, “Save”, “Quit” }
class BitEdit
public void actionPerformed (ActionEvent e){
}
e.getSource()(MenuItem)( ). getLabel()if (s.equals (“Quit”)
)else if (s.equals (“Stop”)
)else if (s.equals (“Start”)
)
else
String s;s = ;
System . exit(0);{ ani = new Thread(this); ani . start ( ); }
ani = null;
uitvoer . doeOperatie( s );
class BitEdit
public void run ( ){
}
while (ani != null){
}
model . doeOperatie ( “Step” ) ;
Thread . sleep(100);
try { }catch (Exception e) { }
class BitEdit
public static void main ( String [ ] ps ){
}
BitEdit b;b = new BitEdit ( );b . setVisible (true);
w, h );
int w=20, h=20;if (ps.length>0) w=h= ps[0] ;
Integer.parseInt( )if (ps.length>1) h=
Integer.parseInt( ps[1] );
Methoden BitMap
class BitMap extends BufferedImage constructor BitMap (2 versies) punten zetten maakZwart, isZwart schuiven left, right, up, down bewerken clear, invert, bold, outline
or, xor, androtate
game of life life, buren
class BitMap
public BitMap ( int w, int h ){
}
breed=w; hoog=h;waarde = new boolean [breed] [hoog];
public BitMap ( BitMap ander){ ....}public void maakZwart ( int x, int y, boolean b ){
}public boolean isZwart ( int x, int y ){}
waarde[x][y] = b;
return waarde[x][y] ;
class BitMap
public void clear ( ){
}
public void invert ( ){
}
this . maakZwart (x, y, false );
for (int y=0; y<hoog; y++) for (int x=0; x<breed; x++)
this . maakZwart (x, y, ! this.isZwart(x,y) );
for (int y=0; y<hoog; y++) for (int x=0; x<breed; x++)
class BitMap
public void left ( ){
}
this.maakZwart( x-1, y, this.isZwart(x,y) );
for (int y=0; y<hoog; y++) for (int x=0 ; x<breed; x++)1{
this.maakZwart( breed-1, y, false );}
class BitMap
public void right ( ){
}
this.maakZwart( x, y, this.isZwart(x-1 ,y) );
for (int y=0; y<hoog; y++) for (int x=0 ; x<breed; x++)breed-1; x>0; x-- ){
this.maakZwart( 0, y, false );}
class BitMappublic void bold ( ){ BitMap
ander;ander = new BitMap(this);ander . left ( );
this . or (ander);ander = new BitMap(this);ander . down ( );
this . or (ander);
}
class BitMap
public void or ( BitMap ander ){
}
this . maakZwart (x, y,
for (int y=0; y<hoog; y++) for (int x=0; x<breed; x++)
this . isZwart (x, y) || ander.isZwart (x, y) );
public void and ( BitMap ander ){
}
this . maakZwart (x, y,
for (int y=0; y<hoog; y++) for (int x=0; x<breed; x++)
this . isZwart (x, y) && ander.isZwart (x, y) );
public void xor ( BitMap ander ) { ... ^ ... }
}
class BitMap
public void outline ( ){
BitMap ander;ander = new BitMap(this);ander . left ( );ander . down ( );this . xor (ander);
}
class BitMap
public void life ( ){
BitMap oud;oud = new BitMap(this);
this . maakZwart (x, y,
}
int n = oud . buren(x,y);
n==3 ||n==2 && oud.isZwart(x,y) );
for (int y=0; y<hoog; y++) for (int x=0; x<breed; x++) {
}
class BitMap
public int buren (int x, int y ){ int xl = x-1;
int xr = x+1;int yb = y-1;int yo = y+1;
return n;
int n = 0;
if ( this.isZwart(xl,yb) ) n++;if ( this.isZwart(x ,yb) ) n++;if ( this.isZwart(xr,yb) ) n++;if ( this.isZwart(xl,y ) ) n++;if ( this.isZwart(xr,y ) ) n++;if ( this.isZwart(xl,yo) ) n++;if ( this.isZwart(x ,yo) ) n++;if ( this.isZwart(xr,yo) ) n++;
if (xl<0) xl += breed;if (xr>=breed) xr -= breed;if (yb<0) yb += hoog;if (yo>=hoog) yo -= hoog;
xl xr
yb
yo
Methoden BitCanv
class BitCanv extends Canvas BitCanv
setModel paint
updatediameter
mousePressedmouseDragged
doeOperatie
objectvariabele:
BitMap model;
class BitCanv
public void paint ( Graphics gr ){
gr . fillRect (x, y, 1, 1);
}
if (model.isZwart(x,y)) gr.setColor(Color.red);elsegr.setColor(Color.white);
for (int y=0; y<hoog; y++) for (int x=0; x<breed; x++) {
}
int d = this . diameter( );
x*d, y*d, d, d );x*d+1, y*d+1, d-1, d-1 );
this . lijnen (gr, d);
class BitCanv
private int diameter ( ){
Dimension ruimte;ruimte = this . getSize();
ruimte.width / model.breed
return
ruimte.height / model.hoog
Math . min(,);
}
class BitCanvprivate void lijnen ( Graphics gr, int d){
// horizontale lijnen
}
// verticale lijnen
gr . setColor (Color .blue);
gr . drawLine ( 0, y*d, model.breed*d, y*d );
for (int y=0; y <= hoog; y++)
gr . drawLine ( x*d, 0, x*d, model.hoog*d );
for (int x=0; x <= breed; x++)
class BitCanv
public void mousePressed ( MouseEvent ev ){
}
ev . getX ( )ev . getY ( )
/ diam/ diam
int diam = this . diameter ( );
model . maakZwart ( , , );
ev . isLinkerKnop ( )
this . repaint ( );
public void mouseDragged ( MouseEvent ev ){ this . mousePressed (ev);}
class BitCanv
public class BitCanv
{ BitMap model;
public BitCanv ( int w, int h){
}
model = new BitMap(w, h );this . addMouseListener (this);this . addMouseMotionListener (this);
implements MouseListener, MouseMotionListener
public void setModel (Model m){}
model = m;
class BitMap
public BitMap ( int w, int h ){
}
breed=w; hoog=h;waarde = new boolean [breed] [hoog];
public void maakZwart ( int x, int y, boolean b ){
}public boolean isZwart ( int x, int y ){}
waarde[x][y] = b;
return waarde[x][y] ;
class BitMap extends BufferedImage
public BitMap ( int w, int h ){
}
public void maakZwart ( int x, int y, boolean b ){
}public boolean isZwart ( int x, int y ){}
super ( w, h, BufferedImage.TYPE_INT_RGB );this . clear( ) ;
else this . setRGB( x, y, WIT );
if ( b ) this . setRGB( x, y, ZWART );
return this . getRGB(x,y) == ZWART ;
static final int WIT = 0xFFFFFFFF;static final int ZWART = 0xFF000000;
repaint en paintpublic class Component{ public void repaint ( )
{ Graphics gr = ????; this . paint (gr);}public void paint (Graphics g){}
public class MijnKlasse extends Canvas{
public void paint (Graphics g){ gr . draw....(....);}
public void update (Graphics g){ gr . setColor(Color.WHITE); gr . fillRect (0,0,veel,veel); gr . setColor(Color.BLACK); this . paint (gr);}
public void update (Graphics g){ this . paint (gr);}
update (gr);update
anti-knipperen