I've done basic programming in java using Netbeans IDE. So I've good foundational knowledge in java programming. Now I looking forward to draw shapes in java and eventually learn to make simulations. Now I want to learn to create geometric shapes like circle triangle, rectangle etc. I don't know in which Jcomponent(s) should I set the drawing of the shapes, the codes, the libraries and classes I need to import.
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes:
• circles -- area = π*radius^2 (format the answer to have two decimal places) • rectangles -- area = width * length • trapezoid -- area = (base1 + base2) * height/2
Because the three methods are to be overloaded, they should each have the same name , but different parameters (for example, the method to be used with circles should only take one parameter , the radius of the circle).
Demonstrate the methods in a program that prints out the following areas, each on a separate line:
• the area of a circle with radius 3 • the area of a rectangle with length 2 and width 4 • the area of a trapezoid with base lengths 3 and 5 and height 5
SAMPLE RUN #1 --- Prompts For Keyboard/Console/Standard Input --- Inputs Outputs --- Monitor/Console/Standard Output --- 28.27 8.0 17.5
What The Console Looks Like In An Interactive Session: (Note: this combines standard input with standard output )
>java Area 28.27 8.0 17.5
THIS IS MY CODE
import java.util.*; public class Area { public static double area(double radius) { return (double)(Math.PI*Math.pow(radius,2)); } public static int area(int length, int breadth)
I have a problem which can't solve it for 2 days search. I need to draw 3d shapes , i downloaded all packages and set an example code and run it ...
Exception in thread "main" java.lang.UnsatisfiedLinkError: no J3D in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java :1886) at java.lang.Runtime.loadLibrary0(Runtime.java:849) at java.lang.System.loadLibrary(System.java:1088)
I have to create a method with the following header :
public static <E extends Comparable<E> > void sort ( ArrayList<E> list , int left, int right)
i also had to create a swap cells method and position of max integer method. and also had to read the preserved data file in with a scanner. I implemented the comparable interface I am having difficulty sorting my list by the area. It has to be in descending order.
Geometric Object class: since it has comparator also am interested if i need to change this?
CODE:
Driver: public static void main(String[] args) throws IOException, ClassNotFoundException { Circle c1 = new Circle (4, "red", false); Circle c2 = new Circle (2, "blue", true); Circle c3 = new Circle (10, "blue", true); Rectangle r1 = new Rectangle (10, 6, "yellow", true); Rectangle r2 = new Rectangle ( 5, 11, "green", true); ArrayList <GeometricObject> list = new ArrayList();
I made a tiny app that saves notes to a folder within the classes directory where I created the .jar file.Let's say I wanted to add this to Java Web Start. Would I be able to transfer the jar file within a folder(s) or would I have to change the program so that It creates a new folder to save texts In automatically ? Basically , can jar files be transferred with other files/folders ?
I was developed a simple GUI, in that it requires modification of jtextfield content while JComboBox item selected and vice-versa. I was used itemListener on JComboBox and Document Listener on JTextField. It was gives exception while running the code. Because one listener source effected by another one..
Exceptions are like this:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification at javax.swing.text.AbstractDocument.writeLock(Unknown Source) at javax.swing.text.AbstractDocument.replace(Unknown Source) at javax.swing.text.JTextComponent.setText(Unknown Source) at ronanki.swing.pcahostsimUI$5.itemStateChanged(pcahostsimUI.java:368) at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
I developed an application that is accessing some property files. The condition was that the user should be able to modify the content or parameters of those property file.How can I distribute the application using Java web start that also includes those property file in the client side?
This is a program to input a sentence and print the words that start with a vowel.......There is no syntax error but on executing and typing the sentence and pressing enter,I get a error saying :
java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at sentence.main(sentence.java:22)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Game extends JPanel implements ActionListener, KeyListener{ Timer t = new Timer(5,this); double x = 0, y = 0, velx = 0, vely = 0;
[Code] .....
So why doesn't it work and what about the second dot.... ?
I am trying to get random shapes to generate automatically. I was able to get just three shapes to generate but nothing random. When I added the random code now only the frame shows up and no shapes. I also keep getting an error with frame.setVisible(true). It says identifier expected. Here is what I have so far:
Main program:
import javax.swing.*; import java.awt.*; import java.util.Random; public class ShapeGen { public static void main(String [] args) { //Create window and set title draw panel = new Draw();
[Code] ....
This is the draw program to generate random shapes:
import javax.swing.*; import java.awt.*; import java.util.Random; public class draw extends JPanel { public draw(Color backColor) { setBackground(backColor);
I am having issues with drawing shapes from bottom right to top left.
Issue:
- g.drawRect() will show like I am calling g.fillRect() - other shapes will not even show the shape in that area
Needs:
- g.drawSHAPE needs to show and not be filled unless I have my fill checkbox selected
The Program:
- Create a JFrame with a draw panel and a component panel - have a combobox with shapes that, when selected, will draw that shape in the draw panel - have a button that, when clicked, will launch JColorChooser to change the color of the drawn shape (draw panel is set to black) - have a checkbox that, when checked, fills the shape - have mouse listeners to adjust X and Y and will instantly update the shapes size to where you drag/click/press/release
Code for my drawRect():
Java Code:
// if statement to check if mouse drag X is less than starting X if(x2 <= x){ if(emptyORfill.isSelected()) // emptyORfill is my JCheckBox g.fillRect(x2, y, x-x2, y2-y); // x-x2 is the same as Math.abs(x2-x) else g.drawRect(x2, y, x-x2, y2-y);
[Code] .....
This is just for my Rectangle. This will show a filled rectangle when both mouse drag X and Y are less then the starting X and Y. If I take this fully functional code and adapt it to drawRoundRect(), the round rectangle wont even show the shape when mouse drag X and Y are less than the starting X and Y but will be fine if one or the other is less than the starting X or Y. NOTE: This same exact code worked on my classmates laptop in her program, but in my program on her laptop it did not. She took out the "else" in the else if's and just made them if statements all the way down and it worked on her laptop in my program, but the same "fix" did not work on my pc.
My mouse listener just sets X and Y values in my Shape class that updates my shape methods. I have an item listener for my comboBox that sets default values when a new selection is made and enables/disables editable on my fill checkbox for certain shapes. My action listener looks for the button click and the checkBox click.
I'm trying to make a simple program that will feature some graphics. It will have a JCombobox that the user selects to draw various shapes. I can't get the repaint function to work however.
Is there some way to create compound shapes/paths in JavaFX?
For the record I'm not implying the use of the methods intersect, subtract, or union. These produce other shapes. A compound shape/path is one that has a knockout of some sort. For instance, a circle within a circle, such as in a 2d donut shape. Alternatives do not include a circle with a thick stroke nor an overlayed circle with the background color. Specifically, JavaFX supports the FillRule, in the case of the Path object. However, there doesn't appear to be an "add" method as there was in the Area shape in Swing.
How to make shapes move at same time when user enter up it should move up. It works for rectangle but i don't know how to move the others like that. (btw i am new to java). How can i do that?
I have a problem with a shape generator. It randoms the shapes just fine, I just do not know how to make all the shapes fully stay within the frames border.
RandomShapeMaker.java
import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import java.util.Random; public class RandomShapeMaker extends JPanel{ private static final Random randomShape = new Random(); public void paintComponent( Graphics g ){ super.paintComponent( g );
I am currently writing a small drawing program and I am having trouble with changing the size of the shapes. To do this, I have to access the arraylist shapes, check whether pressedX/pressedY is on any of the shapes in the arraylist using the findShape() method and then when released, uses moveBy() in the Rectangle/Oval/Line class and moveShape() in the miniDraw class to move the shape and draw it in the newreleasedX/releasedY position.
So far I think I have pin pointed the problem to being the method in all the shapes classes, that checks whether the pressedX/pressedY which is the on() method, and the findShape() method in the miniDraw class.
how to create a tree data structure in java.I tried with a class consisting of node field and arraylist child nodes. but it does not satisfy the requirement.the requirement is that,
I'm getting a DOMException, "HIERARCHY_REQUEST_ERR". I know that the problem is from the following code towards the bottom in my function, but I don't know how to deal with it. When I get rid of doc.appendChild(staff);, I get rid of the problem, but it obviously doesn't add the new entry to my root element.
Element staff = doc.createElement("Staff"); doc.appendChild(staff);
I want to create a TASK MANAGER for windows OS using java. Of course i am not expecting any code snippets, that is, what are the classes to be included and moreover HOW to do it.
Anyway I am creating a game for my A2 coursework, most commonly known as Checkers. I have completed my code and everything works as I had planned except that the CheckerBoard itself as well as the checkerpieces do not appear to be showing.
The section of were my board should be present is just a black space. Although my board does not appear to be displaying, all of the actions I perform on it such as clicking certain section produces the planned response, and although I've checked through my code I cannot work out what I've done wrong.
CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard application.pack(); // Use preferred size of content to set size of application. Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); application.setLocation( (screensize.width - application.getWidth())/2, (screensize.height - application.getHeight())/2 );
[Code] ....
I have noticed is that within the BoardComplete(), if you change the setBackground(Color.BLACK) to WHITE, it will change the colour of the section to white, but the checkboard is still unshown.
I'm finding common elements in a collection of arrays. This is my code so far.
import java.util.ArrayList; import java.util.List; public class CommonElements //< T extends Comparable<T> > { Comparable[] comparable; Comparable[] tempArr; Comparable[] tempArr1; Comparable[] newArray = new Comparable[tempArr.length]; int comparisonCount;
[code]....
I'm using this method to sort through my array. The method accepts a collection of arrays (of varying length and of any type) as input, the algorithm input should be no greater than n(k-1). I know I can solve it using a quadratic algorithm, but it won't meet the requirement of the assignment. I did have an idea of storing all my algorithm in one giant array. After storing as one giant array I was going to sort it. After sorting I was going to compare each array side by side and see if they are the same. Here is note my teacher provided.
Note About Testing You will need to develop several sets of test collections for testing your algorithm. The grading rubric mentions covering the case where all the test collections have the same length, as well as covering the case where the test collections are of different lengths. You will also need to think about what constitutes the worst case scenario for this algorithm, since only that scenario will make your analysis of total comparisons performed a meaningful one. You can use the formulas in the grading rubric to tell you how many comparisons you should expect in the quadratic and linear cases. For example, if you have 5 total collections (1 query collection and 4 test collections), each of which contains 10 elements, the total number of comparisons performed in the worst case should be: (k - 1)N2, which for k = 10 and N = 10 is: (5 - 1)102, or 400 comparisons. For the linear algorithm, you should only have N*(k - 1), which is 10*(5 - 1), or 40 comparisons.
Here is Q/A that my teacher provided:
1. Are the elements in the collections already sorted?The input collections may or may not be sorted. Don’t assume that they are sorted.
2. Can I sort the elements in the collections? Am I supposed to sort the elements in thecollections?Yes, you can sort the elements in the collections. It is not required that you do so, however..
3. If I sort the collections, should I count the element comparisons that are performed by the sorting algorithm?No. The only element comparisons you should count are the ones that are directly used to find the common elements. Ignore comparisons performed by any sorting algorithms you use.
4. How do I extract an individual collection from the Object[] collections argument of the findCommonElements method?You will need to typecast each collection as type Comparable[]. For example:Comparable[] currentCollection = (Comparable[])collections[i];
5. Why are we using a one-dimensional Object array instead of a two-dimensional array of type Comparable (e.g., Comparable[][])?In Java, arrays are treated as objects, so only a variable of type Object[] can contain arrays. A variable of type Comparable[] can only contain Comparable elements, not arrays. A variable of type Comparable[][] also can only store Comparable elements. Although it is possible to organize the elements such that all the elements at indexes [1][i], for example, are considered to comprise a collection, this is not the same as having an array that contains other arrays. The additional bookkeeping is required to keep a 2D array organized is more complex than simply performing the typecast described above.
6. Can I use other data structures besides arrays; e.g., HashMaps?No. Although it is easier from a programming perspective to use higher order data structures like HashMaps to find common elements, using these structures simply hides the details. If you were to examine the implementations of those structures, you would probably find that they use relatively inefficient algorithms such as sequential searches to find elements. Additionally, if you use library components, you have no way of counting the number of comparisons performed, since you don’t have access to the source code of those components.
7. Is this a trick question? I can’t think of any way to get below NlogN comparisons.It is not a trick question. It is possible to solve this problem using a number of comparisons proportional to (k – 1) * N.