/* * Alec Jacobson * */ import java.awt.*; import java.util.LinkedList; class DropDown{ Color outline_color = Color.BLACK; Color foreground = Color.BLACK; Color background = Color.WHITE; DropDownItem items[]; int x, y, width, height; boolean is_selected = false; int selected_index; public DropDown(int num_items){ items = new DropDownItem[num_items]; } } class DropDownItem{ Color outline_color = Color.WHITE; Color foreground = Color.BLACK; Color background = Color.WHITE; String text; boolean is_selected = false; public DropDownItem(String text){ this.text = text; } public DropDownItem( String text, Color outline_color, Color foreground, Color background) { this.text = text; this.outline_color = outline_color; this.foreground = foreground; this.background = background; } } class Slider{ static final int RADIUS = 10; int x,y,width,height; double f = 0.5; Color color = Color.GREEN; boolean is_selected = false; public Slider(Color color){ this.color = color; } public Slider(Color color, int x, int y, int width, int height){ this.color = color; this.x = x; this.y = y; this.width = width; this.height = height; } } class Button{ static final int WIDTH = 100; static final int HEIGHT= 20; int x,y; Color background = Color.WHITE; Color foreground = Color.BLACK; boolean is_selected = false; String text = "button"; int click_count = 0; public Button(String text){ this.text = text; } public Button(String text, int x, int y){ this.text = text; this.x = x; this.y = y; } public Button(String text, int x, int y, Color foreground, Color background){ this.text = text; this.x = x; this.y = y; this.foreground = foreground; this.background = background; } } /** * * @author ajx */ public class Versus extends BufferedApplet{ int w, h;//bounds of applet int bottom_x=0; int bottom_y=0; int top_x=0; int top_y=0; int old_bottom_x=0; int old_bottom_y=0; int old_top_x=0; int old_top_y=0; int bottom_handle_x=0; int bottom_handle_y=0; int top_handle_x=0; int top_handle_y=0; int mx = 0; int my = 0; int size1= 128; int size2= 128; final Font menu_font = new Font("SansSerif", Font.PLAIN, 12); long last_type = 0; float alpha = 0.3f; boolean initialized = false; boolean fitted = false; Choice FontChooser1 = new Choice(); Choice FontChooser2 = new Choice(); TextField text_field = new TextField("Rr "); Button size1_button; Button size2_button; Button grid_button; Button link_button; Button reset_button; Button key_button; Slider slider1; Slider slider2; Slider stationary_slider; boolean grid_on = false; boolean dragging1 = false; boolean dragging2 = false; final int HANDLE_DIAMETER = 20; Color color1; Color color2; Button down_button = null; boolean shift_down = false; DropDown color_chooser1; DropDown color_chooser2; final Color[] COLORS = new Color[] { Color.BLACK, Color.WHITE, Color.PINK, Color.MAGENTA, Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE }; //Label label = new Label("versus"); public void initialize() { Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); for(int i=0;iMath.abs(bottom_y-old_bottom_y)) bottom_y = old_bottom_y; else bottom_x = old_bottom_x; } } bottom_handle_x = bottom_x-HANDLE_DIAMETER; bottom_handle_y = bottom_y; g2.drawString(text_field.getText(), bottom_x, bottom_y); g2.setColor(Color.WHITE); if(dragging1) g2.setColor(Color.BLACK); g2.fillOval(bottom_handle_x,bottom_handle_y,HANDLE_DIAMETER,HANDLE_DIAMETER); g2.setColor(color1); g2.drawOval(bottom_handle_x,bottom_handle_y,HANDLE_DIAMETER,HANDLE_DIAMETER); //DRAW TOP Font f2 = new Font(FontChooser2.getSelectedItem(), Font.PLAIN, (int)(slider2.f*h)); g2.setFont(f2); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); g2.setColor(color2); //g2.fillRect(FontChooser2.getLocation().x+FontChooser2.getWidth()+1,FontChooser2.getLocation().y-1, // w,FontChooser2.getHeight()+1); if(dragging2){ top_x = mx+HANDLE_DIAMETER/2; top_y = my-HANDLE_DIAMETER/2; if(shift_down){ if(Math.abs(top_x-old_top_x)>Math.abs(top_y-old_top_y)) top_y = old_top_y; else top_x = old_top_x; } } top_handle_x = top_x-HANDLE_DIAMETER; top_handle_y = top_y; g2.drawString(text_field.getText(), top_x, top_y); g2.setColor(Color.WHITE); if(dragging2) g2.setColor(Color.BLACK); g2.fillOval(top_handle_x,top_handle_y,HANDLE_DIAMETER,HANDLE_DIAMETER); g2.setColor(color2); g2.drawOval(top_handle_x,top_handle_y,HANDLE_DIAMETER,HANDLE_DIAMETER); //DRAW MENU ITEMS g2.setFont(menu_font); draw_slider(g2,slider1); draw_slider(g2,slider2); draw_button(g2,size1_button); draw_button(g2,size2_button); draw_button(g2,grid_button); draw_button(g2,link_button); draw_button(g2,key_button); draw_button(g2,reset_button); draw_dropdown(g2,color_chooser1); draw_dropdown(g2,color_chooser2); FontChooser1.repaint(); //label.repaint(); FontChooser2.repaint(); animating = true; } public boolean keyUp(Event e, int key) { if(!key_button.is_selected) return false; else{ /**switch(key){ case Event.DOWN: slider1.f *= 0.5; slider1.f = slider1.f<1 ? 1 : slider1.f; slider2.f *= 0.5; slider2.f = slider2.f<1 ? 1 : slider2.f; break; case Event.UP: slider1.f *= 2.0; slider2.f *= 2.0; break; case Event.RIGHT: slider2.f *= 1.1; break; case Event.LEFT: slider2.f /= 1.01; slider2.f = slider2.f<1 ? 1 : slider2.f; break; default: */ if(text_field.hasFocus()){ //do nothing...text_field will take care of things on her own }else if(System.currentTimeMillis()-last_type<2000){ text_field.setText(text_field.getText() + (char)key); }else{ text_field.setText(""+(char)key); } // toggle grid background if(text_field.getText().toLowerCase().equals("grid")) grid_button.is_selected = !grid_button.is_selected; // select two fonts at random if(text_field.getText().toLowerCase().equals("random")){ FontChooser1.select((int)(Math.random()*FontChooser1.getItemCount())); FontChooser2.select((int)(Math.random()*FontChooser2.getItemCount())); } if(text_field.getText().toLowerCase().equals("reset")) reset_button.is_selected = !reset_button.is_selected; //switch bottom and top fonts if(text_field.getText().toLowerCase().equals("switch")){ String temp = FontChooser1.getSelectedItem(); FontChooser1.select(FontChooser2.getSelectedItem()); FontChooser2.select(temp); } } last_type = System.currentTimeMillis(); return damage = true; } public boolean mouseDown(Event e, int x, int y) { mx = x; my = y; damage = true; // actions in priority // grabbing top's handle if(isInRectCircle(mx,my,top_handle_x,top_handle_y,HANDLE_DIAMETER)){ stationary_slider = slider1; dragging2 = true; old_top_x = top_x; old_top_y = top_y; // grabbing bottom's handle }else if(isInRectCircle(mx,my,bottom_handle_x,bottom_handle_y,HANDLE_DIAMETER)) { stationary_slider = slider2; dragging1 = true; old_bottom_x = bottom_x; old_bottom_y = bottom_y; }else if( isInRect( mx,my, color_chooser2.x,color_chooser2.y, color_chooser2.width, color_chooser2.height)) { color_chooser2.is_selected = true; }else if( isInRect( mx,my, color_chooser1.x,color_chooser1.y, color_chooser1.width, color_chooser1.height)) { color_chooser1.is_selected = true; }else if( isInRect(mx,my,size1_button.x,size1_button.y,Button.WIDTH,Button.HEIGHT)) { size1_button.click_count = 0; size1_button.is_selected = !size1_button.is_selected; }else if( isInRect(mx,my,size2_button.x,size2_button.y,Button.WIDTH,Button.HEIGHT)) { size2_button.click_count = 0; size2_button.is_selected = !size2_button.is_selected; }else if( isInRect(mx,my,grid_button.x,grid_button.y,Button.WIDTH,Button.HEIGHT)) { grid_button.click_count = 0; grid_button.is_selected = !grid_button.is_selected; }else if( isInRect(mx,my,link_button.x,link_button.y,Button.WIDTH,Button.HEIGHT)) { link_button.click_count = 0; link_button.is_selected = !link_button.is_selected; }else if( isInRect(mx,my,key_button.x,key_button.y,Button.WIDTH,Button.HEIGHT)) { key_button.click_count = 0; key_button.is_selected = !key_button.is_selected; }else if( isInRect(mx,my,reset_button.x,reset_button.y,Button.WIDTH,Button.HEIGHT)) { reset_button.click_count = 2; reset_button.is_selected = !reset_button.is_selected; }else if(isInRectCircle(mx,my,slider1.x+(int)(slider1.f*slider1.width)-Slider.RADIUS,slider1.y,Slider.RADIUS*2)){ slider1.is_selected = true; }else if(isInRectCircle(mx,my,slider2.x+(int)(slider2.f*slider2.width)-Slider.RADIUS,slider2.y,Slider.RADIUS*2)){ slider2.is_selected = true; }else if(isInRect(mx,my,slider1.x,slider1.y,slider1.width,slider1.height)){ slider1.is_selected = true; }else if(isInRect(mx,my,slider2.x,slider2.y,slider2.width,slider2.height)){ slider2.is_selected = true; } return true; } public boolean isInRect(int x, int y, int rx, int ry, int w, int h){ return (x>rx&&y>ry&&x2 ? (int)(stationary_slider.f*h)/10 : 2; int y = 0; while(y0){ g.drawLine(0,y,w,y); y-= grid_step; } int x = 0; while(x0){ g.drawLine(x,0,x,h); x-= grid_step; } } public void draw_button(Graphics2D g2, Button b){ // get correct backgroun and foreground colors Color background = null; Color foreground= null; if(b.is_selected||b.click_count>0){ background = b.foreground; foreground = b.background; b.click_count= b.click_count<=1 ? 0 : b.click_count-1; }else{ foreground= b.foreground; background = b.background; } // don't draw if not on screen if(b.y+Button.HEIGHT>h||b.y<0) return; if(b.x+Button.WIDTH>w||b.x<0) return; g2.setColor(background); g2.fillRect(b.x,b.y,Button.WIDTH,Button.HEIGHT); g2.setColor(foreground); g2.drawRect(b.x,b.y,Button.WIDTH,Button.HEIGHT); g2.drawString(b.text,b.x+4,b.y+Button.HEIGHT-4); } public void draw_slider(Graphics2D g2, Slider s){ g2.setColor(s.color); g2.fillRect(s.x,s.y,s.width,s.height); g2.setColor(Color.WHITE); if(s.is_selected|| ((slider1.is_selected||slider2.is_selected)&&link_button.is_selected)) g2.setColor(Color.BLACK); g2.fillOval(s.x+(int)(s.f*s.width)-Slider.RADIUS, s.y+s.height/2-Slider.RADIUS,Slider.RADIUS*2,Slider.RADIUS*2); g2.setColor(Color.BLACK); if(s.is_selected|| ((slider1.is_selected||slider2.is_selected)&&link_button.is_selected)) g2.setColor(Color.WHITE); g2.drawOval(s.x+(int)(s.f*s.width)-Slider.RADIUS, s.y+s.height/2-Slider.RADIUS,Slider.RADIUS*2,Slider.RADIUS*2); } public void update_slider(Slider s){ if(s.is_selected){ s.f = (double)(mx-s.x)/(double)(s.width); s.f = s.f>1 ? 1.0 : s.f; s.f = s.f<0 ? 0.0 : s.f; }else if(slider1.is_selected&&link_button.is_selected){ s.f = slider1.f; }else if(slider2.is_selected&&link_button.is_selected){ s.f = slider2.f; } } public void update_dropdown(DropDown d){ if(d.is_selected){ for(int i=0;i