I have recently designed a bit of code for a school assignment that actually draws 3 houses, each one with a specified height and width. It is based on an OOP structure. So here is the house.java code:
Code:
//House: Assignment P4.9 by Brandon Milton
/*
*
*
* Written by brandonio21
* http://brandonsoft.com
* http://bpforums.info
*
* Use as you'd like!
*/
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D.Double;
public class house extends Applet {
int height;
int width;
int x;
int y;
public house(int theight, int twidth, int tx, int ty){
height = theight;
width = twidth;
x = tx;
y = ty;
}
public void draw(Graphics2D g2){
Rectangle2D.Double base = new Rectangle2D.Double(x,y,width,height); //Create the square that will be the bottom of the house
int doorHeight = 0;
if (height < 50){
doorHeight = height;
} else{
doorHeight = 50;
}
//^ Size the door to the house if it is small
Rectangle2D.Double door = new Rectangle2D.Double(x + 20,y + (height - 50),20,doorHeight); //Create the door
Rectangle2D.Double window = new Rectangle2D.Double(x + 50,(y + height)-60,20,20); //Create the window
Line2D.Double leftRoof = new Line2D.Double(x,y,x + (width/2),y- (height/2)); //Create the left side of the roof
Line2D.Double rightRoof = new Line2D.Double(x + (width/2),(y - (height/2)),x+width,y); //Create the right side of the roof
g2.draw(base);
g2.draw(door);
g2.draw(window);
g2.draw(leftRoof);
g2.draw(rightRoof);
//Draw them all!
}
}
And here is the code for the tester class.. This is just an example, you can recode this to your needs if you are using it!
Code:
//Assignment P4.9 by Brandon Milton
/*
*
*
* Coded by brandonio21
* http://brandonsoft.com
* http://bpforums.info
*
* Feel free to use wherever you'd like!
*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.applet.Applet;
import javax.swing.JOptionPane;
public class houseTester extends Applet {
int width1 = Integer.parseInt(JOptionPane.showInputDialog("Width of house1")); //Creates an input dialog
int width2 = Integer.parseInt(JOptionPane.showInputDialog("Width of house2"));//Creates an input dialog
int width3 = Integer.parseInt(JOptionPane.showInputDialog("Width of house3"));//Creates an input dialog
int height1 = Integer.parseInt(JOptionPane.showInputDialog("What is the height of house1"));//Creates an input dialog
int height2 = Integer.parseInt(JOptionPane.showInputDialog("What is the height of house2"));//Creates an input dialog
int height3 = Integer.parseInt(JOptionPane.showInputDialog("What is the height of house3"));//Creates an input dialog
int x = Math.max(height1, height2);
int maxheight = Math.max(x, height3);
//Finds the tallest house height
public void paint(Graphics g){
setSize(width1+width2+width3 + 25, (maxheight * 5)); //Sets the window size to give 25px of padding on the right, and a large amount of vertical padding
Graphics2D g2 = (Graphics2D)g;
house hs = new house(height1,width1,0,(0 + (height1*2))); //Draw house with user specs.
hs.draw(g2);
house hs2 = new house(height2,width2,width1 + 5,0 + (height2*2));//Draw house with user specs.
hs2.draw(g2);
house hs3 = new house(height3,width3,0 + (width1 + width2) + 5,0 + (height3*2));//Draw house with user specs.
hs3.draw(g2);
}
}
Here is the output of the already implemented code(With 100 set for every value):
This was actually a school assignment, but I thought that I'd share my code with you:
Code:
//Exercise P4.6 - by Brandon Milton
/*
* This bullseye code is made by Brandon Milton, known as brandonio21 on BP Forums
* This exercise is directly from Cay Horstman's: Computing Concepts with Java Essentials
*
* Commenting is for better understanding
*
* Feel free to use
* http://brandonsoft.com
* http://bpforums.info
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.applet.Applet;
import java.awt.geom.Ellipse2D;
public class bullsEye extends Applet { //Tell Java that this is indeed an applet
public void paint(Graphics g){ //This is what java needs to actually draw something
Graphics2D g2 = (Graphics2D)g; //Tell java to use the newer graphics2d
int rings = 8; //THIS CAN BE EDITED - This is the number of rings that the bullseye itself has. default is '8'
int i = rings*50; //multiply the number of rings by 50, to get the largest rings width
while (i > 0){ //As long as the width is not 0,
Ellipse2D.Double elip = new Ellipse2D.Double(400 - (i/2),400 - (i/2),i,i); //Create a new circle, it's position is in the center of the previous ring, and it's size is the previous rings - 50
if ((i/50 %2) == 0){ //If it is an even ring
g2.setColor(Color.red);
g2.draw(elip);
g2.fill(elip);
} else{ //It is an odd ring
g2.setColor(Color.white);
g2.draw(elip);
g2.fill(elip);
}
i = i -50; //Decrease the size, and draw another ring
}
}
}
This code draws a bullseye to the screen at the location of 400,400, and grows outward. The number of rings can be edited.
Ever since I started making Java tutorials, tons of people have sent me private messages on YouTube asking which language to learn, Visual Basic .net, or Java.
Well, each language has its own perks: vb.net is more secure, and easier to learn. Plus, it is really easy to design the interface of an application using Visual Studio.
Java, however, is multiplatform, (Meaning it can run on any Operating system), and is relatively difficult to learn. Plus, interfaces require coding knowledge.
Overall, it is really about preference. More companies tend to hire developers who know Java. However, knowing both gets you extra points!!
Here is an image to help anyone that is confused:
[attachment=0]<!-- ia0 -->vbnetvsjava.png<!-- ia0 -->[/attachment]
I recently did a java tutorial on integer arrays, so I thought that I'd post the vb.net version of the code as well!
Code:
Public Sub Execute()
Dim arrInt(5) As Integer 'Creates an integer array with 5 slots
arrInt(0) = 1 'Assigns a value of 1 to the first slot in the array
Dim i As Integer = 0 'Creates an integer, i, and gives the initial value of 0
Do While i < 5 'While i is 0-4
arrInt(i) = i + 1 'Tell the corresponding slot in the array to be equal to i+1, so if i was 3, arrInt(3) would be set to 4
i = i + 1 'Increase the value of i by 1
Loop
MsgBox(arrInt(0).ToString + " " + arrInt(1).ToString + " " + arrInt(2).ToString + " " + arrInt(3).ToString + " " + arrInt(4).ToString) 'Convert all the array values to strings, and display them in a messagebox
End Sub
After posting this little code snippet, I realized that I never really created a video explaining Arrays in vb.net. Since arrays are extremely useful, I may be doing a tutorial on this soon!
I recently made a video tutorial on integer arrays, and some people may just want to copy/paste the code for their own purposes, so here it is, fully commented!
[code2=java]public class arrayTutorial { //Create the class
public static void main(String[] args){ //Create the main method (This is what gets executed)
int arrInt[] = new int[5]; //Create a new integer array with 5 slots
//Note: Indices start from 0
arrInt[0] = 1; //Tell the first slot (index) in the array to have a value of 1
int i = 1; //Give i a value of 1
while (i <= 4){ //If i is less than 5
arrInt[i] = i+1; //Assign the corresponding slot of the array (i) a value of 1 + i; so if i is 2, the 3rd slot of the array would be filled with 2+1, which is 3.
i++; //Increase the value of i by one
}
System.out.println(arrInt[0]+ " " + arrInt[1] + " " + arrInt[2]+ " " + arrInt[3] + " " + arrInt[4]); //Output all the slots in the array in string form (seperated by spaces)
}
}[/code2]
Basically, what I am doing here, is creating a new integer array with 5 slots, using a while statement to fill the 5 slots, and displaying each slot, separated by a space.
Hopefully this helps anyone actually doing Java (As I know this forum is mostly vb.net based)
Well, this weekend I was browsing the forum and realized that it is simply full of inactivity.
Right now, I am focusing on completing several personal projects, doing a project for a client, and learning Java.. While at the same time playing Battlefield 3 - So I have had barely any time to post things on the forum.
But, for the sake of people, I will try to post on the forums more often! <!-- s --><img src="{SMILIES_PATH}/icon_razz.gif" alt="" title="Razz" /><!-- s -->
Hello, here I am asking for help again <!-- s --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="" title="Very Happy" /><!-- s -->. So, what I want is to know if there's a way to make a programm like a PHP editor, I want my application to "read" the code and change some keywords like "for", "while" to a specified color.
Allthough i havnt come up with a proper name for it yet
i felt like i should show you guys some videos of my progress so far
The idea behind the program is for us at Alyus can use it for our current projects and have an easy to share progress/Description/Imaging with eachother
I have 2 videos and it says itself the second video is the newest <!-- s --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="" title="Smile" /><!-- s -->