Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The problem is that the text under the bar chart is not aligned with the bars in the bar chart. How do I make them align properly?
What more could I possibly add as details to this question to make your detector shut up? :)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleBarChart extends JPanel {
private double[] value;
private String[] languages;
private String title;
private int gapBetweenBars = 40;//MODIFICATION - NOT A PART OF ORIGINAL CODE
public SimpleBarChart(double[] val, String[] lang, String t) {
languages = lang;
value = val;
title = t;
}
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
if (value == null || value.length == 0) {
return;
}
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < value.length; i++) {
if (minValue > value[i]) {
minValue = value[i];
}
if (maxValue < value[i]) {
maxValue = value[i];
}
}
Dimension dim = getSize();
int clientWidth = dim.width;
int clientHeight = dim.height;
int barWidth = clientWidth / value.length;
barWidth = barWidth / 3;//MODIFICATION - NOT A PART OF ORIGINAL CODE
Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int q = titleFontMetrics.getAscent();
int p = (clientWidth - titleWidth) / 2;
graphics.setFont(titleFont);
graphics.drawString(title, p, q);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue) {
return;
}
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
q = clientHeight - labelFontMetrics.getDescent();
graphics.setFont(labelFont);
for (int j = 0; j < value.length; j++) {
int valueP = j * (barWidth + gapBetweenBars) + 1; //MODIFICATION - NOT A PART OF ORIGINAL CODE
int valueQ = top;
int height = (int) (value[j] * scale);
if (value[j] >= 0) {
valueQ += (int) ((maxValue - value[j]) * scale);
} else {
valueQ += (int) (maxValue * scale);
height = -height;
}
graphics.setColor(Color.blue);
graphics.fillRect(valueP, valueQ, barWidth - 2, height);
graphics.setColor(Color.black);
graphics.drawRect(valueP, valueQ, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(languages[j]);
p = j * barWidth + (barWidth - labelWidth) / 2;
graphics.drawString(languages[j], p, q);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
double[] value = new double[5];
String[] languages = new String[5];
value[0] = 1;
languages[0] = "Visual Basic";
value[1] = 2;
languages[1] = "PHP";
value[2] = 3;
languages[2] = "C++";
value[3] = 4;
languages[3] = "C";
value[4] = 5;
languages[4] = "Java";
frame.getContentPane().add(new SimpleBarChart(value, languages, "Programming Languages"));
WindowListener winListener = new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
};
frame.addWindowListener(winListener);
frame.setVisible(true);
}
public void setGapBetweenBars(int gapBetweenBars) {
this.gapBetweenBars = gapBetweenBars;
}
public int getGapBetweenBars() {
return this.gapBetweenBars;
}
}
The value of p in your for loop is incorrect and should rather be:
p = j * (barWidth + gapBetweenBars) + (barWidth - labelWidth) / 2;
You forgot to take the gap into account.
use this in side for loop
int labelWidth = labelFontMetrics.stringWidth(languages[j]);
p = j * (barWidth+gapBetweenBars) + (barWidth - labelWidth) / 2;
changed calculation of space between Languages strings
Related
I am currently just messing around with some code and I keep running into an issue. I want to create ten circles and simply have them bounce around the window. I've had a couple of problems (like when I want the circles to bounce off the wall, for some reason the 400,400 window isn't actually that size. I have the circles collide on the right by checking if x + width >= 400, but it bounces outside the screen unless I change the 400 to 380?), but my main issue is that when I create the circles, I want them to be in different locations (so they aren't colliding before they can even move). I am trying to get it so that if a circle is going to be 'inside' another circle then instead create random x and y coordinates again until it isn't inside another circle. But for some reason, if I put r.nextInt() inside the while loop it keeps giving me the same values. Can anyone help?
p.s. I wouldn't mind advice on any other mistakes I have made.
package practicedots;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PracticeDots extends JPanel {
float dots[][] = new float[10][7];
Random r = new Random();
boolean first = true;
float x = 0;
float y = 0;
float xAccel = 0;
float yAccel = 0;
int wall = 380;
int width = 50;
float radius = 0;
float centreX = 0;
float centreY = 0;
boolean collision;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new PracticeDots());
f.setPreferredSize(new Dimension(400, 400));
f.setResizable(true);
f.pack();
f.setVisible(true);
}
/**
*
* #return
*/
public float[][] CreateDots() {
if (first == true) {
for (int i = 0; i < 10; i++) {
while(collision == true){
x = r.nextInt(300);
y = r.nextInt(300);
xAccel = r.nextFloat() / 2;
yAccel = r.nextFloat() / 2;
radius = width/2;
centreX = x + radius;
centreY = y + radius;
dots[i][0] = x;
dots[i][1] = y;
dots[i][2] = xAccel;
dots[i][3] = yAccel;
dots[i][4] = radius;
dots[i][5] = centreX;
dots[i][6] = centreY;
bounce();
}
}
first = false;
} else if (first == false) {
for (int i = 0; i < 10; i++) {
dots[i][0] = dots[i][0] + dots[i][2];
dots[i][1] = dots[i][1] + dots[i][3];
if (dots[i][0] >= wall - width) {
dots[i][2] = -dots[i][2];
}
if (dots[i][1] >= wall - 20 - width) {
dots[i][3] = -dots[i][3];
}
if (dots[i][0] < 0) {
dots[i][2] = -dots[i][2];
}
if (dots[i][1] < 0) {
dots[i][3] = -dots[i][3];
}
bounce();
}
}
repaint();
return dots;
}
//(x2-x1)^2 + (y1-y2)^2 <= (r1+r2)^2
public void bounce() {
for (int i = 0; i < 10; i++) {
for (int a = 0; a < 10; a++) {
if (a != i) {
System.out.println((dots[i][0] - dots[a][0])*(dots[i][0] - dots[a][0]) + (dots[i][1] - dots[a][1])*(dots[i][1] - dots[a][1]) <= (dots[i][4] + dots[a][4]) * (dots[i][4] + dots[a][4]));
collision = (dots[i][0] - dots[a][0])*(dots[i][0] - dots[a][0]) + (dots[i][1] - dots[a][1])*(dots[i][1] - dots[a][1]) <= (dots[i][4] + dots[a][4]) * (dots[i][4] + dots[a][4]);
}
}
}
}
/**
*
* #param g
*/
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 10; i++) {
CreateDots();
g.drawOval((int) dots[i][0], (int) dots[i][1], width, width);
g.fillOval((int) dots[i][0], (int) dots[i][1], width, width);
}
}
}
<!-- end snippet -->
There were a couple of problems:
During bounce you should return the first time you find a collision, otherwise the collision will be set to true, but then could be set back to false on the next iteration in the for-loop.
In the first == true condition, you should initialize collision to true or it will never do the while loop at all. Either that or change it to a do-while.
During paintComponent you should not call CreateDots within the for-loop since it loops over all dots itself. Just call it before.
The code seems to work with these changes (including width of 400 not 380):
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PracticeDots extends JPanel {
float dots[][] = new float[10][7];
Random r = new Random();
boolean first = true;
float x = 0;
float y = 0;
float xAccel = 0;
float yAccel = 0;
int wall = 400;
int width = 50;
float radius = 0;
float centreX = 0;
float centreY = 0;
boolean collision;
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new PracticeDots());
f.setPreferredSize(new Dimension(400, 400));
f.setResizable(true);
f.pack();
f.setVisible(true);
}
public float[][] CreateDots() {
if (first == true) {
for (int i = 0; i < 10; i++) {
do {
x = r.nextInt(300);
y = r.nextInt(300);
xAccel = r.nextFloat() / 2;
yAccel = r.nextFloat() / 2;
radius = width / 2;
centreX = x + radius;
centreY = y + radius;
dots[i][0] = x;
dots[i][1] = y;
dots[i][2] = xAccel;
dots[i][3] = yAccel;
dots[i][4] = radius;
dots[i][5] = centreX;
dots[i][6] = centreY;
bounce();
} while (collision == true);
}
first = false;
} else {
for (int i = 0; i < 10; i++) {
dots[i][0] = dots[i][0] + dots[i][2];
dots[i][1] = dots[i][1] + dots[i][3];
if (dots[i][0] >= wall - width) {
dots[i][2] = -dots[i][2];
}
if (dots[i][1] >= wall - 20 - width) {
dots[i][3] = -dots[i][3];
}
if (dots[i][0] < 0) {
dots[i][2] = -dots[i][2];
}
if (dots[i][1] < 0) {
dots[i][3] = -dots[i][3];
}
bounce();
}
}
repaint();
return dots;
}
public void bounce() {
collision = false;
for (int i = 0; i < 10; i++) {
for (int a = 0; a < 10; a++) {
if (a != i && !(dots[a][0] == 0 && dots[a][1] == 0)) {
boolean thisCollision = (dots[i][0] - dots[a][0]) * (dots[i][0] - dots[a][0]) + (dots[i][1] - dots[a][1]) * (dots[i][1] - dots[a][1]) <= (dots[i][4] + dots[a][4]) * (dots[i][4] + dots[a][4]);
// System.out.println("collision: "+collision+" i="+i+" a="+a);
if (thisCollision) {
collision = true;
return;
}
}
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
CreateDots();
for (int i = 0; i < 10; i++) {
g.drawOval((int) dots[i][0], (int) dots[i][1], width, width);
g.fillOval((int) dots[i][0], (int) dots[i][1], width, width);
}
}
}
Here is my code:
public class test extends GraphicsProgram {
private static final int LABELGAP = 30;
private static final int LINEGAP = 20;
int numLabel = 1;
double y = 75;
public void run() {
for (int i = 0; i < 8; i++) {
GLabel label = new GLabel("Hello World");
label.setFont("London-12");
double x = (getWidth() - label.getWidth() * numLabel - LABELGAP * (numLabel -1)) / 2;
double step = label.getWidth() + LABELGAP;
for (int j = 0; j < numLabel; j++) {
add(label, x, y);
x += step;
}
numLabel++;
y += LINEGAP;
}
}
}
But there is only one label per line. What's wrong?
I want to have one more label than previous line.
Also, if I put
GLabel label = new GLabel("Hello World");
label.setFont("London-12");
double x = (getWidth() - label.getWidth() * numLabel - LABELGAP * (numLabel -1)) / 2;
double step = label.getWidth() + LABELGAP;
This part of the code outside of 2nd for loop, the result is different from when I put them inside(neither is the desired result). Why?
I am creating a program for my computer science program. I am trying to call a class called "StockBarChart". How would I call this class from my main class?
Edit: I have posted the code below
In this class I have created a bar chart. I am not quite sure where I have gone wrong.
public class StockBarChart extends JPanel {
private double[] values;
private String[] names;
private String title;
public StockBarChart(double[] v, String[] n, String t) {
names = n;
values = v;
title = t;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (values == null || values.length == 0)
return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
if (minValue > values[i])
minValue = values[i];
if (maxValue < values[i])
maxValue = values[i];
}
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);
for (int i = 0; i < values.length; i++) {
int valueX = i * barWidth + 1;
int valueY = top;
int height = (int) (values[i] * scale);
if (values[i] >= 0)
valueY += (int) ((maxValue - values[i]) * scale);
else {
valueY += (int) (maxValue * scale);
height = -height;
}
g.setColor(Color.red);
g.fillRect(valueX, valueY, barWidth - 2, height);
g.setColor(Color.black);
g.drawRect(valueX, valueY, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[i]);
x = i * barWidth + (barWidth - labelWidth) / 2;
g.drawString(names[i], x, y);
}
}
public static void main(String[] argv) {
try{
String breadStockBeforeTrim = new String(Files.readAllBytes(Paths.get("bread.txt")));
String breadStock = breadStockBeforeTrim.trim();
int breadNumber = Integer.parseInt(breadStock);
String browniesStockBeforeTrim = new String(Files.readAllBytes(Paths.get("brownies.txt")));
String browniesStock = browniesStockBeforeTrim.trim();
int browniesNumber = Integer.parseInt(browniesStock);
JFrame f = new JFrame();
f.setSize(400, 300);
double[] values = new double[3];
String[] names = new String[3];
values[0] = 1;
names[0] = "Item 1";
values[1] = breadNumber;
names[1] = "Item 2";
values[2] = browniesNumber;
names[2] = "Item 3";
f.getContentPane().add(new
StockBarChart(values, names, "title"));
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
f.addWindowListener(wndCloser);
f.setVisible(true);
}catch(IOException b){
b.printStackTrace();
}
}
}
The answer is simple:
Here is the signature of the Constructor of your class StockBarChart:
public StockBarChart(double[] v, String[] n, String t) {
names = n;
values = v;
title = t;
}
Whenever you want to instantiate any of your class, you init it via the constructor. Even when a constructor is not written explicitly, java assigns a constructor (empty constructor) by default.
In your case, when you instantiate the class, it expects a few values which are mapped through the constructor and are necessary for the object to operate on variables, via the class methods.
Simply providing those parameters will solve your problem.
So to say, you should instantiate the Class like below:
StockBarChart stockBarChart = new StockBarChart(doubleArray,stringArray,stringValue)
If the error message says that the required types are double[] String[] and String that means that you need to give the constructor the needed parameters.
It seems that your class StockBarChart has a constructor such as:
public StockBarChart(double[] v, String[] n, String t) {}
and that means that when instantiating the class, you need to give it those double[], String[] and String:
StockBarChart stockBarChart = new StockBarChart(someSetParameterOfDouble[]Type, someString[]Parameter, someString);
You initialize those and given them to the constructor.
I'm working on a problem where I'm needing to represent the Mandelbrot set graphically using OpenCL and working on my sequential code first. However, the image it is producing isn't very good and I'm unsure if I've missed something somewhere or if this is merely an issue with a lack of resolution (so to speak). I've posted the code below along with a screenshot of what it produces - is this what I should be expecting or have I messed this up somewhere?
public class SequentialMandelbrot {
private static int[] colorMap;
private static int xSize = 200, ySize = 200;
private static float yMin = -2f, yMax = 2f;
private static float xMin = -2f, xMax = 2f;
private static float xStep = (xMax - xMin) / (float)xSize;
private static float yStep = (yMax - yMin) / (float)ySize;
private static final int maxIter = 250;
private static BufferedImage image;
private static JComponent imageComponent;
public static void main(String[] args) {
// Create the image and the component that will paint the image
initColorMap(32, Color.RED, Color.GREEN, Color.BLUE);
image = new BufferedImage(xSize, ySize, BufferedImage.TYPE_INT_RGB);
imageComponent = new JPanel()
{
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0,0,this);
}
};
for (int j = 0; j < xSize; j++) {
for (int k = 0; k < ySize; k++) {
int iter = mandelbrot(j, k);
if (iter == maxIter) {
image.setRGB(j, k, 0);
} else {
int local_rgb = colorMap[iter%64];
image.setRGB(j, k, local_rgb);
}
}
}
JFrame frame = new JFrame("JOCL Simple Mandelbrot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
imageComponent.setPreferredSize(new Dimension(xSize, ySize));
frame.add(imageComponent, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private static int mandelbrot(float j, float k) {
int t = 0;
float norm = 0;
float x = 0;
float y = 0;
float r = xMin + (j * xStep);
float i = yMin + (k * yStep);
while (t < maxIter && norm < 4) {
x = (x*x) - (y*y) + r;
y = (2*x*y) + i;
norm = (x*x) + (y*y);
t++;
}
return t;
}
I have also altered the code for the Julia set (from the number 0.45 + 0.1428i) and it produces something equally questionable:
This is your iteration loop, which is incorrect.
while (t < maxIter && norm < 4) {
x = (x*x) - (y*y) + r;
y = (2*x*y) + i;
norm = (x*x) + (y*y);
t++;
}
You are overwriting x before re-using it to calculate y. I suggest using a temporary variable, such as
while (t < maxIter && norm < 4) {
tempx = (x*x) - (y*y) + r;
y = (2*x*y) + i;
x = tempx;
norm = (x*x) + (y*y);
t++;
}
Aside: there is room for some efficiency too, as you are calculating x*x and y*y twice.
I'm coding a minesweeper clone and what I have problem with right now is the ratio of buttons on my grid - I want to allow users to resize the window as they please but I'd love the buttons to keep the same 1:1 width/height ratio rather than becoming pancakes or prison bars under some circumstances :) Is there some way to easily force the JButton to always remain square without giving it a fixed width and height?
You can write a custom LayoutManager e.g. GridLayout extension and set the children sizes.
import javax.swing.*;
import java.awt.*;
public class SquareButtonsTestApp extends JFrame {
public static void main ( String[] args ) {
new SquareButtonsTestApp();
}
public SquareButtonsTestApp() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new MyGridLayout(10, 20));
for (int i=0; i<10; i++) {
for (int j=0; j<20; j++) {
getContentPane().add(new JButton(" "));
}
}
setSize(800,600);
setLocationRelativeTo(null);
setVisible(true);
}
class MyGridLayout extends GridLayout {
public MyGridLayout(int rows, int cols) {
super(rows, cols);
}
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
boolean ltr = parent.getComponentOrientation().isLeftToRight();
if (ncomponents == 0) {
return;
}
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
} else {
nrows = (ncomponents + ncols - 1) / ncols;
}
// 4370316. To position components in the center we should:
// 1. get an amount of extra space within Container
// 2. incorporate half of that value to the left/top position
// Note that we use trancating division for widthOnComponent
// The reminder goes to extraWidthAvailable
int totalGapsWidth = (ncols - 1) * getHgap();
int widthWOInsets = parent.getWidth() - (insets.left + insets.right);
int widthOnComponent = (widthWOInsets - totalGapsWidth) / ncols;
int extraWidthAvailable = (widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2;
int totalGapsHeight = (nrows - 1) * getVgap();
int heightWOInsets = parent.getHeight() - (insets.top + insets.bottom);
int heightOnComponent = (heightWOInsets - totalGapsHeight) / nrows;
int extraHeightAvailable = (heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2;
int size=Math.min(widthOnComponent, heightOnComponent);
widthOnComponent=size;
heightOnComponent=size;
if (ltr) {
for (int c = 0, x = insets.left + extraWidthAvailable; c < ncols ; c++, x += widthOnComponent + getHgap()) {
for (int r = 0, y = insets.top + extraHeightAvailable; r < nrows ; r++, y += heightOnComponent + getVgap()) {
int i = r * ncols + c;
if (i < ncomponents) {
parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
}
}
}
} else {
for (int c = 0, x = (parent.getWidth() - insets.right - widthOnComponent) - extraWidthAvailable; c < ncols ; c++, x -= widthOnComponent + getHgap()) {
for (int r = 0, y = insets.top + extraHeightAvailable; r < nrows ; r++, y += heightOnComponent + getVgap()) {
int i = r * ncols + c;
if (i < ncomponents) {
parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
}
}
}
}
}
}
}
}