Create a scrollable canvasTag(s): AWT
JDK1.1 using a ScrollPane
import java.applet.*; import java.awt.*; public class apptest extends Applet { Canvas c; ScrollPane s; public void init() { setLayout(new BorderLayout()); s = new ScrollPane(); s.setSize(100,100); add("Center", s); c = new myCanvas(); c.setSize(500,300); s.add(c); } class myCanvas extends Canvas { Image buffImage; Graphics offscreen; boolean initDone = false; myCanvas() { super(); } public void paint(Graphics g) { if (!initDone) initpaint(g); else g.drawImage(buffImage, 0, 0, this); } public void update(Graphics g) { g.drawImage(buffImage, 0, 0, this); } public void initpaint(Graphics g) { try { buffImage = this.createImage(500, 500); offscreen = buffImage.getGraphics(); offscreen.setColor(Color.black); offscreen.fillRect(0, 0, 500, 500); offscreen.setColor(Color.white); offscreen.setFont(new Font("Courier", Font.ITALIC, 42)); offscreen.drawString("Hello World!", 0, 50); initDone = true; g.drawImage(buffImage,0,0, this); } catch (Exception e) { e.printStackTrace(); } } } }
import java.applet.*; import java.awt.*; public class apptest extends Applet { ScrollCanvas sc; public void init() { setLayout(new FlowLayout()); sc = new ScrollCanvas (150,150, 300,200, Color.black, Color.white); add(sc); } public boolean handleEvent(Event e) { if (e.target instanceof Scrollbar) { switch (e.id) { case Event.SCROLL_ABSOLUTE: case Event.SCROLL_PAGE_DOWN: case Event.SCROLL_PAGE_UP: case Event.SCROLL_LINE_UP: case Event.SCROLL_LINE_DOWN: sc.redraw(); return true; } } return super.handleEvent(e); } } class ScrollCanvas extends Panel { int vw,vh; int rw,rh; Color b,f; myCanvas c; Scrollbar sv, sh; // constructor // visible h w // real h w // background foreground ScrollCanvas (int vw1, int vh1, int rw1, int rh1, Color b1, Color f1) { super(); vw = vw1; vh = vh1; rh = rh1; rw = rw1; b = b1; f = f1; int ScrollIncrement = 10; setLayout(new BorderLayout()); c = new myCanvas(vw, vh, rw, rh, b ,f); add("West", c); sv = new Scrollbar (Scrollbar.VERTICAL,0, ScrollIncrement, 0, rh); add("East", sv); sh = new Scrollbar (Scrollbar.HORIZONTAL, 0, ScrollIncrement, 0, rw); add("South", sh); } public void redraw() { int y = sv.getValue(); int x = sh.getValue(); c.draw(x,y); } public Dimension minimumSize() { return new Dimension(vw,vh); } public Dimension preferredSize() { return new Dimension(vw,vh); } } class myCanvas extends Canvas { int vw, vh; int rw, rh; Color b, f; int x, y; Image buffImage; Graphics offscreen; boolean initDone; myCanvas (int vw1, int vh1, int rw1, int rh1, Color b1, Color f1) { super(); vw = vw1; vh = vh1; rh = rh1; rw = rw1; b = b1; f = f1; initDone = false; repaint(); } public void paint(Graphics g) { if (!initDone) initpaint(g); else g.drawImage(buffImage, x, y, this); } public void update(Graphics g) { g.drawImage(buffImage, x, y, this); } public void initpaint(Graphics g) { try { buffImage = this.createImage(rw, rh); offscreen = buffImage.getGraphics(); offscreen.setColor(b); offscreen.fillRect(0, 0, rw, rh); offscreen.setColor(f); offscreen.setFont(new Font("Courier", Font.ITALIC, 42)); offscreen.drawString("Hello World!", 0, 50); initDone = true; g.drawImage(buffImage,0,0, this); } catch (Exception e) { System.out.println("oups..."); } } public void draw (int x1, int y1) { x = -x1; y = -y1; update(getGraphics()); } public Dimension minimumSize() { return new Dimension(vw,vh); } public Dimension preferredSize() { return new Dimension(vw,vh); } }
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com