N-sided Regular Polygon

May 8, 2013

I'm working on is to create a program to display an n-sided regular polygon and uses two buttons named +1 and -1 to increase or decrease the size of the polygon. Also enable the the user to increase or decrease the size by clicking the right or left mouse button and by pressing the UP and DOWN arrow keys. So, first off I'm just trying to figure out how to display an n-sided polygon. I have some of the other components started, but I'm just trying to focus on getting this to work.

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Polygon;

[code]....

View Replies


ADVERTISEMENT

N-Sided Regular Polygon Main Method Error

Sep 5, 2014

My code is giving me an error at the main method and it says that modifier 'static' is not allowed in constant variable declarations. every program that i searched for had the same code line but none had the problem i do.

package regularpolygon;
/**
*
* @author home1
*/
import java.lang.Math;
import java.text.*;

[code]...

View Replies View Related

Roll Four Sided Die Between 100 And 1000 Times Depending On Input And Display Answer

Oct 29, 2014

I'm trying to get the program to roll a four sided die between 100 and 1000 times, depending on your input, and then displaying the answer.

public class Lab08
{
public static void main(String[] args)
{
int v1 =0, v2 = 0, v3 = 0, v4 = 0;
int n;
char response;
System.out.print("Enter number of rolls (100-1000): ");
 
[Code] ....

View Replies View Related

Java - Polygon Shrinks Before Rotating

Dec 22, 2014

I have a java program which rotates a rectangular polygon. My program is having a problem. As soon as it starts rotating, it shrinks. The polygon is rotating perfectly though. I am trying but unable to figure out the cause.Main class which has an infinite loop (game loop) to rotate polygon continuously :-Java Code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

[code]....

View Replies View Related

Swing/AWT/SWT :: How To Draw A Diamond Using Polygon

Mar 8, 2015

I'm currently trying to do a diamond using Polygon. I have done this so far. It is not drawing a diamond.

import java.applet.Applet;
import java.awt.Graphics;

public class Diamond extends Applet {

int[] a = {-30, 100 , 30, 100};
int[] b = {100, 40, 100, -40};

public void init()
{
}
public void paint(Graphics g)
{
g.drawPolygon(a, b, 4);
}

View Replies View Related

JavaFX 2.0 :: Point Inside A Polygon

Mar 2, 2015

I have a shape, Polygon (javafx.scene.shape.Polygon) and want to test whether a point (javafx.geometry.point2D) is contained within the shape. (contains doesn't work)
 
I can change the polygon to any type, but I need to be able to create it using the 4 corners points and add it as a child to the Pane.

View Replies View Related

Drawing Polygon On Screen - Collections As Arguments

Aug 29, 2014

I have a method that draws a polygon on the screen:

public void poly(List<Point> points) {
//code
}

For the usage of the method, it makes no difference whether points is an array or a List. In fact, switching doesn't even change any of the code in the method since I use a for-each loop. So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?

View Replies View Related

LibGdx / Box2D - How To Group Tiles Into One Larger Polygon

Jan 19, 2015

I am making a tile based top-down 2D RPG and am using Box2D for the physics. Since my game is tile-based, there are many tiles on each map that cannot be moved through. This results in many small individual Box2D bodies. This is obviously very inefficient and makes the game lag. Therefore I figured that combining the individual tiles' bodies into larger complex groups would be better.

The way I thought of doing it is to, first, group together tiles into groups of tiles that all share 2 vertices with at least one other tile in the group. Then, for each group I do the following. First I get all of the uncommon vertices (these should be the ones on the outside of the polygon). Then I connect all of those vertices and then remove all of the overlapping lines. This should result in only lines on the outside of the polygon. Then I sort the lines so that the first line in the sorted array shares its end point with the second line's start point, etc. Then I remove the doubled vertices and using those remaining vertices (I called them the "true vertices") I create the polygon. I know that Box2D only supports convex polygons, but with Box2DSeparator I should be able to do this, but I first want this method to actually work.

/**
* Attempts to combine the given blocks into larger bodies to improve performance.
* @param bs The created wall blocks.
* @param w The world. Used for body creation.
*/
private void makeLargeBodies(Block[][] bs, World w) {
Array<Tile> tiles = new Array<Tile>();
for(int i = 0; i < bs.length; i++) {

[Code] .....

However, this is where the first problem arises. This method is extremely slow for large maps (like 200x200 tiles). I have worked at this for very long and right now my head can't figure out how to make the loop more efficient...

Now for the next part. After that I attempt to create the bodies for the TileGroups:

//create all the groups' bodies
size = groups.size;
System.out.println(size + " groups");
for(int i = 0; i < size; i++) {
TileGroup g = groups.get(i);
System.out.println("Starting group " + i + ".");
long timeCheck = TimeUtils.millis();
g.createBody(w);
System.out.println("Group 0 took " + (TimeUtils.millis() - timeCheck) + " millis");
}

*Note that the printing is for debugging purposes*...

This method is probably not a problem; it is what is inside createBody(World) that is the issue...

public void createBody(World w) {
Array<Vector2> allVertices = new Array<Vector2>();
Array<Vector2> uncommonVertices = new Array<Vector2>();
System.out.print("Starting search for uncommon vertices. ");
long timeCheck = TimeUtils.millis();

[Code] .....

Most of the code is self-explanatory (with comments). My current issue is where I connect the uncommon vertices (see the printed statements). This method does not actually finish (I have let it run for several minutes and it does not complete). This is likely due to a large number of vertices (often around 3000 in a 60x60 map), but I cannot figure out how to make the loop more efficient... Because of this early failure I don't know if the rest of the method works, both physically and in theory.

All relevant classes (Tile, TileGroup, Line) are below:

private class Tile {
private Vector2[] vertices;
private TileGroup group;
public Tile(int x, int y) {
vertices = new Vector2[4];

[Code] ....

View Replies View Related

Multi-sided Dice - Keep Track Of Total Sum Of All Dice In Array

May 7, 2014

I'm finishing up this assignment, and I'm stuck. These are the last 2 instructions:

. Roll each of the Dice by invoking the roll method on each Dice in the array.
. Keep track of the totals sum of all the dice in the array. Be sure to roll the dice array at least 10000 times.

How to finish it up?

Here's my code so far:

package homework3;

import java.util.Random;
public class Dice {
private int numberShowing;
private final int numberOfSides;
private static final Random randomNumber = new Random();
public Dice() {
numberOfSides = 6;

[Code] ....

View Replies View Related

How To Check Regular Expression

Aug 25, 2014

I have to match pattern like 76XYYXXXX mean x can be 4or 5 and Y can be 6 or 7. All x and y should be same .i.e. 764664444

View Replies View Related

Simple Regular Expression

Jan 17, 2014

I am currently enrolled in my first Java class. I have taken C# in the past so I've messed with some regular expressions, but I am having trouble finding a good website for Java. Is the syntax the same?I want to create a regular expression to only allow the following characters: c C f F [and any 1-3 digit number].

View Replies View Related

Checking SSN Without Regular Expressions

Jun 5, 2012

Prompt user to enter a social security number in the format DDD-DD-DDDD, where D is a digit. Displays "Valid SSN" for a correct ssn, and "Invalid SSN" otherwise.I have it working I am just looking for other ways to solve this with an array maybe or something simpler. I have used if statements here:

public static boolean checkSSN(String social) {
boolean valid = false;
// 9 digits and 2 hyphens. First three characters, 5 and 6, and 8, 9,
// 10, 11 are digits

[code]...

View Replies View Related

How To Use Regular Expression Using Pattern

Mar 16, 2015

I have a string "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle" I need to replace the numbers based on the below condition.
 
if more then 5, replace with many
if less then 5, replace with a few
if it is 1, replace with "only one"
 
below is my code, I am missing the equating part to replace the numbers

private static String REGEX="(d+)";
  private static String INPUT="We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle";
  //String pattern= "(.*)(d+)(.*)";
  private static String REPLACE = "replace with many";
  public static void main(String[] args) {
  // Create a Pattern object

[Code]...

View Replies View Related

Java String Regular Expression

Apr 17, 2014

I have written below regex for two lines.

String LN1Pattern = "^((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))$";
System.out.println("/ABC//FGhiJkl012345".matches(LN1Pattern));
 
String LN2Pattern = "^(|((s+(//[a-zA-Z0-9]{1,33})){1,2}))$";
System.out.println("".matches(LN2Pattern));

s+ is a newline character.

But when I combines both as below, its not giving me expected result.

^(((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))(|((s+(//[a-zA-Z0-9]{1,33})){1,2})))$

For string "/ABC//FGhiJkl012345

//abCD01EF02" - returns False. Expected is True

I think there is some problem in lookahead placed.

View Replies View Related

How To Use Regular Expressions For Integer Input

Jul 21, 2014

I have a program in which I take some characters input from User

using
String inputString=in.next();

How can i restrict user that input should be only char 0 or 1?

If the answer is regular expressions then What Should be Its Regular expressions?

View Replies View Related

Performing Regular Expression - Valid Last Name

Feb 4, 2015

I have some problems about performing regular expression. In the following code, the output as a result is being "valid last name". But in my opinion, it should be "invalid last name". Because "Doe" has not any space, apostroph or any hypen. Look at the regular expression statement: "[a-zA-Z]+([ '-][a-zA-Z]+)*"
 
package deneme; 
public class Deneme {
public static void main(String[] args) {
String lastName = "Mc-Something"; // Mc-Something 
if(!lastName.matches("[a-zA-Z]+([ '-][a-zA-Z]+)*"))
System.out.println("Invalid last name");
else
System.out.println("Valid last name");
}
}

I know that the asterisk qualifier means character(s) in any number.(Is this wrong?) So for "Mc-Something", 'M' corresponds to [a-zA-Z], 'c' corresponds to +, - corresponds to [ '-], 'S' corresponds to [a-zA-Z], "o" corresponds to +, and finally "mething" corresponds to *, doesn't they? But for "Doe", there is no any space, apostroph or hypen.

View Replies View Related

Inserting Into A Regular Binary Tree?

Nov 26, 2014

I know that for a binary search tree the insert() method is easy. You just kept comparing the values of the nodes, and you would know whether to go left or right depending on the value of the node.However, I am stumped when trying to figure out how to add a node into a normal binary tree where you just keep adding the nodes to the leftmost side, regardless of value.Here is my attempt at the insert() method:

public class RegularTree extends BinaryTree {
public CompleteTree() {
super();
}

public void insert(Comparable item) {
if(this.isEmpty()) {
this.setRoot(new TreeNode(item));
return;

[code]....

View Replies View Related

Regular Expressions For Range Statements

Apr 1, 2014

I have the following Output

_G7120+1#=K,

_G7132+_G7133#=_G7120,

_G7144+_G7145#=_G7132,

_G7156+_G7157#=_G7144,

_G7168*Z#=_G7156,

_G7180*Z#=_G7168,

_G7192*Z#=_G7180,

_G7204*Y#=_G7192,

_G7192, in 10..15 / 16

X*Y#=_G7204,

X+Y#=_G7133,

_G7145+X#=Z1_a,

Y in 1..15,

Z/Y#=_G7157,

__X in 1..15 / 17 / 20.

From this, I need to extract the statements of variables that do not start with _G . I mean, I need to extract, Y in 1..15 , __X in 1..15 /17/20 but not _G7145 in 10..15 / 16.

I am using regular Expression for this as [^_G]^[A-Za-z0-9_]+ in|ins [-9 -9]..[-9-9] [/[-9-9]..[-9-9]]+

View Replies View Related

Area Of Regular Pentagon - End Result

Jul 17, 2014

I am doing exercises which has to do with the area of regular pentagon.

(Geometry: area of a regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is :

I have checked errata on their page and they did not list the type-o under the final result.

Errata link [URL] ....

their result is
Enter the number of sides: 5
Enter the side: 6.5
The area of the polygon is 74.69017017488385

and mine is Area of regular pentagon is 72.69017017488385

is it a code error?

import java.util.Scanner;

public class AreaOfAregularPolygon4_5 {
public static void main (String[] args){
//Initiate scanner and use the input
Scanner input = new Scanner (System.in);
System.out.println(" Enter the number of sides in polygon");

[Code] ....

View Replies View Related

Regular Expression In Java Which Allow Only Numbers

Nov 28, 2013

I need a regular expression in java for phone number which that does not allow any character special character only numbers should be allowed.. My sample program is
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class c {
  public String removeOrReplacePhoneNumber(String input) {
  if (null == input)
  return input;

[Code] ....
 
but this is allowing characters like a,b,c..

View Replies View Related

I/O / Streams :: Parsing Logs Using Regular Expression

Dec 1, 2014

I am new to java coding. so, reading the log file using java code.

1) I wish to parse a file, and find a value for variable e.g. [StorageVersion].
2) After finding the value, it will be pushed to database.

Following is the code that i have written till now.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.regex.*;

[code]....

1) I don't know what regular expression can i use.
2) I want a line to check that preceding line should have //StorageVersion .
3) n then i will check any string between := K[' and '];. From above log file "'706146.0.22106932'" is the value that i am looking for.

View Replies View Related

How To Split A Text Looking For Values With Regular Expression

Aug 6, 2014

I have to read a text looking for value related to certain variable. The variable are always DE plus identifier from 1 to 99 or DE plus identifier from 1 to 99 and SF plus identifier from 1 to 99. The value can be alphanumeric. How can I get these values? As a start point, I try to use split("DE") but I can get something that is wrong if there is "DE" inside the text that doesn't me interest. I look for scanner but it doesn't work. I guess that there is some way, maybe using regex but I am completely lost ( I have never used regedix before and I am in rush to fix this).

Basically, the text is similar to the below where DE means data element and sf sub field. Some data elements have sub fields while others don't. I guess that there is a way to split with something like DE+anyNumberFrom1To99 = theValueAimed in some array and DE+anyNumberFrom1To99+,+SF+anyNumberFrom1To99 = theValueAimed in other array.

DE 2, SF 1 = 00 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 1 = 0 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 4 = 1 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 5 = 0 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 6 = 11 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 7 = 90x SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 7 = 12ab SOME TEXT THAT DOESN'T INTEREST ME
DE 99 = 1234 SOME TEXT THAT DOESN'T INTEREST ME

View Replies View Related

Regular Expression In Java For HTML Pages

Feb 18, 2014

I need to parse an html web page to extract specific information from the tags in Java. For example,

<b>Species </b> Strain </td>

I need to look for the Strain info (Strain is variable in length) in the page. The whole web page is stored as a huge string. I need a regular expression that can identify all the Species and retrieve their corresponding strain info. how to do this or can propose some clever string manipulation methods in Java.

View Replies View Related

Specific Regular Expression - Split A String

Mar 3, 2014

Regular expression which I want to use to split a string. The string could look similar to this:

"a = "Hello World" b = "Some other String" c = "test""

The String is read from a file where the file contents would be:

a = "Hello World" b = "Some other String" c = "test"

After splitting I would like to get the following array:

String[] splitString = new String[] {"a", "=", ""Hello World"", "b", "=", ""Some other String"", "c", "=", ""test""}

I know I could just write a simple text parser to go over the string character by character, but I would rather not do it to keep my code clean and simple. No need to reinvent the wheel.

However, I just cant seem to be able to find the right regular expression for this task. I know that a RE must exist because this can be solved by a finite automaton.

View Replies View Related

Validate IP Address With Java Regular Expression

Jan 23, 2015

I am trying to implement an example (Book* : Java SE 7 ..By S G Ganesh) for validating an IP address but it fails to validate a valid IP addresses. I found another example on the internet(**) and it works super fine, no problem at all. I edited the code (the one I got from internet) into the exact format like book and it still works super but i don't understand why the books' example doesn't work though both look exactly the same now ,further more, how can i compare String x and y for equality?

public class TestClass {
void validateIP(String ipStr) {
String one = "((25[0-5]|2[0-4]d|[01]?dd?)(.)){3}(25[0-5]|2[0-4]d|[01]?dd?)"; //copied from internet and edited
String two = "((25[0–5]|2[0–4]d|[01]?dd?)(.)){3}(25[0–5]|2[0–4]d|[01]?dd?)"; // copied from book
String x = "((25[0-5]|2[0-4]d|[01]?dd?)(.))";
String y ="((25[0–5]|2[0–4]d|[01]?dd?)(.))";

[Code] ....

View Replies View Related

Java Servlet :: String Regular Expression

Mar 7, 2014

Do not want to use loop and  Character.isLetter to validation string , not sure at how to using regular expression?

If I want  8 characters string length, the first 3 is letter, the remind character is number ...

^[a-z0-9_-]{8}$
^[a-zA-Z]{3} + [/d]{5} $  ??

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved