Draw line after a button pressed in java applet - java

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

Related

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

Why is the JButton btn[3][5] not getting placed on the position given in the setBounds() call? [duplicate]

This question already has answers here:
What is setBounds and how do I use it?
(7 answers)
Closed 3 years ago.
I am coding a program wich should contain multiple JButtons set next to each other. At first I wanted a 4 by 6 Grid. I have 3 classes: a Main clas containing the main method and a constructor call of the Gui class constructor. The Gui class should include all JObjects (JButtons) + the JFrame setup. Then there is a third ButtonPlacement class wich includes the setBounds method calls. As I ran the Code in Eclipse ervery Button in my Button Array was placed at the right position except the last one: btn[3][5]. It is as big as the whole JFrame.
class Main:
package pack1;
public class Main {
public static void main(String[] args) {
new Gui();
}
}
class Gui:
package pack1;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import pack1.ActionHandler;
import pack1.ButtonPlacement;
public class Gui {
static JFrame jf;
static JButton btn[][] = new JButton[4][6];
JButton btnReset;
public Gui() {
jf = new JFrame();
jf.setSize(500, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLocationRelativeTo(null);
for(int i = 0; i<4;++i) {
for(int j = 0; j<6; ++j) {
btn[i][j] = new JButton("btn" + i + j);
btn[i][j].setVisible(true);
btn[i][j].addActionListener(new ActionHandler());
btn[i][j].setFocusPainted(false);
btn[i][j].setContentAreaFilled(true);
btn[i][j].setBorder(null);
btn[i][j].setFont(new Font("Century Gothic", Font.PLAIN, 20));
jf.add(btn[i][j]);
}
}
ButtonPlacement.place();
jf.setVisible(true);
}
}
class ButtonPlacement:
package pack1;
import pack1.Gui;
public class ButtonPlacement {
public static void place() {
Gui.btn[0][0].setBounds(0, 140, 100, 60);
Gui.btn[1][0].setBounds(100, 140, 100, 60);
Gui.btn[2][0].setBounds(200, 140, 100, 60);
Gui.btn[3][0].setBounds(300, 140, 100, 60);
Gui.btn[0][1].setBounds(0, 200, 100, 60);
Gui.btn[1][1].setBounds(100, 200, 100, 60);
Gui.btn[2][1].setBounds(200, 200, 100, 60);
Gui.btn[3][1].setBounds(300, 200, 100, 60);
Gui.btn[0][2].setBounds(0, 260, 100, 60);
Gui.btn[1][2].setBounds(100, 260, 100, 60);
Gui.btn[2][2].setBounds(200, 260, 100, 60);
Gui.btn[3][2].setBounds(300, 260, 100, 60);
Gui.btn[0][3].setBounds(0, 320, 100, 60);
Gui.btn[1][3].setBounds(100, 320, 100, 60);
Gui.btn[2][3].setBounds(200, 320, 100, 60);
Gui.btn[3][3].setBounds(300, 320, 100, 60);
Gui.btn[0][4].setBounds(0, 380, 100, 60);
Gui.btn[1][4].setBounds(100, 380, 100, 60);
Gui.btn[2][4].setBounds(200, 380, 100, 60);
Gui.btn[3][4].setBounds(300, 380, 100, 60);
Gui.btn[0][5].setBounds(0, 440, 100, 60);
Gui.btn[1][5].setBounds(100, 440, 100, 60);
Gui.btn[2][5].setBounds(200, 440, 100, 60);
Gui.btn[3][5].setBounds(300, 440, 100, 60);
}
}
I expect the Button btn[3][5] at its position 300, 440 with its size 100, 60, but the button is as big as the JFrame.
Adding this to the class Gui let it work properly for me:
jf.setLayout(null);

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.

Creating a Bar graph on JPanel

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.

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