Java BoxProject using two classes -
im trying code print out new boxes based on original box class , im stuck. not sure variables need specify in grid class. nor know put in following class.
public void actionperformed(actionevent evt) { // maybe stuff here repaint(); }
here both of classes.
---the code has been updated since first reply---
box class
import java.awt.*; public class box{ int upperleftx = 0; int upperlefty = 0; int height = 20; int width = 20; color color = color.red; //constructor public box(int i, int j, int k, int l, color m) { upperleftx = i; upperlefty = j; height = k; width = l; color = m; } // paints box on screen public void display(graphics g) { g.setcolor(color); g.fillrect(upperleftx,upperlefty,width, height); } // getters , setters public int getupperleftx() { return upperleftx; } public void setupperleftx(int upperleftx) { this.upperleftx = upperleftx; } public int getupperlefty() { return upperlefty; } public void setupperlefty(int upperlefty) { this.upperlefty = upperlefty; } public int getheight() { return height; } public void setheight(int height) { this.height = height; } public int getwidth() { return width; } public void setwidth(int width) { this.width = width; } public color getboxcolor() { return color; } public void setboxcolor(color color) { this.color = color; } }
grid class
import java.applet.applet; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class grid extends applet implements actionlistener{ // declare variables here public void actionperformed(actionevent evt) { // maybe stuff here repaint(); } public void paint(graphics g) { box box1 = new box(0,0,40,40,color.white); box1.display(g); // more stuff here box box2 = new box(40,40,40,40,color.white); box2.display(g); } }
first of all, box
should not extend applet
. in fact, should not extend @ all. should serve storage container data boxes. not affect anything, confusing , doesn't make sense.
second, dan455 has pointed out, assignments in box
constructor in wrong order. cause no harm, except parameters passed constructor mysteriously ignored, , seem every box
constructed new box(0, 0, 20, 20)
.
third, actionlistener
, don't need add unless have responds actionevent
. if arrangement of boxes needs change, should have array of boxes drawn in paint
, , if display of boxes needs updated, can modify array , call repaint
, call paint
possible.
Comments
Post a Comment