Swing/AWT/SWT :: Filling A Rectangle With Double Precision
Mar 4, 2014
I have run into a bit of a head scratcher, at least to me. I am building multiple rectangles using double precision. I had to switch from int to double due to another issue that requires decimal places. Now, my fillRect (last line in the code section I posted) is causing an error as it only wants to work with int.
public void draw(Graphics2D g2) {
// check that sizes are above 0
if ((rectWidth <= 0) || (rectHeight <= 0))
[Code]....
View Replies
ADVERTISEMENT
Dec 31, 2014
What is the simplest way to sum a huge number of double precision numbers so the sum has higher precision than double?
View Replies
View Related
Feb 5, 2015
I have a problem with the size of my scrollPane. It won't fill the space in the JPanel.
is there a way to let the scrollPane fill the whole space?
View Replies
View Related
Apr 13, 2014
Is there a way to simply slap a rectangle into a JPanel (make it appear) with out creating an inner Class or helper Method, all within "Main"? And if not, why?
Making a JFrame is easy.
Adding a JPanel is a snap.
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TheJFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
[code]....
View Replies
View Related
Mar 2, 2014
I'm trying to build a program which sets a fill color for a rectangle. Each time the mouse is clicked the fill color changes according to the position of the mouse's X and Y. I succeeded to create a JFrame with a changing background according to the Mouse's position, but I can't seem to change the color of the rectangle (defined in MyPanel class).
This is the current code,
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
[Code]...
View Replies
View Related
May 29, 2014
My latest issue was that I wanted to only get a double value into a JTable Cell.
So first I Overrode the getCellEditor(int row,int col) method of my JTable, so if I was in a cell i knew I needed a double value I returned my Editor.
My editor filters out anything but numbers. I also check to make sure we get one and only one decimal point.
Other solutions Ive seen use keyboard Listeners, but to me that doesn't work. What if the user cuts and pastes data? My solution handles that as well.
It doesn't handle if they try to cut and paste something like 1234abcd.bob.123.23 , this would result in 1234..123.23 pasted in. I'll handle faulty values on the getValue method of my AbstractTable, but as long as the user is using mostly valid data this works well.
public TableCellEditor getCellEditor(int row, int col) {
if (row==TheRowWant && col==TheColumnI want) {
return (new DefaultCellEditor(createDoubleTextField()));
}
}
private JTextField createDoubleTextField() {
final JTextField field = new JTextField();
[Code] .....
View Replies
View Related
Feb 19, 2014
In the book we made a GUI which has 2 buttons, a label and a panel (the panel is a subclass of JPanel). What the program does is the following: When I press one of the two buttons, in the panel there is a rectangle that changes its color. When I press the other button it has to change the text on the label. Now the problem is that when I start the program, the FIRST time I press the button on which the label must change, this also changes the color in the rectangle, which it should not (I also noticed that when i FIRST click the rectangle shifts a little bit to the left). After that the program works fine.
Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoButtons{
JFrame fr;
JButton labelButton;
JButton colorButton;
JLabel label;
[Code] ....
View Replies
View Related
Mar 26, 2014
I am struggling to make a double border which has rounded corners. The border should be 2 parallel lines.
I made something which looks almost good, but if you look closer there are a few wrong things.To achieve this, I made 2 paths, then painted them in 2 different colours.
The main problem is painting with AA, because the colours overlap and are affected.
I think it would be smoother to paint a single path using 2 colours.
For the first problem, I think some kind of a stroke would be enough. But, if I build a stroke, it's almost same thing like drawing the path twice, like I did before. Anyway i think that would fix AA problem. But still, maybe I can achieve a smoother effect for the corners.
Question:Maybe something with a 2x1 Texture ? It's possible to achieve this ? I know I tried this before and when a curve was encountered the direction of the texture was wrong and looked messy.
PS: Ignore the blueish thing, it's a dragged component to activate the highlight border.
View Replies
View Related
Feb 9, 2014
I've been working my way through a tutorial to build a simple 2D tile-based engine that also allows the code to manipulate the colour of pixels on the screen based on a monochrome map read from a file. I've got the code right line for line, but I seem to have a bug when it comes to draw to the screen! At regular intervals, despite what is held in the pixel array of ints (which I have confirmed to be correct when debugging), I get the same row of pixels being draw twice in a row. I figured there was some loop issue somewhere, but after attempting to debug this for some time, I haven't come up with the answer.
Original tilesheet:As rendered:
Game.java:
package igg.engine.game;
import igg.engine.game.gfx.Screen;
import igg.engine.game.gfx.SpriteSheet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
[Code] ....
View Replies
View Related
Mar 15, 2015
I am having trouble conceptualizing how to fill arrays. I understand that I can declare one and number it's elements, and it stores values of a certain data types. I don't really understand beyond that. Like how I would fill and use data in it.
I can be more specific with an assignment I have for school, but need to get started understanding how to fill arrays with data and then how to use that data.
View Replies
View Related
Jul 24, 2014
i have a row of int JTextfields and i want to get a running total of the sum of all boxes in the last total JTextfield, as the user types.I understand its an actionlistener but whats the best way of doing it. What the action to listen out for?
View Replies
View Related
Dec 13, 2014
I am just starting to use ArrayLists for the first time. I was wondering if there is a way to automatically fill ArrayLists with a for loop.
For example:
ArrayList<Integer> numberList = new ArrayList<Integer>();
int limit;
Integer starter = new Integer(1);
[Code].....
I need to make an ArrayList with numbers 1-limit, inclusive, and rather than doing this with a regular array I am doing it with an ArrayList because I will need the remove() method later. Anyway, my question was if there is a way to fill an ArrayList with a for loop. My code doesn't work at the moment, giving this error: Array type expected; found: 'java.util.ArrayList<java.lang.Integer>'
View Replies
View Related
Feb 10, 2015
I'm stuck trying to figure out why this isn't compiling.
Bumper.java:87: error: possible loss of precision
myX = (Math.random()* (rightEdge-myXWidth) + myXWidth / 2);
^
required: int
found: double
Bumper.java:88: error: possible loss of precision
myY = (Math.random()* (bottomEdge-myYWidth) + myYWidth / 2);
^
required: int
found: double
View Replies
View Related
Oct 19, 2014
So I an assignment in Java to write a code which will randomly populate squares in a Tic Tac Toe Board. I pretty much have it I think, but I cannot get the 'O' to appear on the board, some squares will be blank. We were told to use the Random utility to generate the squares. I am attaching the .gif's which are used. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class LandryTicTacToe extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -8781512780135301721L;
private final int HEIGHT = 450;//Set value for Height
private final int WIDTH = 500;//Set value for Width
private static JButton [] button = new JButton[9];//Declare array of Buttons
[Code] .....
[attachment=37084:TicTac0.gif][attachment=37085:TicTacX.gif]
Instructions Given to me:
Display a frame that contains nine labels, arranged like a Tic Tac Toe board. A label may display an image icon for X, an image icon for O, or nothing. Display images randomly in each label. Use the Random class to generate 0, 1, or 2, which corresponds to displaying an X image, an O image, or nothing.
Attached image(s)
View Replies
View Related
Jan 26, 2014
Ok, so in my quest to achive perfect ray casting and line/plane intersection, what I believe to be my last problem is precision of a float. I need some precision (0.000) but I am dealing with the difference of (-1.000000000) and (-1.000000002), where the second number would be completely off. I looked at DecimalFormat, but that just puts it into a string, I need actual loss of precision.
Java Code:
DecimalFormat form = new DecimalFormat("0.00");
String newX = form.format(x);
String newY = form.format(y);
String newZ = form.format(z);
x = Float.parseFloat(newX);
y = Float.parseFloat(newY);
z = Float.parseFloat(newZ); mh_sh_highlight_all('java');
View Replies
View Related
Aug 9, 2014
I realized that I was not filling the array with anything but now I'm having issues filling it correctly. It's only filling the array with three of the user inputs ....
import java.util.Scanner;
public class DriverSort {
public static void main(String[] args) {
Scanner scan =new Scanner(System.in);
Sorter sorter = new Sorter();
int choice; // variable which says which sorting algorithm to use
[Code] .....
View Replies
View Related
Feb 5, 2015
public class Access
{
public static void main(String args[])
{
long a=2;
int j[]=new int[a];
}
}
Code shows error as "possible loss of precision" as am working with long type.but need to have my array size as 10^10 or some other logic?
View Replies
View Related
Mar 26, 2015
I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...
Here's my code:
package sqrroot;
public class SqrRoot {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double sroot, rerr;
int count = 0;
for(double num = 1.0; num <= 100.0; num++){
[Code] ....
and here is the output:
run:
0.0
1.0 is a perfect square.
0.0
4.0 is a perfect square.
[Code] ....
There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)
Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?
View Replies
View Related
Jun 6, 2014
I am trying to create an array filled with the object Card. It keep throwing the exception that the "deck is empty". I am not sure why that's happening because the code for filling the array seems fine.
public class Deck {
private Card[] deck;
private CardPile cardPile;
private int numCards;
public Deck() throws InvalidDataException{
this.deck = new Card[52];
[Code] .....
View Replies
View Related
May 14, 2014
I have code for precision and recall testing for search engines. I am trying to run it in Newbeans 8.0, but I am getting the common error in UnsupportedOperationException
I have tried making files and directories as the given in the code file. I am attaching the two files here.
Attached File(s)
PrecisionRecall.rtf (1.79K)
FSDirectory.rtf (687bytes)
error file-result.rtf (506bytes)
View Replies
View Related
Jan 27, 2015
Consider this small program and output.
public class Floating{
public static void main( String[] args ){
System.out.println( 2.80 - 1.75 );
}
}
The output is 1.0499999999999998. Why not 1.05?
View Replies
View Related
Apr 18, 2015
Here is the code: I believe the issue is in the nest for loop with I use
f1.length
But I want to not use the individual sizes I put in, something more suitable so I don't have to change the the values everywhere I may need to.
public class ForestFireDemo {
public static void main(String[] args) {
Forest[][] f1;
f1 = new Forest[10][5];
for(int i =0 ; i < f1.length; i++) {
[Code] ....
View Replies
View Related
Feb 2, 2015
I was asked to write code to calculate a person's calories burned/min. This is what I got. The problem is I keep getting an error.
--------------------Configuration: <Default>--------------------
C:Program FilesJavajdk1.7.0_72CaloriesBurned.java:22: error: possible loss of precision
caloriesBurnedPerMinute = 0.0175 * METS * weightInKg;
^
required: int
found: double
1 error
Loss of precision? What does that mean? Do I have to change weightInKg into some other number type?
import java.util.Scanner;
public class CaloriesBurned
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int runningHours, basketballHours, sleepingHours, METS, caloriesBurnedPerMinute;
[Code] ....
View Replies
View Related
Mar 11, 2014
Below code I am using to typecast int to char.
char escapeValue = (char)0;
char cha = (char) 10;
escapeValue = (char)(escapeValue * cha); // Here am getting the above error.
I have 38 similar issues in my workspace.
View Replies
View Related
Feb 7, 2015
I am passing input from the user to a method that will initialize an array of the data (scores in this case). The method is filling the entire array with the last input value.
array initializer method
Java Code:
public static float[] inputAllScores(float validScore) {
float[] diverScores = new float[7];
for (int i = 0; i < diverScores.length; i++) {
diverScores[i] = validScore;
}
return diverScores;
} mh_sh_highlight_all('java');
[Code] .....
View Replies
View Related
Apr 12, 2014
obstacleX = (int) (obstacleX - 0.1);
if(obstacleX <= 0){
obstacleX = 600;
}
repaint();
Thats my code, and the rectangle (ObstacleX is the X cordinate for the rectangle) goes fine on the first few times across the screen, then starts to go hyperspeed....
View Replies
View Related