Creating a Bar graph on JPanel - java

Can anyone tell me what is wrong with this? I'm pretty new to Java and I can't seem to get why this won't open the JPanel or the rectangles, or the strings. Nothing is being shown but it does compile.
Code:
import java.awt.*;
import javax.swing.*;
public class Graphing extends JPanel
{
public static void main (String[] args)
{
}
public Graphing()
{
JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane();
frame.setPreferredSize(new Dimension(350,200));
frame.pack();
frame.setVisible(true);
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
final int HEIGHT = 10;
page.setColor (Color.yellow);
page.fillRect (50, 50, (7*10), HEIGHT);//1-10
page.fillRect (50, 64, (5*10), HEIGHT);//11-20
page.fillRect (50, 78, (0*10), HEIGHT);//21-30
page.fillRect (50, 92, (1*10), HEIGHT);//31-40
page.fillRect (50, 106, (9*10), HEIGHT);//41-50
page.fillRect (50, 120, (12*10), HEIGHT);//51-60
page.fillRect (50, 134, (4*10), HEIGHT);//61-70
page.fillRect (50, 148, (6*10), HEIGHT);//71-80
page.fillRect (50, 162, (2*10), HEIGHT);//81-90
page.fillRect (50, 176, (13*10), HEIGHT);//91-100
page.drawString("1-10",10,60);
page.drawString("11-20",10,74);
page.drawString("21-30",10,88);
page.drawString("31-40",10,102);
page.drawString("41-50",10,116);
page.drawString("51-60",10,130);
page.drawString("61-70",10,144);
page.drawString("71-80",10,158);
page.drawString("81-90",10,172);
page.drawString("91-100",10,186);
page.drawString("7", 30, 60);
page.drawString("5", 30, 74);
page.drawString("0", 30, 88);
page.drawString("1", 30, 102);
page.drawString("9", 30, 116);
page.drawString("12", 30, 130);
page.drawString("4", 30, 144);
page.drawString("6", 30, 158);
page.drawString("2", 30, 172);
page.drawString("13", 30, 186);
}
}

public Graphing() {
JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
in main
public static void main(String[] args) {
new Graphing();
}
To be fully Kosher, the main method should actually be:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Graphing();
}
});
}
While at this point, you don't have to know the details of this second main method code, but do know that it ensures that the Swing GUI is called on the main Swing thread. Doing this can help avoid unpredictable threading problems that are not likely to occur in your simple program, but can occur if your code gets a little more complex.
Other issues -- you'll want to try to avoid using magic numbers, hard-coded numbers, and instead use variables which will allow you to more easily change the heights of your bars.

Related

Java on Eclipse won't show the JPanel even when I add it to JFrame

These are the files. I have set JFrame to be visible, and have added JPanel to it, but still, the code only shows the window without anything in it.
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Collections;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
frame.setVisible(true);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.setVisible(true);
}
-------------DRAWINGPANEL FILE-------------------
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel {
public void painting(Graphics pen) {
pen.drawRect(50, 50, 20, 20);
pen.drawRect(100, 50, 40, 20);
pen.drawOval(200,50,20,20);
pen.drawOval(250, 50, 40, 20);
pen.drawString("Square", 50, 90);
pen.drawString("Rectangle", 100, 90);
pen.drawString("Cirlce", 200, 90);
pen.drawString("Oval", 250, 90);
pen.fillRect(50, 100, 20, 20);
pen.fillRect(100, 100, 40, 20);
pen.fillOval(250, 100, 20, 20);
pen.fillOval(250, 100, 40, 20);
pen.drawLine(50, 150, 300, 150);
pen.drawArc(50, 150, 200, 100, 0, 180);
pen.fillArc(100, 175, 200, 75, 90, 45);
}
}
Here's what I get after making your code runnable, fixing your JFrame method calls and fixing your drawing JPanel.
Swing applications should always start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
You pack a JFrame. You set the preferred size of your drawing JPanel. This way, you know how big your drawing JPanel is, without worrying about the JFrame decorations.
Here's the complete runnable code.
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawingPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawingPanelExample());
}
#Override
public void run() {
JFrame frame = new JFrame("My Empty Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(350, 300));
}
#Override
protected void paintComponent(Graphics pen) {
super.paintComponent(pen);
pen.drawRect(50, 50, 20, 20);
pen.drawRect(100, 50, 40, 20);
pen.drawOval(200, 50, 20, 20);
pen.drawOval(250, 50, 40, 20);
pen.drawString("Square", 50, 90);
pen.drawString("Rectangle", 100, 90);
pen.drawString("Cirlce", 200, 90);
pen.drawString("Oval", 250, 90);
pen.fillRect(50, 100, 20, 20);
pen.fillRect(100, 100, 40, 20);
pen.fillOval(250, 100, 20, 20);
pen.fillOval(250, 100, 40, 20);
pen.drawLine(50, 150, 300, 150);
pen.drawArc(50, 150, 200, 100, 0, 180);
pen.fillArc(100, 175, 200, 75, 90, 45);
}
}
}
Try changing the method in DrawingPanel from painting to paint, which will get called when run. paint is a method inherited from JPanel.
Edit: As mentioned by NomadMaker, use paintComponent() not paint() here. Read this for more information.
You should override paintComponent like so:
...
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D pen = (Graphics2D) g;
...
}
Also some suggestions:
You can extending JComponent instead of JPanel (It should work both ways)
You can use setSize or setPreferredSize for your panel to fit it with your frame size.
You can only use setVisisble(true); only once after all of the configurations of your frame.
And add it to the center of the frame like so:
...
JFrame frame = new JFrame();
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
DrawingPanel panel = new DrawingPanel();
panel.setPreferredSize(new Dimensions(350, 300));
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
...
On a side note:
Adding a layout manager may not be necessary and you can also replace setPreferredSize with setBounds like so:
panel.setBounds(0, 0, 350, 300);
frame.add(panel);
Where 0s are x and y coordinates respectively.

Overriding Method with MouseListener

I'm creating a Java program that uses Swing to draw a face, and then I am using MouseListener to respond to mouse clicks to make one of the eyes blink. How would I make one of the eyes blink using MouseListener? The method paint(Graphics g) can only be created once with that name, so if I want to repeat it and edit it under the MouseListener code with one of the eyes turned into a line for blinking, how would I do that?
Here is my code so far:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Sans extends JPanel {
public void paint(Graphics g) {
super.paintComponent(g);
setSize(650, 650);
g.drawOval(110, 250, 500, 275);
g.setColor(new Color(226, 222, 217));
g.fillOval(110, 250, 500, 275);
g.drawOval(475, 300, 75, 75);
g.setColor(new Color(74, 199, 226));
g.fillOval(475, 300, 75, 75);
g.drawOval(505, 330, 15, 15);
g.setColor(new Color(0, 0, 0));
g.fillOval(505, 330, 15, 15);
g.drawOval(175, 300, 75, 75);
g.setColor(new Color(0, 0, 0));
g.fillOval(175, 300, 75, 75);
g.drawOval(205, 330, 15, 15);
g.setColor(new Color(232, 255, 243));
g.fillOval(205, 330, 15, 15);
g.drawOval(350, 375, 20, 50);
g.setColor(new Color(0, 0, 0));
g.fillOval(350, 375, 20, 50);
g.drawArc(290, 360, 150, 150, 180, 180);
g.setColor(new Color(255, 255, 255));
g.fillArc(290, 360, 150, 150, 180, 180);
}
public static void main(String[] args) {
Font font = new Font("TimesRoman", Font.PLAIN, 15);
JFrame frame = new JFrame();
Sans spook = new Sans();
frame.add(spook);
frame.setSize(750, 750);
frame.setTitle("I'VE GOTTEN A TON OF WORK DONE TODAY. A SKELE-TON.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public class BlinkHandler implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
}

How to display a picture on JFrame?

I'm doing this exercise where I'm supposed to write a program that simulates a race between two cars.
I've created a JFrame and added two rectangles that are supposed to be the tracks.
But I can't insert the cars. I have googled and tried some solutions but it just doesn not work out.
Here is my code.
public class Race extends JComponent {
private ImageIcon image;
public void paint(Graphics g) {
g.setColor(Color.GRAY);
g.fill3DRect(30, 150, 530, 55,true);
g.setColor(Color.GRAY);
g.fill3DRect(30, 250, 530, 55, true);
g.setColor(Color.BLACK);
g.fill3DRect(90, 130, 12, 189, true);
}
public static void main(String[] a) {
JFrame window = new JFrame();
window.setPreferredSize(new Dimension(600, 400));
window.getContentPane().setBackground(Color.GREEN);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new Race());
window.pack();
window.setVisible(true);
}
}
Where and how can I add two pictures?
Thanks
This is how you would add a image into your JFrame
frame.add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));

why when i add a for loop my jframe turns black?

I am trying to make a flashing light in my jframe by creating a list of the colors and then cycling through them with a for loop and then repainting. but when I add a for loop to my code the whole thing bugs out and I get a black screen and it frezzes. Why is this happening?
public class bb {
static Color colors[] = {Color.ORANGE, Color.GRAY};
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400, 525);
JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
JButton smallerButton = new JButton("Flash");
JButton largerButton = new JButton("Steady");
JPanel southPanel = new JPanel();
southPanel.add(smallerButton);
southPanel.add(largerButton);
frame.add(southPanel, BorderLayout.SOUTH);
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(180, 110, 10, 30);
g.drawRect(180, 140, 9, 30);
g.fillRect(180, 170, 10, 30);
g.drawRect(180, 200, 9, 30);
g.fillRect(180, 230, 10, 30);
g.drawRect(180, 260, 9, 30);
g.fillRect(180, 290, 10, 30);
g.drawRect(180, 310, 9, 30);
g.fillRect(180, 340, 10, 30);
int i = 0;
g.setColor(colors[i]);
for(i=0; i <= colors.length; i++){
g.fillOval(160, 70, 50, 50);
if (i ==colors.length){
i=0;
}
frame.repaint();
}
smallerButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if (action.equals("Flash")){
}
}
});
}
};
frame.add(panel);
frame.validate();
}
}
This statement resets your loop index to 0 causing it to loop indefinitely blocking the EDT
if (i == colors.length) {
i = 0;
}
since you exceed the last array index in the for statement.
Take a look at using a Swing Timer to achieve this functionality.

Draw line after a button pressed in java applet

When i tried this following error came
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
at ButtonTest.actionPerformed(ButtonTest.java:58)
import java.awt.*;
import java.awt.event.*;//step-1
import java.applet.Applet;
public class ButtonTest extends Applet implements ActionListener//step-2
{
Button b1,b2,b3;
Font f;
Graphics gc;
public void init()
{
b1=new Button("Request");
b2=new Button("Grant");
b3=new Button("Accept");
f=new Font("Arial",Font.BOLD,12);
b1.setFont(f);
b2.setFont(f);
b3.setFont(f);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1);
add(b2);
add(b3);
}
public void paint(Graphics gc)
{
gc.drawLine(100, 150, 100, 400);
gc.drawLine(300, 150, 300, 400);
gc.drawOval(95, 155, 10, 10); //1.1
gc.drawOval(95, 225, 10, 10); //1.2
gc.drawOval(95, 295, 10, 10); //1.3
gc.drawOval(95, 365, 10, 10); //1.4
gc.drawOval(295, 155, 10, 10); //2.1
gc.drawOval(295, 225, 10, 10); //2.2
gc.drawOval(295, 295, 10, 10); //2.3
gc.drawOval(295, 365, 10, 10); //2.4
}
public void myPaint(Graphics gc) // this line is not working*******???????
{
gc.drawLine(95, 155, 295, 225); //1.1 to 2.2
gc.drawLine(95, 295, 295, 225); //1.3 to 2.2
gc.drawLine(95, 295, 295, 365); //1.3 to 2.4
gc.drawString(">>>", 260, 220);
gc.drawString(">>>", 218, 255);
gc.drawString(">>>", 267, 365);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
myPaint(gc); //this line is not working
setBackground(Color.red);
}
else if(ae.getSource()==b2)
{
setBackground(Color.green);
}
else{
setBackground(Color.blue);
}
}
}
/*<applet code="ButtonTest" width=300 height=300>
*/
error is
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
at ButtonTest.actionPerformed(ButtonTest.java:58)
There a few mistakes in your code. After you have edited your code the line
public void myPaint(Graphics gc)
works. The line
myPaint();
does not work because myPaint needs an argument of type Graphics. The method call should look like
myPaint(gc);
I don't know what you really want to do, but you can get the canvas of the applet by calling
this.getGraphics()
There is no need to use gc as an argument in myPaint or to store Graphic as a member in inside your class.
Hope that helps

Categories