I have to design and implement an application that draws the graph of the equation of ax^2 + bx + c where the values of a b and c are set using sliders. I am editing my original post and thus am going to do my best to post an sscce. My code is below. Everything compiles and runs. My one question is why is my graph not displaying anything when the sliders are moved? Here are my 2 class files:
import java.awt.*;
import javax.swing.*;
public class QuadraticGraph
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Quadratic Grapher");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new QuadraticPanel());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class QuadraticPanel extends JPanel
{
private JPanel controls, quadpanel;
private JSlider aslider, bslider, cslider;
private JLabel alabel, blabel, clabel;
//-----------------------------------------------------------------
// Sets up the sliders and their labels, aligning them along
// their left edge using a box layout.
//-----------------------------------------------------------------
public QuadraticPanel()
{
aslider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
aslider.setMajorTickSpacing (50);
aslider.setMinorTickSpacing (10);
aslider.setPaintTicks (true);
aslider.setPaintLabels (true);
aslider.setAlignmentX (Component.LEFT_ALIGNMENT);
bslider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
bslider.setMajorTickSpacing (50);
bslider.setMinorTickSpacing (10);
bslider.setPaintTicks (true);
bslider.setPaintLabels (true);
bslider.setAlignmentX (Component.LEFT_ALIGNMENT);
cslider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
cslider.setMajorTickSpacing (50);
cslider.setMinorTickSpacing (10);
cslider.setPaintTicks (true);
cslider.setPaintLabels (true);
cslider.setAlignmentX (Component.LEFT_ALIGNMENT);
SliderListener listener = new SliderListener();
aslider.addChangeListener (listener);
bslider.addChangeListener (listener);
cslider.addChangeListener (listener);
alabel = new JLabel ("A: 0");
alabel.setAlignmentX (Component.LEFT_ALIGNMENT);
blabel = new JLabel ("B: 0");
blabel.setAlignmentX (Component.LEFT_ALIGNMENT);
clabel = new JLabel ("C: 0");
clabel.setAlignmentX (Component.LEFT_ALIGNMENT);
controls = new JPanel();
BoxLayout layout = new BoxLayout (controls, BoxLayout.Y_AXIS);
controls.setLayout (layout);
controls.add (alabel);
controls.add (aslider);
controls.add (Box.createRigidArea (new Dimension (0, 20)));
controls.add (blabel);
controls.add (bslider);
controls.add (Box.createRigidArea (new Dimension (0, 20)));
controls.add (clabel);
controls.add (cslider);
quadpanel = new JPanel();
quadpanel.setPreferredSize (new Dimension (500, 500));
quadpanel.setBackground (Color.white);
add (controls);
add (quadpanel);
}
//*****************************************************************
// Represents the listener for all three sliders.
//*****************************************************************
private class SliderListener implements ChangeListener
{
private double a, b, c, x, y, g, h;
//--------------------------------------------------------------
// Gets the value of each slider, then updates the labels and
// the color panel.
//--------------------------------------------------------------
public void stateChanged (ChangeEvent event)
{
a = aslider.getValue();
b = bslider.getValue();
c = cslider.getValue();
alabel.setText ("A: " + a);
blabel.setText ("B: " + b);
clabel.setText ("C: " + c);
}
public void paintComponent (Graphics page)
{
x = (-b + (Math.sqrt((b*b - ((4 * a * c))))))/ (2 * a);
y= (a*(Math.pow(x,2)))+(b*x)+(c);
int g = (int)Math.round(x);
int h = (int)Math.round(y);
page.setColor (Color.black);
page.drawOval (g, h, 1, 1);
}
}
}
i guess you're very new to java, so here's some starters help ^^
create a content Panel and set some layout to the panel;
add the sliders and a drawing-panel to your content panel;
you're doing it right, adding an change listener to the slider, but they should redraw the drawing-panel.
i'll add this snippets to make it easier for you ^_^
private JPanel drawPanel; //don't forget to create a proper one! override paint in that panel!
private int a,b,c;
public QuadraticPanel(){ //constructor
setLayout(new BoderLayout();
JSlider aSidler = new JSlider();
slider.addChangeListener(new ChangeListener(){
#Override
public void stateChanged(ChangeEvent arg0) {
a = arg0.getValue(); //setting a value
//it might even be better to calculate the value
//BEFORE you redraw
//recalcEquotiation()
drawPanel.repaint(); //and redraw the paint-panel
}
});
add(aSlider, Borderlayout.WEST); //add more sliders with better layouts or subcomponents
add(drawPanel, BorderLayout.CENTER);
}
don't forget - these are just snippets, you'll have to do some work on your own...
Related
I have a problem when i'm trying to use a Timer Object and a repaint() when it is called.
Here's my Window Class :
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame{
Panel pan = new Panel();
JPanel container, north,south, west;
JButton ip,print,cancel,ok;
JTextArea timeStep;
JLabel legend;
double temperature=0.0;
public static void main(String[] args) {
new Window();
}
public Window()
{
System.out.println("je suis là");
this.setSize(700,400);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setTitle("Assignment2 - CPU temperature");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = new JPanel(new BorderLayout());
north = new JPanel();
ip = new JButton ("New");
north.add(ip);
north.add(new JLabel("Time Step: "));
timeStep = new JTextArea("10",1,5);
north.add(timeStep);
print = new JButton ("Print");
north.add(print);
south = new JPanel();
legend = new JLabel("Legends are here");
south.add(legend);
west = new JPanel();
JLabel temp = new JLabel("°C");
west.add(temp);
container.add(north, BorderLayout.NORTH);
container.add(west,BorderLayout.WEST);
container.add(pan, BorderLayout.CENTER);
container.add(south, BorderLayout.SOUTH);
this.setContentPane(container);
this.setVisible(true);
}
}
And Here's my Panel class :
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Panel extends JPanel implements ActionListener {
Timer chrono = new Timer(1000,this);
int i = 0;
int t = 10;
public Panel()
{
super();
chrono.start();
}
public void paintComponent(Graphics g)
{
g.drawLine(20, 20, 20, this.getHeight()-50);
g.drawLine(20, this.getHeight()-50, this.getWidth()-50, this.getHeight()-50);
g.drawLine(20, 20, 15, 35);
g.drawLine(20, 20, 25, 35);
g.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-45);
g.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-55);
g.drawLine(20+t, this.getHeight()-50-i, 20+t, this.getHeight()-50);
}
#Override
public void actionPerformed(ActionEvent e) {
/*i = (int)(50+(20 + (Math.random() * (60 - 20))));
t = t+10;
repaint();
System.out.println("chrono");*/
}
}
Here's how it is like when the repaint() function isn't called :
GUI:
And Finally here's how it looks like when repaint() function is called :
GUI bug:
It seems that all of my JPanels are repainting only once and then everything works...
Any thought?
The problem is that you never paint over what's drawn before. JPanel by default is supposed to be opaque, meaning that it will draw over its entire area each redraw, freeing the frame to not have to worry about cleaning up that space. However, you're removing that functionality from the JPanel.
In your paintComponent method, add this at the top:
super.paintComponent(g);
This will let the JPanel correctly redraw itself (by calling its own paintComponent method), so that you can do your drawing over a clean slate.
In response to your comment, the best way to keep the old lines is to keep track of each line to draw, and repaint them all each time. To do that, you would need to replace t and i with lists, which would look something like this:
List<Integer> is = new ArrayList<>();
List<Integer> ts = new ArrayList<>();
int lastT = 0;
And then add to those instead of simply setting the value:
int i = (int)(50 + (20 + (Math.random() * (60 - 20))));
lastT += 10;
is.add(i);
ts.add(lastT);
repaint();
And finally, loop over each value:
for(int j = 0; i < is.size() && ts.size(); i++){
int i = is.get(j);
int t = ts.get(j);
g.drawLine(20 + t, this.getHeight() - 50 - i,
20 + t, this.getHeight() - 50);
}
I haven't tested the above code, so it may have some mistakes, but it should be the right idea.
I am making a platformer game for a project for class and need to have a chicken character jump on some platforms. I have a start screen and a button, and when the button is clicked, it will change the frame to the first level. When I add the chicken character to the frame as well as the background image, all I can see is the background image. Should I be using a different layout or is there something else I can do. This is my code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main extends JFrame {
public Main(){
//Creates Chicken Character
final JLabel chicken = new JLabel(" ");
ImageIcon chick1 = new ImageIcon("chicken.gif");
ImageIcon chick2 = new ImageIcon("chicken2.gif");
chicken.setIcon(chick1);
//Sets Chicken Location
chicken.setLocation(1, 1);
//Creates Title Image
JLabel title = new JLabel(" ");
ImageIcon tl = new ImageIcon("title.gif");
title.setIcon(tl);
//Creates Start Image
final JButton start = new JButton("");
ImageIcon st = new ImageIcon("start.gif");
start.setIcon(st);
//Creates Options Image
JButton options = new JButton("");
ImageIcon opt = new ImageIcon("options.gif");
options.setIcon(opt);
options.setBackground(Color.BLACK);
//Creates label for level 0 background image
JLabel background = new JLabel(" ");
ImageIcon back = new ImageIcon("level0.gif");
background.setIcon(back);
//Creates a panel for level 0
final JPanel p5 = new JPanel();
chicken.setLocation(1, 1);
p5.add(background);
//Create first frame for "Start" button
final JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
p1.add(start, BorderLayout.CENTER);
//Create second panel for title label
final JPanel p2 = new JPanel(new BorderLayout());
p2.setLayout(new GridLayout(1, 3));
p2.add(title, BorderLayout.WEST);
//Create third panel for "Options" button
final JPanel p3 = new JPanel(new BorderLayout());
p3.setLayout(new GridLayout(1, 1));
p3.add(options, BorderLayout.SOUTH);
//Creates fourth panel to organize all other primary
final JPanel p4 = new JPanel(new BorderLayout());
p4.setLayout(new GridLayout(1, 3));
p4.add(p1, BorderLayout.WEST);
p4.add(p2, BorderLayout.CENTER);
p4.add(p3, BorderLayout.EAST);
//When button is clicked, it changes the level
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(start.isEnabled()) {
remove(p4);
add(new ContentPanel());
add(chicken);
chicken.setLocation(100, 100);
setSize(1440, 500);
setLocale(null);
chicken.isOpaque();
validate();
}
else {
return;
}
}
});
//Adds fourth panel to frame
add(p4, BorderLayout.CENTER);
}
public static void main(String arg[]) {
Main frame = new Main();
//Finds screen size of monitor
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//Creates the frame
frame.setTitle("Cockadoodle Duty: Awakening");
frame.setSize(screenSize);
frame.setLocale(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
String background = "#000000";
frame.setBackground(Color.decode(background));
}
}
class ContentPanel extends JPanel{
Image bgimage = null;
ContentPanel() {
MediaTracker mt = new MediaTracker(this);
bgimage = Toolkit.getDefaultToolkit().getImage("level0.gif");
mt.addImage(bgimage, 0);
try {
mt.waitForAll();
} catch (InterruptedException e){
e.printStackTrace();
}
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
int imwidth = bgimage.getWidth(null);
int imheight = bgimage.getHeight(null);
g.drawImage(bgimage, 1, 1, null);
}
}
Note: It's been a while since I used the Graphics API
Quick Answer:
You need to draw everything in your paintComponent method. Your drawing routine should check the state of all game objects and draw them accordingly. Right now the panel is drawing the background image - that's it. Add your chicken image the same way you added your bgImage.
Some more things to consider:
If you experience screen flicker look into Double Buffering.
Not to overwhelm you but you might want to do some light reading on game coding. Get a general idea on how to code your game loop and what happens each time the loop is executed.
Since you are using an OO language you should also probably make a Chicken class.
public class Chicken {
private int x;
private int y;
private Image chickenSprtie;
//add get / set for access
}
Or even a general Super Class - Sprite with int x, int y. Or even a "Drawable" interface to forces all your drawable game objects to have common methods....
So, I'm trying to make a program where you can input the quadratic formula (ax^2+bx+c) via sliders. Then it draws a graph as you adjust for A, B, and C.
Issues:
I want the stuff I wrote in super paint and the sliders to be in one place.
The sliders are in place when I run it. There's space with the correct background where I want my graph in the panel but no actual graph.
Here's my driver class:
import java.awt.*;
import javax.swing.*;
public class quadraticslider
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Quadratic Slider");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new pp109quadraticpanel());
frame.pack();
frame.setVisible(true);
}
}
Here's the panel class:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class quadraticpanel extends JPanel
{
private JPanel controls, graphPanel;
private JSlider ASlider, BSlider, CSlider;
private JLabel ALabel, BLabel, CLabel;
double A, B, C, x,Y;
//
//SLIDERS YO
//
public quadraticpanel()
{
ASlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0);
ASlider.setMajorTickSpacing (50);
ASlider.setMinorTickSpacing (10);
ASlider.setPaintTicks (true);
ASlider.setPaintLabels (true);
ASlider.setAlignmentX (Component.LEFT_ALIGNMENT);
BSlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0);
BSlider.setMajorTickSpacing (50);
BSlider.setMinorTickSpacing (10);
BSlider.setPaintTicks (true);
BSlider.setPaintLabels (true);
BSlider.setAlignmentX (Component.LEFT_ALIGNMENT);
CSlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0);
CSlider.setMajorTickSpacing (50);
CSlider.setMinorTickSpacing (10);
CSlider.setPaintTicks (true);
CSlider.setPaintLabels (true);
CSlider.setAlignmentX (Component.LEFT_ALIGNMENT);
SliderListener listener = new SliderListener();
ASlider.addChangeListener (listener);
BSlider.addChangeListener (listener);
CSlider.addChangeListener (listener);
ALabel = new JLabel ("a: 0");
ALabel.setAlignmentX (Component.LEFT_ALIGNMENT);
BLabel = new JLabel ("b: 0");
BLabel.setAlignmentX (Component.LEFT_ALIGNMENT);
CLabel = new JLabel ("c: 0");
CLabel.setAlignmentX (Component.LEFT_ALIGNMENT);
controls = new JPanel();
BoxLayout layout = new BoxLayout (controls, BoxLayout.Y_AXIS);
controls.setLayout (layout);
controls.add (ALabel);
controls.add (ASlider);
controls.add (Box.createRigidArea (new Dimension (0, 20)));
controls.add (BLabel);
controls.add (BSlider);
controls.add (Box.createRigidArea (new Dimension (0, 20)));
controls.add (CLabel);
controls.add (CSlider);
graphPanel = new JPanel();
graphPanel.setPreferredSize (new Dimension (500, 500));
graphPanel.setBackground (Color.white);
add (controls);
add (graphPanel);
}
//Here I'm taking the equation, running it through -10 to 10
//It takes the doubles from the equation, converts
//it to int then draws the quadratic formula in dots.
public void paintComponent(Graphics page)
{
super.paintComponent (page);
for ( x=-10; x <= 10; x++)
{
Y = (A*(Math.pow(x,2)))+(B*x)+(C);
int g = (int)Math.round(x);
int h = (int)Math.round(Y);
page.setColor (Color.black);
page.fillOval (g, h, 1, 1);
}
}
public class SliderListener implements ChangeListener
{
///
///Reads the user input via slider.
///
public void stateChanged (ChangeEvent event)
{
A = ASlider.getValue();
B = BSlider.getValue();
C = CSlider.getValue();
ALabel.setText ("a: " + A);
BLabel.setText ("b: " + B);
CLabel.setText ("c: " + C);
}
}
}
These examples using JFreeChart may be of interest. As shown here, you can animate the rendering using SwingWorker, and this example updates a chart using a JSlider.
Addendum: This variation of your code may guide you going forward. Note,
Override relevant methods in your graphPanel.
Scale and invert coordinates, as shown here.
Consider JSpinner for fractional values.
Use constants for consistency.
Use common naming conventions for clarity.
See also Initial Threads.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/** #see https://stackoverflow.com/a/20556929/230513 */
public class QuadraticSlider {
private static final int N = 500;
private static final int A = 1;
private static final int B = 0;
private static final int C = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Quadratic Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new QuadraticPanel());
frame.pack();
frame.setVisible(true);
}
});
}
private static class QuadraticPanel extends JPanel {
private Box controls;
private JPanel graphPanel;
private JSlider aSlider, bSlider, cSlider;
private JLabel aLabel, bLabel, cLabel;
double a, b, c, x, y;
public QuadraticPanel() {
aSlider = new JSlider(JSlider.HORIZONTAL, -25, 25, A);
aSlider.setMajorTickSpacing(10);
aSlider.setMinorTickSpacing(5);
aSlider.setPaintTicks(true);
aSlider.setPaintLabels(true);
aSlider.setAlignmentX(Component.LEFT_ALIGNMENT);
bSlider = new JSlider(JSlider.HORIZONTAL, -10, 10, B);
bSlider.setMajorTickSpacing(5);
bSlider.setMinorTickSpacing(1);
bSlider.setPaintTicks(true);
bSlider.setPaintLabels(true);
bSlider.setAlignmentX(Component.LEFT_ALIGNMENT);
cSlider = new JSlider(JSlider.HORIZONTAL, -100, 100, C);
cSlider.setMajorTickSpacing(50);
cSlider.setMinorTickSpacing(10);
cSlider.setPaintTicks(true);
cSlider.setPaintLabels(true);
cSlider.setAlignmentX(Component.LEFT_ALIGNMENT);
SliderListener listener = new SliderListener();
aSlider.addChangeListener(listener);
bSlider.addChangeListener(listener);
cSlider.addChangeListener(listener);
aLabel = new JLabel("a: 0");
bLabel = new JLabel("b: 0");
cLabel = new JLabel("c: 0");
controls = new Box(BoxLayout.Y_AXIS);
controls.add(aLabel);
controls.add(aSlider);
controls.add(Box.createRigidArea(new Dimension(0, 20)));
controls.add(bLabel);
controls.add(bSlider);
controls.add(Box.createRigidArea(new Dimension(0, 20)));
controls.add(cLabel);
controls.add(cSlider);
graphPanel = new JPanel() {
private static final int SCALE = 5;
#Override
public Dimension getPreferredSize() {
return new Dimension(N, N);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (x = -10; x <= 10; x++) {
y = a * x * x + b * x + c;
g.setColor(Color.black);
int w = (int) (x * SCALE) + N / 2;
int h = (int) (-y * SCALE) + N / 2;
g.fillOval(w, h, 5, 5);
}
}
};
graphPanel.setBackground(Color.white);
add(controls);
add(graphPanel);
listener.stateChanged(null);
}
class SliderListener implements ChangeListener {
#Override
public void stateChanged(ChangeEvent event) {
a = aSlider.getValue() / 5d;
b = bSlider.getValue();
c = cSlider.getValue();
aLabel.setText("a: " + a);
bLabel.setText("b: " + b);
cLabel.setText("c: " + c);
repaint();
}
}
}
}
_"error: possible loss of precision Y = (A*(Math.pow(x,2)))+(B*x)+(C); ^ required: int found: double"_
All your int variables int A, B, C, x,Y;. Make them doubles. double A, B, C, x,Y;
I modified the code so that it solves for x given a b and c. Then plugs x back in and solves for y. I'm still not getting it to draw though. I also took out the loop since the sliders are setting the a b and c values which lead to x. Anyone know why it won't draw?
package quadraticslider;
import java.awt.*;
import javax.swing.*;
public class quadraticslider
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Quadratic Slider");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new quadraticpanel());
frame.pack();
frame.setVisible(true);
}
}
package quadraticslider;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class quadraticpanel extends JPanel
{
private JPanel controls, graphPanel;
private JSlider ASlider, BSlider, CSlider;
private JLabel ALabel, BLabel, CLabel;
double A, B, C, x,Y;
public quadraticpanel()
{
ASlider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
ASlider.setMajorTickSpacing (50);
ASlider.setMinorTickSpacing (10);
ASlider.setPaintTicks (true);
ASlider.setPaintLabels (true);
ASlider.setAlignmentX (Component.LEFT_ALIGNMENT);
BSlider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
BSlider.setMajorTickSpacing (50);
BSlider.setMinorTickSpacing (10);
BSlider.setPaintTicks (true);
BSlider.setPaintLabels (true);
BSlider.setAlignmentX (Component.LEFT_ALIGNMENT);
CSlider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
CSlider.setMajorTickSpacing (50);
CSlider.setMinorTickSpacing (10);
CSlider.setPaintTicks (true);
CSlider.setPaintLabels (true);
CSlider.setAlignmentX (Component.LEFT_ALIGNMENT);
SliderListener listener = new SliderListener();
ASlider.addChangeListener (listener);
BSlider.addChangeListener (listener);
CSlider.addChangeListener (listener);
ALabel = new JLabel ("a: 0");
ALabel.setAlignmentX (Component.LEFT_ALIGNMENT);
BLabel = new JLabel ("b: 0");
BLabel.setAlignmentX (Component.LEFT_ALIGNMENT);
CLabel = new JLabel ("c: 0");
CLabel.setAlignmentX (Component.LEFT_ALIGNMENT);
controls = new JPanel();
BoxLayout layout = new BoxLayout (controls, BoxLayout.Y_AXIS);
controls.setLayout (layout);
controls.add (ALabel);
controls.add (ASlider);
controls.add (Box.createRigidArea (new Dimension (0, 20)));
controls.add (BLabel);
controls.add (BSlider);
controls.add (Box.createRigidArea (new Dimension (0, 20)));
controls.add (CLabel);
controls.add (CSlider);
graphPanel = new JPanel();
graphPanel.setPreferredSize (new Dimension (500, 500));
graphPanel.setBackground (Color.white);
add (controls);
add (graphPanel);
}
public void paintComponent(Graphics page)
{
super.paintComponent (page);
x = (-B + (Math.sqrt((B*B - ((4 * A * C))))))/ (2 * A);
Y = (A*(Math.pow(x,2)))+(B*x)+(C);
int g = (int)Math.round(x);
int h = (int)Math.round(Y);
page.setColor (Color.black);
page.drawOval (g, h, 1, 1);
}
public class SliderListener implements ChangeListener
{
///
///Reads the user input via slider.
///
public void stateChanged (ChangeEvent event)
{
A = ASlider.getValue();
B = BSlider.getValue();
C = CSlider.getValue();
ALabel.setText ("a: " + A);
BLabel.setText ("b: " + B);
CLabel.setText ("c: " + C);
}
}
}
how can I correctly returns XxxSize from JComponent(s) added to the JLabel
1st. figure >> lets LayoutManager works like as for JPanel, JLabel returns Size(0, 0)
2nd. figure >> added some PreferredSize to the JLabel
3rd. figure >> calculated PreferredSize from JComponent(s) added to the JLabel
4th. figure >> lets LayoutManager works changed JLabel to JPanel, now LayoutManager correctly calculated Dimension without using any XxxSize
notice sice there is used Nimbus L&F, same output is there for all accesible L&F
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
public class NimbusBorderPainterDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame();
private JPanel fatherPanel = new JPanel(), titlePanel = new JPanel();
private JLabel buttonPanel = new JLabel();
//figure ---> 4th. switch JLabel with JPanel
//private JPanel buttonPanel = new JPanel();
private Queue<Icon> iconQueue = new LinkedList<Icon>();
public NimbusBorderPainterDemo() {
iconQueue.add(UIManager.getIcon("OptionPane.errorIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.informationIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.warningIcon"));
JButton button0 = createButton();
JButton button1 = createButton();
JButton button2 = createButton();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
int gap = 5;
buttonPanel.setLayout(new GridLayout(0, 3, gap, 0));
buttonPanel.add(button0);
buttonPanel.add(button1);
buttonPanel.add(button2);
// figure 1st. ---> without PreferredSize
// figure 2nd. --->
//buttonPanel.setPreferredSize(new Dimension(160, 30));
// figure 3rd. --->
/*Dimension dim = button0.getPreferredSize();
int w = dim.width;
int h = dim.height;
w = (w + 5) * 3;
h += 4;
dim = new Dimension(w, h);
buttonPanel.setPreferredSize(dim);*/
titlePanel.setLayout(new BorderLayout());
titlePanel.add(new JLabel(nextIcon()), BorderLayout.WEST);
titlePanel.add(new JLabel("My Frame"), BorderLayout.CENTER);
titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
titlePanel.add(buttonPanel, BorderLayout.EAST);
fatherPanel.setLayout(new BorderLayout());
fatherPanel.add(titlePanel, BorderLayout.CENTER);
frame.setUndecorated(true);
frame.add(fatherPanel);
frame.setLocation(50, 50);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
}
private JButton createButton() {
JButton button = new JButton();
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
button.setIcon(nextIcon());
//button.setRolloverIcon(nextIcon());
//button.setPressedIcon(nextIcon());
//button.setDisabledIcon(nextIcon());
nextIcon();
return button;
}
private Icon nextIcon() {
Icon icon = iconQueue.peek();
iconQueue.add(iconQueue.remove());
return icon;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception fail) {
}
UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.RED);
NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo();
}
});
}
}
The default preferred size calculation is to use the layout manager to determine the preferred size of a component. This means the layout manager iterates through all the child components to determine the preferred size of each. For a JPanel, which is meant to be used as a Container this calculation is used.
However, for other Swing components, the getPreferredSize() method is always overridden to provide a reasonable size for the given component.
In the case of a JLabel, the preferred size calculation takes into account the text and the icon used. Since you didn't provide either the preferred size is zero. Of course if you manually override this calculation by using the setPreferredSize() method then the component will have a preferred size.
So even though Swing allows you to add components to any component and use a layout manager to layout the child components, these child components are not used in the preferred size calculation.
This is not just a Nimbus issue.
I am using Swing and AWT (for the listeners) to make a small program. I have a problem concerning getting the size of my JPanel (the class named Chess).
My Layout:
public class Main extends JFrame implements MouseListener, ActionListener{
Chess chessPanel = new Chess ();
JButton newGameButton = new JButton ("New Game");
JButton loadGameButton = new JButton ("Load Game");
JButton saveGameButton = new JButton ("Save Game");
JButton exitButton = new JButton ("Exit");
public static void main (String [] args) {
new Main();
}
Main () {
super ("Chess");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dim);
setLocation(0,0);
setUndecorated(true);
chessPanel.addMouseListener(this);
add(chessPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
newGameButton.addActionListener(this);
loadGameButton.addActionListener(this);
saveGameButton.addActionListener(this);
exitButton.addActionListener(this);
buttonPanel.add(newGameButton);
buttonPanel.add(loadGameButton);
buttonPanel.add(saveGameButton);
buttonPanel.add(exitButton);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
// ... Code ...
}
As you can see by the code, I have one JPanel in the CENTER, which takes nearly all the screen. In the bottom I have another JPanel (SOUTH), which has a row of buttons.
What I need is the size that the JPanel in the CENTER takes. When I call the getWidth(), getHeight() or getBounds() methods inherited from JPanel, they all return 0, because of the BorderLayout.
Any idea how to get the real values?
PS: The screen always takes up the entire screen, and will never be resized, if that helps.
You're likely calling getWidth before the JPanel has been rendered, and so it will be 0. The solution is to get the size after rendering, for instance after pack() or setVisible(true) has been called on the root container that holds this JPanel.
Also, I recommend against calling setSize() on anything since most of the standard layout managers observe the preferred size of a component, not the size, and when you call pack() telling the layout managers to do their thing, the set sizes are usually ignored. You may want to make your JPanel that is in the center set its own size by overriding its setPreferredSize method if it needs to be a certain size. Then let the JFrame and its held containers set the bet fit size based on the their layout managers when you call pack.
e.g.,
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
Chess chessPanel = new Chess();
JButton newGameButton = new JButton("New Game");
JButton loadGameButton = new JButton("Load Game");
JButton saveGameButton = new JButton("Save Game");
JButton exitButton = new JButton("Exit");
public static void main(String[] args) {
new Main();
}
Main() {
super("Chess");
add(chessPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(newGameButton);
buttonPanel.add(loadGameButton);
buttonPanel.add(saveGameButton);
buttonPanel.add(exitButton);
System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(buttonPanel, BorderLayout.SOUTH);
pack();
System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize());
setLocationRelativeTo(null);
setVisible(true);
}
// ... Code ...
}
#SuppressWarnings("serial")
class Chess extends JPanel {
private static final int CHESS_WIDTH = 600;
private static final int CHESS_HEIGHT = CHESS_WIDTH;
private static final int MAX_ROW = 8;
private static final int MAX_COL = 8;
private static final Color LIGHT_COLOR = new Color(240, 190, 40);
private static final Color DARK_COLOR = new Color(180, 50, 0);
#Override
public Dimension getPreferredSize() {
return new Dimension(CHESS_WIDTH, CHESS_HEIGHT);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int panelWidth = getWidth();
int panelHeight = getHeight();
int sqrWidth = panelWidth / MAX_ROW;
int sqrHeight = panelHeight / MAX_COL;
for (int row = 0; row < MAX_ROW; row++) {
for (int col = 0; col < MAX_COL; col++) {
Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR;
g.setColor(c);
int x = (row * panelWidth) / MAX_ROW;
int y = (col * panelHeight) / MAX_COL;
g.fillRect(x, y, sqrWidth, sqrHeight);
}
}
}
}