package com.jengine.kevinchwong;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import com.threed.jpct.*;
import javax.swing.*;
public class SoftwareRenderTest {
private World world;
private FrameBuffer buffer;
private Object3D box;
private JFrame frame;
public static void main(String[] args) throws Exception {
new SoftwareRenderTest().loop();
}
public SoftwareRenderTest() throws Exception {
frame=new JFrame("Hello world");
frame.setSize(800, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
world = new World();
world.setAmbientLight(0, 255, 0);
TextureManager.getInstance().addTexture("box", new Texture("res/textures/box.jpg"));
box = Primitives.getBox(13f, 2f);
box.setTexture("box");
box.setEnvmapped(Object3D.ENVMAP_ENABLED);
box.build();
world.addObject(box);
world.getCamera().setPosition(50, -50, -5);
world.getCamera().lookAt(box.getTransformedCenter());
}
private void loop() throws Exception {
buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
while (frame.isShowing()) {
box.rotateY(0.01f);
buffer.clear(java.awt.Color.BLUE);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
//GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// ge.getAllFonts();
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
Graphics2D g2d = (Graphics2D) buffer.getGraphics();
g2d.setFont(new Font("Serif", Font.PLAIN, 13));
//g2d.setFont(new Font("Franklin Gothic Medium", Font.PLAIN, 33));
g2d.drawString("??Most relationships seem so transitory", 20, 130);
g2d.drawString("They're all good but not the permanent one", 20, 160);
g2d.drawString("Who doesn't long for someone to hold", 20, 190);
g2d.drawString("Who knows how to love you without being told", 20, 220);
g2d.drawString("Somebody tell me why I'm on my own", 20, 250);
g2d.drawString("If there's a soulmate for everyone", 20, 280);
Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
g2d.setStroke(new BasicStroke(1));
g2d.setColor(Color.gray);
for (double deg = 0; deg < 360; deg += 5) {
AffineTransform at =
AffineTransform.getTranslateInstance(400,300);
at.rotate(Math.toRadians(deg));
g2d.draw(at.createTransformedShape(e));
}
buffer.display(frame.getGraphics());
Thread.sleep(10);
}
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
buffer.dispose();
frame.dispose();
System.exit(0);
}
}
J Engine Team
Friday, January 31, 2014
Software Rendering with JPCT and java2d together
Test for XML Import and MOXy + EclipseLink
package kcwobjectxmljsontest;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.stream.StreamSource;
/**
*
* @author KCW
*/
public class kcwMOXytest {
@XmlRootElement
@XmlType(propOrder={"street", "city", "zip"})
@XmlAccessorType(XmlAccessType.FIELD)
static public class Address {
String street;
String city;
String zip;
}
@XmlRootElement
@XmlType(propOrder={"name","address"})
@XmlAccessorType(XmlAccessType.FIELD)
static public class Customer {
String name;
@XmlElement
Address address;
}
@XmlRootElement
static public class Customers {
@XmlElement(name="customer")
List<Customer> customers;
}
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customers.class);
System.out.println("---------------------");
System.out.println("Convert XML to Object");
System.out.println("---------------------");
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("data/customers.xml");
// Customers customers = (Customers) unmarshaller.unmarshal(xml);
Customers doc= (Customers) unmarshaller.unmarshal(xml);
// System.out.println("customers.test="+customers.test);
System.out.println("customerlist.customers.="+doc.customers.size());
int i=0;
for(Customer s : doc.customers) {
System.out.println("doc.customers.get("+i+").name="+s.name);
System.out.println("doc.customers.get("+i+").address.city="+s.address.city);
System.out.println("doc.customers.get("+i+").address.street="+s.address.street);
System.out.println("doc.customers.get("+i+").address.zip="+s.address.zip);
System.out.println("---------------------");
i++;
}
System.out.println("");
System.out.println("");
System.out.println("---------------------");
System.out.println("Convert Object to XML");
System.out.println("---------------------");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(doc, System.out);
FileOutputStream fos= new FileOutputStream("data/test-list.xml");
marshaller.marshal(doc, fos);
}
}
//================================================
// Customers.xml
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<name>Jane Doe</name>
<address>
<zip>19873</zip>
<street>1 A Street</street>
<city>Any Town</city>
</address>
</customer>
<customer>
<name>Jane Doe 2</name>
<address>
<zip>19872</zip>
<street>2 A Street</street>
<city>Any Town Area</city>
</address>
</customer>
</customers>
Test for XML and perst.jar
package kcwxmltest;
import org.garret.perst.*;
import java.io.*;
public class TestXML {
static class Record extends Persistent {
String strKey;
long intKey;
double realKey;
};
static class Indices extends Persistent {
Index strIndex;
FieldIndex intIndex;
FieldIndex compoundIndex;
}
final static int nRecords = 100000;
final static int pagePoolSize = 32*1024*1024;
static public void main(String[] args) throws Exception {
Storage db = StorageFactory.getInstance().createStorage();
for (int i = 0; i < args.length; i++) {
if ("altbtree".equals(args[i])) {
db.setProperty("perst.alternative.btree", Boolean.TRUE);
}
}
db.open("data/test1.dbs", pagePoolSize);
Indices root = (Indices)db.getRoot();
if (root == null) {
root = new Indices();
root.strIndex = db.createIndex(String.class, true);
root.intIndex = db.createFieldIndex(Record.class, "intKey", true);
root.compoundIndex = db.createFieldIndex(Record.class, new String[]{"strKey", "intKey"}, true);
db.setRoot(root);
}
FieldIndex intIndex = root.intIndex;
FieldIndex compoundIndex = root.compoundIndex;
Index strIndex = root.strIndex;
long start = System.currentTimeMillis();
long key = 1999;
int i;
for (i = 0; i < nRecords; i++) {
Record rec = new Record();
key = (3141592621L*key + 2718281829L) % 1000000007L;
rec.intKey = key;
rec.strKey = Long.toString(key);
rec.realKey = (double)key;
intIndex.put(rec);
strIndex.put(new Key(rec.strKey), rec);
compoundIndex.put(rec);
}
db.commit();
System.out.println("Elapsed time for inserting " + nRecords + " records: "
+ (System.currentTimeMillis() - start) + " milliseconds");
start = System.currentTimeMillis();
Writer writer = new BufferedWriter(new FileWriter("data/test.xml"));
db.exportXML(writer);
writer.close();
System.out.println("Elapsed time for XML export " + (System.currentTimeMillis() - start) + " milliseconds");
db.close();
db.open("data/test2.dbs", pagePoolSize);
start = System.currentTimeMillis();
Reader reader = new BufferedReader(new FileReader("data/test.xml"));
db.importXML(reader);
reader.close();
System.out.println("Elapsed time for XML import " + (System.currentTimeMillis() - start) + " milliseconds");
root = (Indices)db.getRoot();
intIndex = root.intIndex;
strIndex = root.strIndex;
compoundIndex = root.compoundIndex;
start = System.currentTimeMillis();
key = 1999;
for (i = 0; i < nRecords; i++) {
key = (3141592621L*key + 2718281829L) % 1000000007L;
String strKey = Long.toString(key);
Record rec1 = (Record)intIndex.get(new Key(key));
Record rec2 = (Record)strIndex.get(new Key(strKey));
Record rec3 = (Record)compoundIndex.get(new Key(strKey, new Long(key)));
Assert.that(rec1 != null);
Assert.that(rec1 == rec2);
Assert.that(rec1 == rec3);
Assert.that(rec1.intKey == key);
Assert.that(rec1.realKey == (double)key);
Assert.that(strKey.equals(rec1.strKey));
}
System.out.println("Elapsed time for performing " + nRecords*2 + " index searches: "
+ (System.currentTimeMillis() - start) + " milliseconds");
db.close();
}
}
// ==============================================
// Sample data not provided.
Test for the access to DB2
package kcwdb2test;
import com.ibm.as400.access.AS400JDBCConnectionPoolDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class KcwDB2TestDev {
public static void main(String[] args) {
Connection dbConnection = null;
String cipher="AES";
String cipherdomain="INTRANET";
String host="1.2.3.4";
String dblibrary="WWWDLIB";
String login="not-encoded";
String user="kcw";
String password="12345678";
try {
AS400JDBCConnectionPoolDataSource datasource = new AS400JDBCConnectionPoolDataSource(host, user, password);
datasource.setLibraries(dblibrary);
dbConnection = datasource.getConnection();
// Sample 1
String query = "SELECT DMNDMCDEC,USRFINAMC,USRLANAMC,USREMPIDN,USRUSIDNN FROM ASWDLIB.WWUSR10 where DMNDMCDEC='INTRANET'";
Statement statement = dbConnection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next()) {
System.out.println(
resultSet.getString(1) + " " +
resultSet.getString(2) + " " +
resultSet.getString(3) + " " +
resultSet.getInt(4) + " " +
resultSet.getInt(5));
}
statement.close();
// Sample 2
PreparedStatement statement2 = dbConnection.prepareStatement(
"SELECT DMNDMCDEC,USRFINAMC,USRLANAMC,USREMPIDN,USRUSIDNN FROM ASWDLIB.WWUSR10 where DMNDMCDEC=?"
);
statement2.setString(1, "INTRANET");
ResultSet resultSet2 = statement2.executeQuery();
while(resultSet2.next()) {
System.out.println(
resultSet2.getString(1) + " " +
resultSet2.getString(2) + " " +
resultSet2.getString(3) + " " +
resultSet2.getInt(4) + " " +
resultSet2.getInt(5));
}
statement2.close();
dbConnection.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
Leetcode: triangle
public class Solution {
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
int n1=triangle.size();
int n2=triangle.get(n1-1).size();
int[] sum=new int[n1+1];
// Bottom Up
for(int i=n1;i>0;i--)
{
int[] newsum=new int[n1+1];
for(int j=0;j<i;j++)
{
int ans1=sum[j];
int ans2=sum[j+1];
int ans=(ans1<ans2)?ans1:ans2;
newsum[j]=triangle.get(i-1).get(j)+ans;
}
sum=newsum;
}
return sum[0];
// Top down
/*
for(int i=0;i<n1;i++)
{
int[] newsum=new int[n2];
for(int j=0;j<=i;j++)
{
int x=triangle.get(i).get(j);
int ans1=sum[j]+x;
int ans2=Integer.MAX_VALUE;
if(j>0)ans2=sum[j-1]+x;
if(j==0)newsum[j]=ans1;
else if(j==i)newsum[j]=ans2;
else newsum[j]=(ans1<ans2)?ans1:ans2;
}
sum=newsum;
}
int m=Integer.MAX_VALUE;
for(int i=0;i<n2;i++)
if(sum[i]<m)m=sum[i];
return m;
*/
}
}
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
int n1=triangle.size();
int n2=triangle.get(n1-1).size();
int[] sum=new int[n1+1];
// Bottom Up
for(int i=n1;i>0;i--)
{
int[] newsum=new int[n1+1];
for(int j=0;j<i;j++)
{
int ans1=sum[j];
int ans2=sum[j+1];
int ans=(ans1<ans2)?ans1:ans2;
newsum[j]=triangle.get(i-1).get(j)+ans;
}
sum=newsum;
}
return sum[0];
// Top down
/*
for(int i=0;i<n1;i++)
{
int[] newsum=new int[n2];
for(int j=0;j<=i;j++)
{
int x=triangle.get(i).get(j);
int ans1=sum[j]+x;
int ans2=Integer.MAX_VALUE;
if(j>0)ans2=sum[j-1]+x;
if(j==0)newsum[j]=ans1;
else if(j==i)newsum[j]=ans2;
else newsum[j]=(ans1<ans2)?ans1:ans2;
}
sum=newsum;
}
int m=Integer.MAX_VALUE;
for(int i=0;i<n2;i++)
if(sum[i]<m)m=sum[i];
return m;
*/
}
}
Thursday, January 30, 2014
LeetCode : Search insert position
It is easy to do by O(N)...
But this solution is by O(logN)
public class Solution {
public int searchInsert(int[] A, int target) {
int f=0;
int l=A.length;
int m;
while(true)
{
m=(f+l)/2;
if(m>0&&m<A.length)
{ if(target>A[m-1]&&target<=A[m]) return m;}
else if(m==0)
{
if(target<=A[m]) return m;
}
else
{ if(target>A[m-1]) return m;}
if(target>A[m])
f=m+1;
else if(target<A[m])
l=m;
}
}
}
But this solution is by O(logN)
public class Solution {
public int searchInsert(int[] A, int target) {
int f=0;
int l=A.length;
int m;
while(true)
{
m=(f+l)/2;
if(m>0&&m<A.length)
{ if(target>A[m-1]&&target<=A[m]) return m;}
else if(m==0)
{
if(target<=A[m]) return m;
}
else
{ if(target>A[m-1]) return m;}
if(target>A[m])
f=m+1;
else if(target<A[m])
l=m;
}
}
}
Tuesday, January 28, 2014
Leet Code: search in rotated sorted array with duplicate...
Search in Rotated Sorted Array II
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
This one is not difficult to find the trick,
but if you think in a wrong way, you will use a lot of effort to ignore duplicate items..
* If you just think on the index, you don't need recursive...
* In each loop, change l and f in order to find a right region to go on searching...
public class Solution {
public boolean search(int[] A, int target) {
int f=0;
int l=A.length-1;
while(l>=f)
{
int m=(f+l)/2;
if(A[m]==target)
return true;
if(A[f]<A[m])
{
if(target>=A[f]&&target<=A[m])
l=m-1;
else
f=m;
}
else if(A[f]>A[m])
{
if(target>=A[m]&&target<=A[l])
f=m+1;
else
l=m;
}
else
f++; // Trick: A[f]==A[m])
}
return false;
}
}
Subscribe to:
Posts (Atom)