Overriding Method with MouseListener - java

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) {
}
}
}

Related

Add buttons that change color of drawing

The drawing is basically a two-leaf window with sky and sun as if you're looking out that window.
I need to add buttons Day and Night. By clicking each button the color of sky changes to dark blue and light blue, the color of the sun changes to color of the moon correspondingly. I have added the buttons and they appear but I can't seem to wrap my head around the way where I should place my actionListener and how to get access to my g2 variable in order to change those colors.
public class DrawingOne extends JFrame{
public DrawingOne() {
Container container = getContentPane();
container.setBackground(new Color(224, 196, 214));
container.setLayout(new BorderLayout(20, 20));
JButton j1 = new JButton("Day");
JButton j2 = new JButton("Night");
container.add(j1, BorderLayout.SOUTH);
container.add(j2, BorderLayout.NORTH);
setSize(DEFAULT_WITDH, DEFAULT_HEIGHT);
DrawComponent c = new DrawComponent();
add(c);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static final int DEFAULT_WITDH = 700;
public static final int DEFAULT_HEIGHT = 500;
}
class DrawComponent extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//the base for window
Rectangle2D.Double rectangle2Drect2 = new Rectangle2D.Double(37, 26, 458, 280);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
//left half-window
rectangle2Drect2 = new Rectangle2D.Double(51, 40, 160, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(new Color(135, 206, 235));
g2.fill(rectangle2Drect2);
//right half-window
rectangle2Drect2 = new Rectangle2D.Double(230, 40, 250, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(new Color(135, 206, 235));
g2.fill(rectangle2Drect2);
//основа ручки
rectangle2Drect2 = new Rectangle2D.Double(215, 140, 10, 10);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
//ручка
rectangle2Drect2 = new Rectangle2D.Double(217, 142, 4, 30);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
//the sun
g2.setColor(Color.getHSBColor(197.4F, 42.6F, 92.2F));
g2.fillOval(100, 60, 80, 80);
//горшок
int[] xpoints = {230 + 60, 300 + 60, 285 + 61, 244 + 60};
int[] ypoints = {240, 240, 290, 290};
int npoints = 4;
g2.setColor(new Color(172, 86, 30));
g2.fillPolygon(xpoints, ypoints, npoints);
//ствол цветка
g2.setStroke(new BasicStroke(5));
g2.setColor(new Color(0, 128, 0));
g2.drawLine(325, 200, 325, 238);
//лепестки
g2.setColor(new Color(255, 192, 203));
g2.fillOval(317, 200, 22, 22);
g2.fillOval(317, 175, 22, 22);
g2.fillOval(317 - 15, 190, 22, 22);
g2.fillOval(317 + 13, 190, 22, 22);
// g2.fillOval(317,190,22,22);
//центр цветка
g2.setColor(new Color(255, 165, 0));
g2.fillOval(317, 190, 15, 15);
}
public static void main(String[] args) {
DrawingOne frame = new DrawingOne();
}
}
I made a night sky from your GUI.
I had to make quite a few changes to your code to get this to work.
I had to make the JComponent class an inner class, so it could reference DrawingOne class variables.
I had to move the main method in the DrawingOne class, so I could start the application.
I started the Java application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I rearranged the order of your statements in the DrawingOne constructor, so they made more sense. I made the drawing panel your preferred size, rather than the JFrame. The size of the drawing panel is what's important, not the JFrame size.
I made the sky and sun colors DrawingOne class variables, so they could be changed. I wrote a couple of methods to change the colors.
I wrote an ActionListener class that changes the colors depending on which button you left-click.
Here's the complete runnable code.
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class DrawingOne extends JFrame {
private static final long serialVersionUID = 1L;
public static final int DEFAULT_WIDTH = 700;
public static final int DEFAULT_HEIGHT = 500;
public Color sunColor;
public Color skyColor;
public DrawComponent c;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new DrawingOne();
}
});
}
public DrawingOne() {
setDaySky();
Container container = getContentPane();
container.setBackground(new Color(224, 196, 214));
container.setLayout(new BorderLayout(20, 20));
container.setPreferredSize(
new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
ButtonListener listener = new ButtonListener();
JButton j1 = new JButton("Day");
j1.addActionListener(listener);
JButton j2 = new JButton("Night");
j2.addActionListener(listener);
container.add(j1, BorderLayout.SOUTH);
container.add(j2, BorderLayout.NORTH);
c = new DrawComponent();
container.add(c, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void setNightSky() {
this.sunColor = Color.getHSBColor(197.4F, 42.6F, 92.2F);
this.skyColor = new Color(7, 78, 107);
}
public void setDaySky() {
this.sunColor = Color.getHSBColor(197.4F, 42.6F, 92.2F);
this.skyColor = new Color(135, 206, 235);
}
public class DrawComponent extends JComponent {
private static final long serialVersionUID = 1L;
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// the base for window
Rectangle2D.Double rectangle2Drect2 =
new Rectangle2D.Double(37, 26, 458, 280);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
// left half-window
rectangle2Drect2 =
new Rectangle2D.Double(51, 40, 160, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(skyColor);
g2.fill(rectangle2Drect2);
// right half-window
rectangle2Drect2 =
new Rectangle2D.Double(230, 40, 250, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(skyColor);
g2.fill(rectangle2Drect2);
// основа ручки
rectangle2Drect2 =
new Rectangle2D.Double(215, 140, 10, 10);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
// ручка
rectangle2Drect2 =
new Rectangle2D.Double(217, 142, 4, 30);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
// the sun
g2.setColor(sunColor);
g2.fillOval(100, 60, 80, 80);
// горшок
int[] xpoints = { 230 + 60, 300 + 60, 285 + 61, 244 + 60 };
int[] ypoints = { 240, 240, 290, 290 };
int npoints = 4;
g2.setColor(new Color(172, 86, 30));
g2.fillPolygon(xpoints, ypoints, npoints);
// ствол цветка
g2.setStroke(new BasicStroke(5));
g2.setColor(new Color(0, 128, 0));
g2.drawLine(325, 200, 325, 238);
// лепестки
g2.setColor(new Color(255, 192, 203));
g2.fillOval(317, 200, 22, 22);
g2.fillOval(317, 175, 22, 22);
g2.fillOval(317 - 15, 190, 22, 22);
g2.fillOval(317 + 13, 190, 22, 22);
// g2.fillOval(317,190,22,22);
// центр цветка
g2.setColor(new Color(255, 165, 0));
g2.fillOval(317, 190, 15, 15);
}
}
public class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("Night")) {
setNightSky();
} else {
setDaySky();
}
c.repaint();
}
}
}

Unable to load a Jbutton to JFrame

I am trying to add a button to my form to bring the user back to the previous screen. I can display the form and drawn objects but unable to get the button to display.
Any help would be greatly appreciated
public static JButton mainMenue = new JButton("Main Menue");
public static void main(String[] args)
{
//creating and setting the values for the Jframe
JFrame form = new JFrame();
Color backColour = new Color(153,153,153);
form.getContentPane().setBackground(backColour);
form.add(mainMenue);
mainMenue.setVisible(true);
mainMenue.setLocation(100,100);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setBounds(30, 30, 1000, 1000);
form.getContentPane().add(new Layout());
form.setVisible(true);
}
#Override
public void paint(Graphics g)
{
//creating the seating
Font titleFont = new Font("Dialog", Font.BOLD, 36);
Font subTitleFont = new Font("Dialog", Font.BOLD, 14);
Graphics2D g2 = (Graphics2D) g;
super.paint(g);
g.setColor(Color.blue);
g.drawLine(225, 60, 255, 60);
g.drawLine(340, 60, 400, 60);
g.drawLine(400, 60, 400, 135);
g.drawLine(400, 135, 650, 135);
g.drawLine(650, 135, 650, 60);
g.drawLine(650, 60, 680, 60);
g.drawLine(765, 60, 825, 60);
g.drawLine(225, 600, 450, 600);
g.drawLine(600, 600, 850, 600);

How to draw a mouth for a Smiley Face in Java

How do I get the mouth part of the smiley face? Do I use a polygon or oval...oval doesn't seem to make sense but I don't know?
here is my code:
import java.awt.Color;
import java.awt.Canvas;
import java.awt.Graphics;
public class HappyFace extends Canvas {
public HappyFace() {
setBackground(Color.BLACK);
}
public void paint(Graphics window) {
window.setColor(Color.YELLOW);
window.fillOval(250, 150, 350, 320);
window.setColor(Color.MAGENTA);
window.fillOval(300, 220, 90, 100);
window.fillOval(450, 220, 90, 100);
window.setColor(Color.WHITE);
window.drawOval(380, 320, 90, 100);
window.setColor(Color.GREEN);
}
}
Maybe a drawArc(...)
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class FaceComponent extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawArc(100, 45, 80, 80, 0, 360);
g.setColor( Color.blue );
g.drawArc( 120, 70, 10, 10, 0, 360);
g.drawArc( 150, 70, 10, 10, 0, 360);
g.setColor( Color.magenta );
g.drawLine ( 140, 85, 140, 100 );
g.setColor( Color.red );
g.drawArc ( 110, 55, 60, 60, 0, -180 );
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(250, 250);
}
private static void createAndShowGUI()
{
JComponent face = new FaceComponent();
face.setForeground(Color.GREEN);
// face.setBackground(Color.YELLOW);
JPanel contentPane = new JPanel( new BorderLayout() );
contentPane.setBackground( Color.CYAN );
contentPane.add( face );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane( contentPane );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
you are going to want to put an inverted arc or you will have a frowning face so you want to add
g.drawArc.invert(110, 55, 60, 60, 0, -180)
` import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class FaceComponent extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawArc(100, 45, 80, 80, 0, 360);
g.setColor( Color.blue );
g.drawArc( 120, 70, 10, 10, 0, 360);
g.drawArc( 150, 70, 10, 10, 0, 360);
g.setColor( Color.magenta );
g.drawLine ( 140, 85, 140, 100 );
g.setColor( Color.red );
g.drawArc ( 110, 55, 60, 60, 0, -180 );
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(250, 250);
}
private static void createAndShowGUI()
{
JComponent face = new FaceComponent();
face.setForeground(Color.GREEN);
// face.setBackground(Color.YELLOW);
JPanel contentPane = new JPanel( new BorderLayout() );
contentPane.setBackground( Color.CYAN );
contentPane.add( face );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane( contentPane );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
`

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

How to make Timer countdown along with progress bar?

How can I make it so that the progress bar slowly goes down with the time limit?
class GamePanel extends JPanel implements MouseListener, ActionListener
{
private JButton quit;
private JButton q;
private Font loadFont;
public GamePanel()
{
setBackground(Color.blue); // sets background color
this.setLayout(null);
quit = new JButton("Quit");
quit.addActionListener(this);
quit.setBounds(550, 700, 100, 30);
this.add(quit);
q = new JButton("Questions");
q.addActionListener(this);
q.setBounds(100, 100, 120, 30);
this.add(q);
loadFont = new Font("Serif", Font.PLAIN, 30);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(80, 100, 610, 560);
g.setColor(Color.white);
g.fillRect(90, 110, 110, 100);// 1st column
g.fillRect(90, 220, 110, 100);//
g.fillRect(90, 330, 110, 100);//
g.fillRect(90, 440, 110, 100);//
g.fillRect(90, 550, 110, 100);//
g.fillRect(210, 110, 110, 100);// 2nd column
g.fillRect(210, 220, 110, 100);//
g.fillRect(210, 330, 110, 100);//
g.fillRect(210, 440, 110, 100);//
g.fillRect(210, 550, 110, 100);//
g.fillRect(330, 110, 110, 100);// 3rd column
g.fillRect(330, 220, 110, 100);//
g.fillRect(330, 330, 110, 100);//
g.fillRect(330, 440, 110, 100);//
g.fillRect(330, 550, 110, 100);//
g.fillRect(450, 110, 110, 100);// 4th column
g.fillRect(450, 220, 110, 100);//
g.fillRect(450, 330, 110, 100);//
g.fillRect(450, 440, 110, 100);//
g.fillRect(450, 550, 110, 100);//
g.fillRect(570, 110, 110, 100);// 5th column
g.fillRect(570, 220, 110, 100);//
g.fillRect(570, 330, 110, 100);//
g.fillRect(570, 440, 110, 100);//
g.fillRect(570, 550, 110, 100);//
g.setColor(Color.green);
g.setFont(loadFont);
g.drawString(input + ":", 100, 710);
}
public void actionPerformed(ActionEvent e)
{
String order = e.getActionCommand();
if(order.equals("Quit"))
cards.show(c, "Introduction");
if(order.equals("Questions"))
cards.show(c, "Questions");
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
class QuestionPanel extends JPanel implements ActionListener
{
private long startTime, elapsedTime;
private Timer timer;
private int countdown;
private Font loadFont;
public QuestionPanel()
{
setBackground(Color.pink); // sets background color
this.setLayout(null); // moved into constructor from ActionPerformed: only change layout in constructor
startTime = 0;
elapsedTime = 0;
countdown = 590;
loadFont = new Font("Segoe Script", Font.BOLD, 20);
if(timer == null)
{// use the biggest value possible that provides your desired time keeping precision (usually no less than 15 on Windows)
timer = new Timer(100, this);
startTime = System.currentTimeMillis(); // gets start time in milliseconds
timer.start();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(100, 100, 600, 25);
g.setColor(Color.green);
g.fillRect(105, 105, countdown, 15);
g.setColor(Color.black);
g.setFont(loadFont);
g.drawString("" + ((System.currentTimeMillis() - startTime) / 1000.0), 100, 80); // display remaining time
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
elapsedTime = System.currentTimeMillis() - startTime;
if(elapsedTime < (5000))
{
countdown--;
repaint();
}
else
{
timer.stop();
if(timer == null)
{
timer = new Timer(500, this);
timer.start();
}
}
if(elapsedTime >= (5000)) // can't use == here because of limited precision of system clock
cards.show(c, "Correct!");
}
}
class AnswerPanel extends JPanel implements ActionListener
{
private JButton revert;
public AnswerPanel()
{
setBackground(Color.yellow); // sets background color
this.setLayout(null);
revert = new JButton("Back");
revert.addActionListener(this);
revert.setBounds(340, 700, 100, 30);
this.add(revert);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void actionPerformed(ActionEvent e)
{
String directive = e.getActionCommand();
if(directive.equals("Back"))
cards.show(c, "Start");
}
}
class FailPanel extends JPanel implements ActionListener
{
private JButton turnaround;
public FailPanel()
{
setBackground(Color.green); // sets background color
this.setLayout(null);
turnaround = new JButton("Back");
turnaround.addActionListener(this);
turnaround.setBounds(340, 700, 100, 30);
this.add(turnaround);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void actionPerformed(ActionEvent e)
{
String bidding = e.getActionCommand();
if(bidding.equals("Back"))
cards.show(c, "Start");
}
}
}// end of the entire program
Sorry, I still could not find the motivation to actually read your code, but just threw together this example based on the question. See if it gives you some ideas.
Note that it is an SSCCE and uses just 40 lines of code in all.
import java.awt.event.*;
import javax.swing.*;
class CountDownProgressBar {
Timer timer;
JProgressBar progressBar;
CountDownProgressBar() {
progressBar = new JProgressBar(JProgressBar.VERTICAL, 0, 10);
progressBar.setValue(10);
ActionListener listener = new ActionListener() {
int counter = 10;
public void actionPerformed(ActionEvent ae) {
counter--;
progressBar.setValue(counter);
if (counter<1) {
JOptionPane.showMessageDialog(null, "Kaboom!");
timer.stop();
}
}
};
timer = new Timer(1000, listener);
timer.start();
JOptionPane.showMessageDialog(null, progressBar);
}
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
CountDownProgressBar cdpb = new CountDownProgressBar();
}
});
}
}
From the looks of it, all of this code is within a big Java file? That is a bad idea.
You should have a good reason to define a class as an inner class, and from the looks of it, you do not have one for QuestionPanel and others.
As for the problem, your paintComponent method is called every time your counter is updated, which is right now roughly once every 0.1 seconds, yet you only tick by 1 pixel on each update, so by the end of 5 seconds, you've cut off 10*5 pixels (50). What you should do is update the progress bar by a different mechanism, such as a calculating the current time processed:
long processed = System.currentTimeMillis() - startTime;
double percent = Math.max(0, 1 - processed / 5000.0);
int width = (int)(590 * percent);
That is definitely too much information, and a very broad question. I'd say at most you only need to include the code for the class where the timer is, and the class where the progress bar gets drawn.
From skimming the code, I'm guessing you're using a rectangle to draw the progress bar. Based on that, one way you could go about it would be using a variable to store the width of the bar, and every time the timer ticks, decrease the width of the bar by a set amount. Then just set the width of the rectangle drawn to the value stored in the variable.

Categories