Skip to content

Commit

Permalink
fix paint class
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Oct 7, 2016
1 parent 45c8bbf commit 201ae05
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 230 deletions.
117 changes: 66 additions & 51 deletions app/src/main/java/org/frice/android/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.View
import org.frice.android.obj.AbstractObject
import org.frice.android.obj.FObject
import org.frice.android.obj.PhysicalObject
import org.frice.android.obj.button.FButton
import org.frice.android.obj.button.FText
import org.frice.android.obj.button.SimpleButton
Expand All @@ -17,9 +19,6 @@ import org.frice.android.utils.graphics.shape.FOval
import org.frice.android.utils.graphics.shape.FRectangle
import org.frice.android.utils.message.error.FatalError
import org.frice.android.utils.message.log.FLog
import org.frice.game.obj.AbstractObject
import org.frice.game.obj.FObject
import org.frice.game.obj.PhysicalObject
import org.frice.game.obj.effects.LineEffect
import org.frice.game.obj.sub.ShapeObject
import org.frice.game.resource.FResource
Expand Down Expand Up @@ -261,9 +260,14 @@ open class Game : AppCompatActivity() {
style = Paint.Style.FILL
}

private fun Paint.reset() {
color = ColorResource.GRAY.color
}

override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
processBuffer()
canvas?.save()

objects.forEach { o ->
if (o is FObject) {
Expand All @@ -273,78 +277,89 @@ open class Game : AppCompatActivity() {
}

canvas?.let {
canvas.save()

objects.forEach { o ->

canvas.restore()
p.reset()

if (autoGC && (o.x.toInt() < -width ||
o.x.toInt() > width + width ||
o.y.toInt() < -height ||
o.y.toInt() > height + height)) {
if (o is PhysicalObject) o.died = true
removeObject(o)
}

canvas.restore()
if (o is PhysicalObject) canvas.rotate(o.rotate.toFloat(),
(o.x + o.width / 2).toFloat(), (o.y + o.height / 2).toFloat())
else canvas.rotate(o.rotate.toFloat(), o.x.toFloat(), o.y.toFloat())
when (o) {
is FObject.ImageOwner ->
canvas.drawBitmap(o.image, o.x.toFloat(), o.y.toFloat(), p)
is ShapeObject -> {
p.color = o.getResource().color
when (o.collideBox) {
is FRectangle -> canvas.drawRect(
o.x.toFloat(),
o.y.toFloat(),
o.width.toFloat(),
o.height.toFloat(),
p
)
is FOval -> canvas.drawOval(
RectF(
o.x.toFloat(),
o.y.toFloat(),
o.width.toFloat(),
o.height.toFloat()
),
p
)
}
}
is LineEffect -> {
p.color = o.colorResource.color
canvas.drawLine(
if (o is PhysicalObject) canvas.rotate(o.rotate.toFloat(),
(o.x + o.width / 2).toFloat(), (o.y + o.height / 2).toFloat())
else canvas.rotate(o.rotate.toFloat(), o.x.toFloat(), o.y.toFloat())
when (o) {
is FObject.ImageOwner ->
canvas.drawBitmap(o.image, o.x.toFloat(), o.y.toFloat(), p)
is ShapeObject -> {
p.color = o.getResource().color
when (o.collideBox) {
is FRectangle -> canvas.drawRect(
o.x.toFloat(),
o.y.toFloat(),
o.x2.toFloat(),
o.y2.toFloat(),
o.width.toFloat(),
o.height.toFloat(),
p
)
is FOval -> canvas.drawOval(
RectF(
o.x.toFloat(),
o.y.toFloat(),
o.width.toFloat(),
o.height.toFloat()
),
p
)
}
}
is LineEffect -> {
p.color = o.colorResource.color
canvas.drawLine(
o.x.toFloat(),
o.y.toFloat(),
o.x2.toFloat(),
o.y2.toFloat(),
p
)
}
}

texts.forEach { b ->

canvas.restore()
p.reset()

if (b is FButton) {
p.color = b.getColor().color
}
when (b) {
is FObject.ImageOwner ->
canvas.drawBitmap(b.image, b.x.toFloat(), b.y.toFloat(), p)
is SimpleButton -> {
p.color = b.getColor().color
canvas.drawRoundRect(b.x.toFloat(), b.y.toFloat(),
b.width.toFloat(), b.height.toFloat(),
Math.min((b.width * 0.5).toFloat(), 10),
Math.min((b.height * 0.5).toFloat(), 10), p)
canvas.drawText(b.text, b.x.toFloat(), b.y.toFloat(), p)
when (b) {
is FObject.ImageOwner ->
canvas.drawBitmap(b.image, b.x.toFloat(), b.y.toFloat(), p)
is SimpleButton -> {
p.color = b.getColor().color
canvas.drawRoundRect(
RectF(
b.x.toFloat(),
b.y.toFloat(),
b.width.toFloat(),
b.height.toFloat()
),
Math.min((b.width * 0.5).toFloat(), 10F),
Math.min((b.height * 0.5).toFloat(), 10F), p)
p.color = ColorResource.GRAY.color
canvas.drawText(b.text, b.x.toFloat(), b.y.toFloat(), p)
}
}
else -> canvas.drawText(b.text, b.x.toFloat(), b.y.toFloat(), p)
}
} else canvas.drawText(b.text, b.x.toFloat(), b.y.toFloat(), p)
}
}

if (showFPS) canvas.drawText("fps: $fpsDisplay", 30F, height - 30F, p)

customDraw(canvas)
}
}
Expand Down
180 changes: 2 additions & 178 deletions app/src/main/java/org/frice/game/Game.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
package org.frice.game

import org.frice.android.obj.button.FButton
import org.frice.android.resource.graphics.ColorResource
import org.frice.game.event.OnClickEvent
import org.frice.game.event.OnKeyEvent
import org.frice.game.event.OnMouseEvent
import org.frice.game.event.OnWindowEvent
import org.frice.game.obj.AbstractObject
import org.frice.game.obj.FObject
import org.frice.game.obj.PhysicalObject
import org.frice.game.obj.button.FButton
import org.frice.game.obj.button.FText
import org.frice.game.obj.button.SimpleButton
import org.frice.game.obj.effects.LineEffect
import org.frice.game.obj.sub.ImageObject
import org.frice.game.obj.sub.ShapeObject
import org.frice.game.resource.FResource
import org.frice.game.resource.graphics.ColorResource
import org.frice.game.resource.image.ImageResource
import org.frice.game.utils.graphics.shape.FOval
import org.frice.game.utils.graphics.shape.FRectangle
import org.frice.game.utils.graphics.utils.ColorUtils
import org.frice.game.utils.message.FDialog
import org.frice.game.utils.message.error.FatalError
import org.frice.game.utils.message.log.FLog
import org.frice.game.utils.misc.forceRun
import org.frice.game.utils.misc.loop
import org.frice.game.utils.misc.loopIf
import org.frice.game.utils.time.FTimeListener
import org.frice.game.utils.time.FTimer
import java.awt.*
import java.awt.event.*
import java.awt.image.BufferedImage
import java.util.*
import javax.imageio.ImageIO
import javax.swing.JFrame
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.WindowConstants
import org.frice.android.resource.image.ImageResource

/**
* First game class(not for you)
Expand All @@ -54,151 +17,12 @@ import javax.swing.WindowConstants
* @author ice1000
* @since v0.2.3
*/
abstract class Game() : JFrame(), Runnable {

private val buffer: BufferedImage

private val panel: GamePanel
private val stableBuffer: BufferedImage

private val bg: Graphics2D
get() = buffer.graphics as Graphics2D


init {
iconImage = ImageIO.read(javaClass.getResourceAsStream("/icon.png"))

Thread(this).start()
}


protected fun drawEverything(getBGG: () -> Graphics2D) {
processBuffer()

// is LineEffect -> bgg.drawLine(o.x.toInt(), o.y.toInt(), o.x2.toInt(), o.y2.toInt())
// }
// if (autoGC && (o.x.toInt() < -width ||
// o.x.toInt() > width + width ||
// o.y.toInt() < -height ||
// o.y.toInt() > height + height)) {
// if (o is PhysicalObject) o.died = true
// removeObject(o)
// }
// }

texts.forEach { b ->
val bgg = getBGG()
if (b is FButton) {
when (b) {
is FObject.ImageOwner -> bgg.drawImage(b.image, b.x.toInt(), b.y.toInt(), this)
is SimpleButton -> {
bgg.color = b.getColor().color
bgg.fillRoundRect(b.x.toInt(), b.y.toInt(),
b.width.toInt(), b.height.toInt(),
Math.min((b.width * 0.5).toInt(), 10),
Math.min((b.height * 0.5).toInt(), 10))
bgg.color = ColorResource.GRAY.color
bgg.drawString(b.text, b.x.toInt() + 10, (b.y + (b.height / 2)).toInt())
}
}
} else bgg.drawString(b.text, b.x.toInt(), b.y.toInt())
}
}
abstract class Game() {

/**
* get a screenShot.
*
* @return screen cut as an bitmap
*/
fun getScreenCut() = ImageResource.create(stableBuffer)

/**
* this method escaped the error
*
* @return exact position of the mouse
*/
override fun getMousePosition() = panel.mousePosition!!

private fun drawBackground(back: FResource, g: Graphics2D) {
when (back) {
is ImageResource -> g.paint = TexturePaint(back.bitmap, Rectangle(0, 0, width, height))
is ColorResource -> g.color = back.color
else -> throw FatalError("Unable to draw background")
}
g.fillRect(0, 0, width, height)
}

/**
* Demo24 game view.
* all rendering work and game object calculating are here.
*
* Created by ice1000 on 2016/8/13.
* @author ice1000
* @since v0.1
*/
inner class GamePanel() : JPanel() {
init {
addMouseListener(object : MouseListener {
override fun mouseClicked(e: MouseEvent) {
click(OnMouseEvent.create(e, OnMouseEvent.MOUSE_CLICK))
onClick(OnClickEvent.create(e))
}

override fun mouseEntered(e: MouseEvent) = onMouse(OnMouseEvent.create(e, OnMouseEvent.MOUSE_ENTERED))
override fun mouseExited(e: MouseEvent) = onMouse(OnMouseEvent.create(e, OnMouseEvent.MOUSE_EXITED))
override fun mouseReleased(e: MouseEvent) {
mouse(OnMouseEvent.create(e, OnMouseEvent.MOUSE_RELEASED))
onMouse(OnMouseEvent.create(e, OnMouseEvent.MOUSE_RELEASED))
}

override fun mousePressed(e: MouseEvent) {
mouse(OnMouseEvent.create(e, OnMouseEvent.MOUSE_PRESSED))
onMouse(OnMouseEvent.create(e, OnMouseEvent.MOUSE_PRESSED))
}
})

addWindowListener(object : WindowListener {
override fun windowDeiconified(e: WindowEvent) = Unit
override fun windowActivated(e: WindowEvent) {
loseFocus = false
onFocus(OnWindowEvent.create(e))
}

override fun windowDeactivated(e: WindowEvent) {
loseFocus = true
onLoseFocus(OnWindowEvent.create(e))
}

override fun windowIconified(e: WindowEvent) = Unit
override fun windowClosing(e: WindowEvent) = onExit()
override fun windowClosed(e: WindowEvent) = Unit
override fun windowOpened(e: WindowEvent) = Unit
})
}

override fun update(g: Graphics?) = paint(g)
override fun paintComponent(g: Graphics) {
drawBackground(back, bg)
drawEverything({ bg })

if (loseFocus && loseFocusChangeColor) {
loop(buffer.width) { x ->
loop(buffer.height) { y ->
buffer.setRGB(x, y, ColorUtils.darkerRGB(buffer.getRGB(x, y)))
}
}
}

if (showFPS) bg.drawString("fps: $fpsDisplay", 30, height - 30)

/*
* 厚颜无耻
* bg.drawString("Powered by FriceEngine. ice1000", 5, 20)
*/

stableBuffer.graphics.drawImage(buffer, 0, 0, this)
g.drawImage(buffer, 0, 0, this)
}
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/org/frice/game/obj/effects/Effects.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.frice.game.obj.effects

import android.graphics.Bitmap
import org.frice.android.obj.AbstractObject
import org.frice.android.resource.graphics.ColorResource
import org.frice.android.resource.graphics.CurveResource
import org.frice.android.resource.graphics.FunctionResource
import org.frice.android.resource.image.ImageResource
import org.frice.game.obj.AbstractObject
import org.frice.game.obj.sub.ImageObject

/**
Expand Down

0 comments on commit 201ae05

Please sign in to comment.