public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); View view = LayoutInflater.from(getContext()).inflate(resourceID, null); ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image); TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitName.setText(fruit.getName()); return view; }
public static void main(String[] args) { Fruit fruit = new Fruit(); fruit.setPrecio(100); Book book = new Book(); book.setPrecio(100); Iva iva = new Iva(); System.out.println("Precio neto Fruit: " + fruit.accept(iva)); System.out.println("Precio neto Book: " + book.accept(iva)); }
@Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); View view; ViewHolder viewHolder; if (convertView == null) { view = LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; }
public void skull() { createEffect("images/bonus_effect_black.png"); // Skaka skärmen gameListener.startShaking(8, 20, 5); PhysicsObject iteratorObject = null; for (int i = 0; i < gameListener.getCollidableObjectList().size(); i++) { iteratorObject = gameListener.getCollidableObjectList().get(i); if (iteratorObject instanceof Fruit) { ((Fruit) iteratorObject).getListener().setDestroyed(true); gameListener.setFruitSelected(false); ((Fruit) iteratorObject).getListener().setSplatImage("images/brown_splat.png"); gameListener.setFruitWasted(gameListener.getFruitWasted() + 1); } } }
/* * 需要扩展程序,添加新的类(Pear)时,需要修改工厂类 * 不符合开放封闭原则。 * * 工厂方法模式同样属于类的创建型模式,又被称为多态工厂模式。工厂方法模式的意义 * 是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不 * 再负责产品的创建,这样核心类称为一个抽象工厂角色,仅负责具体工厂子类必须实现 * 的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角 * 色的情况下引进新的产品。 */ public static void main(String[] args) { Fruit apple = FruitFactory.getFruit("Apple"); Fruit banana = FruitFactory.getFruit("Banana"); Fruit pear = FruitFactory.getFruit("Pear"); apple.get(); banana.get(); pear.get(); }
@Override public int visit(Fruit fruit) { int prix = fruit.getPrixKilos() * fruit.getPoids(); System.out.println(fruit.getName() + " coûte " + prix); return prix; }
public static void main(String[] args) { // TODO Auto-generated method stub Fruit fruit = Factory.getInstance("com.designpattern.Orange"); fruit.eat(); }
@Override public boolean onTouch(MotionEvent e) { switch (e.getAction()) { case MotionEvent.ACTION_DOWN: if (gamestate == State.ROUNDSUMMARY || gamestate == State.STARTGAME || gamestate == State.PLAYERDIED) { // Game�̏����� randomSpeed = 1; gamestate = State.STARTROUND; // prep and start round return false; // no followup msgs } else if (gamestate == State.GAMEOVER) { act.leaveGame(); // user touched after gameover -> back to entry screen return false; // no followup msgs } else { synchronized (fruitsSelectable) { Iterator<Fruit> itf = fruitsSelectable.iterator(); while (itf.hasNext()) { Fruit f = itf.next(); if (f.hasCollision(e.getX(), e.getY())) if (f.seed == ketseed) { // user popped ketchup act.playSound(Sound.KSPLAT); f.burst(); loseLife(); return false; // no followup msgs } else { // user picked up a fruit selectedFruit = f; } } } if (mVelocityTracker == null) { // Retrieve a new VelocityTracker object to watch the velocity of a motion. mVelocityTracker = VelocityTracker.obtain(); } else { // Reset the velocity tracker back to its initial state. mVelocityTracker.clear(); } // Add a user's movement to the tracker. mVelocityTracker.addMovement(e); } break; case MotionEvent.ACTION_MOVE: if (selectedFruit != null) { selectedFruit.x = e.getX(); selectedFruit.y = e.getY(); } mVelocityTracker.addMovement(e); break; case MotionEvent.ACTION_UP: if (selectedFruit != null) { Fruit f = selectedFruit; selectedFruit = null; mVelocityTracker.computeCurrentVelocity(1000); int pointerId = e.getPointerId(e.getActionIndex()); float tvx = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId); float tvy = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId); if (-tvy > 10) { // there is upward motion at release-- user threw fruit // scale throw speed for display size/density tvx = tvx / act.densityscalefactor; tvy = tvy / act.densityscalefactor; // help ease perspective problem when we release fruit away from the horizontal center // of the screen tvx += (e.getX() - width / 2) * 3.5 * act.densityscalefactor; f.throwFruit(tvx, tvy); synchronized (fruitsFlying) { fruitsFlying.add(f); fruitsSelectable.remove(f); } // attempting to adjust sound for how hard fruit was thrown horizontally. // hardness == 0 --> not thrown with any force // hardness == 1 --> thrown as hard as possible // assume that 5000 represents "really fast"; z vel should sound "harder" than y-vel float hardness = (f.vz - f.vy / 2) / 5000; // vy: up is negative. if (hardness >= 1f) hardness = 1.0f; if (hardness < .3f) hardness = .3f; act.playSound(Sound.THROW, hardness * .9f, hardness * 2); } } mVelocityTracker.recycle(); // seems to be a bug here on android 4.4, causing IllegalStateException - not addressing, // since it doesn't affect game play, and may be resolved in later versions break; } return true; }
/** * draw the screen. * * @param c * @param v */ @Override public void draw(Canvas c, View v) { try { // actually draw the screen scaledDst.set(0, 0, width, height); c.drawBitmap(wallbtm, null, scaledDst, p); // draw wall's bounds, for debugging // p.setColor(Color.RED); // c.drawRect(wallbounds_at_screen_z, p); // draw fruits for (Fruit f : fruitsSplatted) { drawFruit3Dcoords(c, f, f.getSplatBitmap(), wallxcenter, wallycenter); } synchronized (fruitsFlying) { for (Fruit f : fruitsFlying) { drawFruit3Dcoords(c, f, f.getBitmap(System.nanoTime()), wallxcenter, wallycenter); } } synchronized (fruitsSelectable) { for (Fruit f : fruitsSelectable) { // selectable fruit is on z=0, so we can just display normally: c.drawBitmap(f.getBitmap(), f.x - f.seed.halfWidth, f.y - f.seed.halfHeight, p); } } // draw combo hits for (Combo combo : hitCombos.keySet()) { ComboHit ch = hitCombos.get(combo); // p.setColor(Color.YELLOW); p.setARGB( ch.alpha, 190 + (int) (Math.random() * 60), 190 + (int) (Math.random() * 60), (int) (Math.random() * 60)); p.setTypeface(act.getGameFont()); p.setTextSize(act.TS_NORMAL); c.drawText(combo.name, ch.x, ch.y, p); } // c.drawText("fps: "+fps // +" w:"+width + " h:" +height // +"x:"+touchx+" y:"+touchy+" // tvx:"+(int)touchvx+"\ttvy:"+(int)touchvy+ // "\tflying:" + fruitsFlying.size() // + "\nff vz:" + (fruitsFlying.size() > 0 ? fruitsFlying.get(0).vz // : -1) // + "\nff vy:" + (fruitsFlying.size() > 0 ? fruitsFlying.get(0).vy // : -1) // + " ffvz:" + (fruitsFlying.size() > 0 ? fruitsFlying.get(0).vz : // -1), // , 0, 200, p); p.setColor(Color.WHITE); p.setTextSize(act.TS_NORMAL); p.setTypeface(act.getGameFont()); p.setFakeBoldText(true); // drawtext draws bottom-aligned? c.drawText("ROUND: " + round, width - rhstextoffset, statstextheight, p); c.drawText("LIVES: " + lives, width - rhstextoffset, statstextheight2, p); c.drawText("SCORE: " + score, 10, statstextheight, p); if (score >= hiscore) { hiscore = score; hilev = round; } c.drawText("HIGH: " + hiscore + ", r" + hilev, 10, statstextheight2, p); // game programming! pure and constant state manipulation! // this is like fingernails on a chalkboard for the functional programming crowd if (gamestate == State.ROUNDSUMMARY || gamestate == State.STARTGAME || gamestate == State.PLAYERDIED || gamestate == State.GAMEOVER) { if (gamestate != State.STARTGAME) { // round ended, by completion or player death, display stats int splatPct = (int) (nWallSplats * 100 / nTotFruit); drawCenteredText( c, splatPct + "% sPLAttaGe! (" + minRoundPassPct + "% required)", height / 3, p, 0); if (gamestate == State.ROUNDSUMMARY) { if (splatPct < 80) drawCenteredText(c, "not too bad.", (int) (height / 2.5), p, 0); else if (splatPct < 85) drawCenteredText(c, "nice!", (int) (height / 2.5), p, 0); else if (splatPct <= 95) { drawCenteredText(c, "cRudE!", (int) (height / 2.5), p, 0); } else if (round > 10) { c.drawText("Dude, really?!", width / 4, (int) (height / 2.5), p); c.drawText("Awesome.", width / 3, (int) (height / 2.2), p); } else { drawCenteredText(c, "eEEeEeeEh!! sPAzMiC!", (int) (height / 2.5), p, 0); } } else if (gamestate == State.PLAYERDIED || gamestate == State.GAMEOVER) c.drawText("...Ooops.", width / 3, (int) (height / 2.5), p); } if (gamestate != State.PLAYERDIED && gamestate != State.GAMEOVER) { String msg = levelmsgMap.get(Integer.valueOf(round)); if (msg != null) { if (msg.contains(LINE_SPLIT_MARKER)) { drawCenteredText( c, msg.substring(0, msg.indexOf(LINE_SPLIT_MARKER)), height * 3 / 5, p, 0); drawCenteredText( c, msg.substring(msg.indexOf(LINE_SPLIT_MARKER) + 1), height * 2 / 3, p, 0); } else drawCenteredText(c, msg, height * 3 / 5, p, 0); } } if (gamestate != State.GAMEOVER) { p.setTextSize(act.TS_BIG); p.setColor(Color.RED); drawCenteredText(c, "Touch to continue", height * 4 / 5, p, -2); p.setColor(Color.WHITE); drawCenteredText(c, "Touch to continue", height * 4 / 5, p, 0); } } if (gamestate == State.GAMEOVER) { p.setTextSize(act.TS_BIG); p.setColor(Color.RED); drawCenteredText(c, "GamE oVeR!", height / 2, p, -2); drawCenteredText(c, "Touch to end game", height * 4 / 5, p, -2); p.setColor(Color.WHITE); drawCenteredText(c, "GamE oVeR!", height / 2, p, 0); drawCenteredText(c, "Touch to end game", height * 4 / 5, p, 0); } } catch (Exception e) { Log.e(MainActivity.LOG_ID, "draw", e); e.printStackTrace(); } }
@Override public void update(View v) { long newtime = System.nanoTime(); // �ɋ}������ // �[������ if (lastSeedCount != fruitsSplatted.size()) { Random random = new Random(); int randomIndex = random.nextInt(2) + 1; if (randomIndex % 2 == 0) { // ����������+0.5�͈͂ł����Ă��� // 1~5�͈̔͂��쐬����1/10�ɂ��� randomSpeed += (random.nextInt(3) + 1) / 10.0f; float decimalSpeed = random.nextFloat(); // 0.1~1.0 // randomSpeed = random.nextInt(4) + decimalSpeed + 1; // 1~5 System.out.println(randomSpeed); } lastSeedCount = fruitsSplatted.size(); // �ʕ��̕\���� } String speedStr = String.format("SPEED:%f", randomSpeed); System.out.println(speedStr); float elapsedsecs = (float) (newtime - frtime) / ONESEC_NANOS * randomSpeed; frtime = newtime; fps = (int) (1 / elapsedsecs); // update combo hits Iterator<Combo> hcit = hitCombos.keySet().iterator(); while (hcit.hasNext()) { Combo combo = hcit.next(); ComboHit ch = hitCombos.get(combo); ch.y -= COMBOHIT_SPEED * elapsedsecs; float chtime = frtime - ch.hitTime; ch.alpha = (int) (255 * (1.0f - chtime / COMBOHIT_DISPLAYTIME)); if (frtime - ch.hitTime > COMBOHIT_DISPLAYTIME / 3) fps = 0; // excuse to put a breakpoint here if (frtime - ch.hitTime > COMBOHIT_DISPLAYTIME) hcit.remove(); } if (gamestate == State.STARTROUND) { // this goofy construction is to make sure we initialize the round from // the update/draw thread, not from the UI thread. initRound(); return; } if (width == 0) { // set variables that rely on screen size width = v.getWidth(); height = v.getHeight(); wallxcenter = width / 2; wallycenter = (int) (height * WALL_Y_CENTER_FACTOR); inity = (int) (INIT_SELECTABLE_Y_FACTOR * height); // initial fruit placement, also bottom of wall. // attempt to compute wall bounds at wall z from screen size. constants are pure // magic, found thru trial and error iterations. // if the background picture changes, they will need to be recalibrated. wallbounds_at_wall_z.set( (int) (-1.5 * width), (int) (-height * .94), (int) (2.43 * width), inity); // wall bounds AT WALL Z (!!WaLLzeY!!) // magic trial and error world-bounds contants, based on screen image size minXbound = 8 * -width; maxXbound = 8 * width; maxYbound = 5 * height; // compute wall bounds at screen z, used for clipping. int effl = (int) (wallbounds_at_wall_z.left + (WALLZFACT * (wallxcenter - wallbounds_at_wall_z.left))); int efft = (int) (wallbounds_at_wall_z.top + (WALLZFACT * (wallycenter - wallbounds_at_wall_z.top))); int effr = (int) (wallbounds_at_wall_z.right + (WALLZFACT * (wallxcenter - wallbounds_at_wall_z.right))); int effb = (int) (wallbounds_at_wall_z.bottom + (WALLZFACT * (wallycenter - wallbounds_at_wall_z.bottom))); wallbounds_at_screen_z.set(effl, efft, effr, effb); p.setTextSize(act.TS_NORMAL); p.setTypeface(act.getGameFont()); String t = "SCORE: 999999"; rhstextoffset = (int) p.measureText(t); p.getTextBounds(t, 0, t.length() - 1, scaledDst); statstextheight = (int) (scaledDst.height() + 5); statstextheight2 = statstextheight * 2; } if (gamestate == State.RUNNING && fruitsSelectable.size() < maxShownSelectableFruit && seedsQueued.size() > 0 && frtime > possspawntime + MIN_SPAWN_INTERVAL_NANOS) { possspawntime = frtime; // "every now and then" make a fruit available if (Math.random() > .6) { Fruit newf = null; if (fruitsRecycled.size() > 0) { // recycle a fruit if we can newf = fruitsRecycled.get(0); fruitsRecycled.remove(0); } else { // create if needed newf = new Fruit(); } // choose fruit Seed s = seedsQueued.get(0); seedsQueued.remove(0); int initx = (int) -s.halfWidth; int speed = selectable_speed; if (Math.random() > .5) { initx = (int) (width + s.halfWidth); speed = -speed; } newf.init(s, initx, inity, 0, speed); fruitsSelectable.add(newf); } } else if (gamestate == State.RUNNING && fruitsSelectable.size() == 0 && fruitsFlying.size() == 0 && seedsQueued.size() == 0) { // round is complete if (nWallSplats * 100 / nTotFruit >= minRoundPassPct) { act.playSound(Sound.PASSLEVEL); round++; gamestate = State.ROUNDSUMMARY; } else loseLife(); } // update fruit positions synchronized (fruitsFlying) { Iterator<Fruit> fit = fruitsFlying.iterator(); while (fit.hasNext()) { Fruit f = fit.next(); f.x += f.vx * elapsedsecs; f.y += f.vy * elapsedsecs; f.z += f.vz * elapsedsecs; f.vy += ACC_GRAVITY * elapsedsecs; if (f.z >= WALL_Z && wallbounds_at_wall_z.contains((int) f.x, (int) f.y)) { // fruit has hit wall fit.remove(); fruitsSplatted.add(f); nWallSplats++; if (f.isBonus == true) { score += f.seed.points * 5; } else { score += f.seed.points; } act.playSound(f.getSplatSound()); // check combo for (Combo c : combos) { neededSeeds.clear(); neededSeeds.addAll(c.seeds); neededSeeds.remove(f.seed); comboFruits.clear(); comboFruits.add(f); for (Fruit spf : fruitsSplatted) { if (neededSeeds.contains(spf.seed)) { if (spf.getBounds().intersect(f.getBounds())) { neededSeeds.remove(spf.seed); comboFruits.add(spf); } if (neededSeeds.size() == 0) break; } } if (neededSeeds.size() == 0) { // combo is hit score += c.points; for (Fruit spf : comboFruits) { fruitsSplatted.remove(spf); } // display combo hit message "somewhere next to" combo hit effpt = renderFromZ(f.x, f.y, f.z, wallxcenter, wallycenter); ComboHit ch = new ComboHit(); ch.x = effpt.x + (float) Math.random() * 100 - 50; ch.y = effpt.y + (float) Math.random() * 100 - 80; // play sound act.playSound(Sound.COMBO); // ensure combo display is fully onscreen p.getTextBounds(c.name, 0, c.name.length(), scaledDst); if (ch.x < 0) ch.x = 0; else if (ch.x > width - scaledDst.width()) ch.x = width - scaledDst.width(); ch.hitTime = System.nanoTime(); hitCombos.put(c, ch); } } } else if (f.y > inity && f.y < inity + f.vy * elapsedsecs && f.z > WALL_Z / 2) { // fruit has hit ground near wall fit.remove(); fruitsSplatted.add(f); } else if (f.z > WALL_Z // here we goofily force java to call render function when we need it && (effpt = renderFromZ(f.x, f.y, f.z, wallxcenter, wallycenter)) != null && wallbounds_at_screen_z.contains(effpt.x, effpt.y)) { // wild pitch, behind wall fit.remove(); fruitsRecycled.add(f); } else if (f.y > maxYbound || f.x >= maxXbound || f.x <= minXbound) { // wild pitch, out of bounds fit.remove(); fruitsRecycled.add(f); } } } if (gamestate == State.RUNNING) { synchronized (fruitsSelectable) { Iterator<Fruit> fit = fruitsSelectable.iterator(); while (fit.hasNext()) { Fruit f = fit.next(); if (f != selectedFruit) { // f.x += f.vx * elapsedsecs; f.x += f.vx * elapsedsecs * f.scroll; // wobble displayable fruit up and down, and return them to regular line when let go // int targy = inity + (int)(Math.sin(f.x/15)* selectable_y_play); // f.y += (targy - f.y) / SELECTABLE_FRUIT_BRAKING_FACTOR; int targy = inity + (int) (Math.sin(f.x / 65) * selectable_y_play); f.y += (targy - f.y) / SELECTABLE_FRUIT_BRAKING_FACTOR; } if (f.x < -f.seed.halfWidth || f.x > width + f.seed.halfWidth) { // we floated off screen fit.remove(); fruitsRecycled.add(f); } } } } }
void CheckEorEnumUalue(Fruit a) { int value = a.getPrice(); }
public static void main(String[] args) { Fruit fruit = new Fruit("香蕉", 5, "北京"); fruit.sell(); }