I have been making an applet for a project for my school but I stumbled upon a nasty error. My problem is probably quite common but I can't seem to find an answer for it. I have been getting the error:
The type Pyramid must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
but I have already implemented the method in there and it's spelling is correct. I checked so many times that my head hurts. My project has frozen at this process for like 3 hours now and I have no idea how to get it to work. Oh and also do you think that using 3 polygons to draw a pyramid is the best way to do it in java?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Pyramid extends Applet implements ActionListener {
int n = 3; // vurhove
double a, k;
double Sok, S, V;
public void init()
{
setLayout(null);
TextField aT; // textbox za osn. rub
aT = new TextField("Osnoven rub", 20);
aT.setBounds(20, 410, 125, 20);
TextField lT; // textbox za ok. rub
lT = new TextField("Okolen rub", 20);
lT.setBounds(20, 460, 125, 20);
TextField SokT; // textbox S ok.
SokT = new TextField("Okolna povurhnina", 20);
SokT.setBounds(20, 510, 125, 20);
TextField ST; // textbox S1
ST = new TextField("Osnovna povurhnina", 20);
ST.setBounds(20, 560, 125, 20);
TextField VT; // textbox za obema
VT = new TextField("Obem", 20);
VT.setBounds(20, 610, 125, 20);
Button ochButton; // izchertava piramidata
ochButton = new Button("Izchertai");
ochButton.setBounds(700, 435, 100, 30);
ochButton.addActionListener(ActionOchButton);
Button oznButton; // ozna4ava vurhovete, izpisva velichinite
oznButton = new Button("Oznachi cherteja");
oznButton.setBounds(700, 510, 100, 30);
oznButton.addActionListener(ActionOznButton);
Button iButton; // iz4islqva veli4inite
iButton = new Button("Izchisli");
iButton.setBounds(700, 585, 100, 30);
iButton.addActionListener(ActioniButton);
add(aT);
add(lT);
add(SokT);
add(ST);
add(VT);
add(ochButton);
add(oznButton);
add(iButton);
Color bg;
bg = new Color(168,168,168);
setBackground(bg);
}
ActionListener ActionOchButton = new ActionListener() {
public void actionPerformed(ActionEvent e){
Graphics g = getGraphics();
int px[] = {340, 440, 490};
int py[] = {235, 335, 235};
g.drawPolygon(px, py, 3);
int px1[] = {340, 415, 490};
int py1[] = {235, 60, 235};
g.drawPolygon(px1, py1, 3);
int px2[] = {440, 415, 490};
int py2[] = {335, 60, 235};
g.drawPolygon(px2, py2, 3);
}
};
ActionListener ActionOznButton = new ActionListener() {
public void actionPerformed(ActionEvent e){
}
};
ActionListener ActioniButton = new ActionListener() {
public void actionPerformed(ActionEvent e){
}
};
public void paint(Graphics g)
{
g.setColor(Color.white);
g.drawRect(20, 20, 780, 370);
}
}
You have not implemented the method, plain and simple.
You need to have that method defined in your class. You're adding a bunch of other action listeners, but nowhere in your class do you implement the required method.
To clarify further, on the same level as your init() method, you need a public void actionPerformed(ActionEvent e) { /* ... */ } method.
You have not impleented ActionListener.actionPerformed(ActionEvent) in Pyramid, only in the three anonymous instances of ActionListener. For Pyramid to implement ActionListener, you must implement this method in th class, not in some local variable.
You should implement the method in your class. For example, here it is at the end of your Pyramid class:
#Override
public void actionPerformed(ActionEvent e) {
}
} //End of Pyramid class
Now, in this actionPerformed() method, you can determine which button was clicked and do the appropriate action. But you'll have to change a few things:
//make these instance variables
Button ochButton; // izchertava piramidata
Button oznButton; // ozna4ava vurhovete, izpisva velichinite
Button iButton; // iz4islqva veli4inite
Add action listener this way...
ochButton.addActionListener(this);
oznButton.addActionListener(this);
iButton.addActionListener(this);
Implement actionPerformed()...
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ochButton) {
System.out.println("ochButton clicked");
} else if (e.getSource() == oznButton) {
System.out.println("oznButton clicked");
} else if (e.getSource() == iButton) {
System.out.println("iButton clicked");
}
}
Regarding drawing a pyramid, perhaps you can draw the base triangle and the top point. Then draw lines from the top point to each point of the triangle.
Related
Hi people of StackOverflow, I'm fairly new to Java and need some help,
I have made a program that compiles how I want it, a guessing game whereby the computer randomly generates a number and the user is asked to guess it, this is all in the console however. I wish to create a GUI for the game, I have made the outline of the GUI in code but I am struggling to put the logic into it, am I better off calling the methods from the console program (and make some changes) or start from the top within in the GUI code and put the logic in there? If the latter, where do i put the logic? Any help would be greatly appreciated!
Here are my two programs so far, both compile but the GUI is kind of meaningless right now!
Thank you in advance
This code is the console one, the user's guess is scanned and compared with the random number.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class GuessingGameSystem {
public static int randomizer() {
Random rand = new Random();
int num = rand.nextInt(10)+1;
System.out.println(num);
return num;
}
public static int userInput() {
System.out.println("I've thought of a number between 1 and 10");
System.out.println("Enter your guess...");
Scanner scan = new Scanner(System.in);
int guess = scan.nextInt();
return guess;
}
public static String compare(int a, int b) {
String result = null;
if (a < b) {
result = "Higher!";
}
else if (a > b) {
result = "Lower!";
}
else {
result = "You guessed it - I was thinking of " + b;
}
return result;
}
public static void main(String[] args) {
Scanner scanLine = new Scanner(System.in);
String playAgain = "";
int count = 0;
int a;
int b;
do {
b= randomizer();
count = 0;
ArrayList<Integer> guessesSoFar = new ArrayList<>();
do {
a = userInput();
count++;
guessesSoFar.add(a);
System.out.println("Guesses so far: " + Arrays.toString(guessesSoFar.toArray()));
System.out.println("Number of guesses so far: " + guessesSoFar.size());
System.out.println(compare(a,b));
} while (a != b);
System.out.println("It took you " + count + " guesses.");
System.out.println("Play again? Yes/No");
playAgain = scanLine.nextLine();
} while (playAgain.equalsIgnoreCase("yes") || playAgain.equalsIgnoreCase("y"));
}
}
And here is the GUI code I have so far:
import javax.swing.*;
import java.awt.event.*;
public class GuessingGameGUI {
private JFrame frame;
private JPanel panel;
private JLabel lblInstructions, lblResult, lblGuesses, lblNoOfGuesses;
private JButton btnCheck, btnNewGame, btnExit;
private JTextField txtUserGuess, txtListOfGuesses, txtGuessesCount;
public static void main(String[] args) {
new GuessingGameGUI();
}
public GuessingGameGUI() {
createForm();
createFields();
createButtons();
createTextField();
frame.add(panel);
frame.setVisible(true);
}
public void createForm() {
frame = new JFrame();
frame.setTitle("Guessing Game");
frame.setSize(800,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
}
public void createFields() {
lblInstructions = new JLabel("Guess the number between 1 and 10:");
lblInstructions.setBounds(285, 50, 350, 20);
panel.add(lblInstructions);
lblResult = new JLabel("Result");
lblResult.setBounds(380, 200, 100, 20);
panel.add(lblResult);
lblGuesses = new JLabel("Your Guesses:");
lblGuesses.setBounds(70, 50, 100, 20);
panel.add(lblGuesses);
lblNoOfGuesses = new JLabel("Number of Guesses:");
lblNoOfGuesses.setBounds(600, 50, 200, 20);
panel.add(lblNoOfGuesses);
}
public void createButtons() {
btnCheck = new JButton("Check");
btnCheck.setBounds(325, 150, 150, 20);
panel.add(btnCheck);
btnNewGame = new JButton("New Game");
btnNewGame.setBounds(160, 300, 150, 20);
panel.add(btnNewGame);
btnExit = new JButton("Exit");
btnExit.setBounds(495, 300, 150, 20);
panel.add(btnExit);
btnCheck.addActionListener(new CheckHandler());
btnNewGame.addActionListener(new NewGameHandler());
btnExit.addActionListener(new ExitHandler());
}
public void createTextField() {
txtUserGuess = new JTextField();
txtUserGuess.setBounds(300, 100, 200, 20);
panel.add(txtUserGuess);
txtListOfGuesses = new JTextField("List of Guesses");
txtListOfGuesses.setBounds(65, 75, 115, 200);
panel.add(txtListOfGuesses);
txtGuessesCount = new JTextField("Guesses Count");
txtGuessesCount.setBounds(610,75, 110, 20);
panel.add(txtGuessesCount);
}
class CheckHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
//to check random number against user guess in txtUserGuess
}
}
class NewGameHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
//to start a new game
}
}
class ExitHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
}
You should have a look at MVP (Model View Presenter).
GUI code shouldn't have the game's logic – you shouldn't call methods that define the game's behaviour, only pass user commands forward.
Here's the outline of a possible way of implementing it using MVP (A > B means A calls B's method; the GUI is the View):
User writes a number
User presses "Check"
View > Presenter: onCheckCommand(int number)
Presenter > Model: receiveGuess(int number)
Model adds the received number to a list of guesses
Model returns HIGHER, LOWER or RIGHT
Presenter > Model: getListOfGuesses() and getGuessesCount()
Presenter > View: showListOfGuesses(List<Integer> list), showGuessesCount(int count) and showResult(Result result) (HIGHER, LOWER or RIGHT)
View: If the result is RIGHT, disable the "Check" button
User presses "New Game"
View enables the "Check" button and clears the result, list of guesses and guesses count
View > Presenter: onNewGameCommand()
Presenter > Model: startNewGame()
Model clears the list of guesses and generates a new number
Basically, the presenter is who knows which game methods should be called (and when) and what should be displayed (and when).
Model should have a public method for anything that happens (in the game) in response to user input or other external events, if any – for example, a timer that fires every second to update the remaining time.
I'm assuming the view forces the user to input integers. You should be careful to call methods that update the GUI on the event dispatching thread. This already happens in this example, since all methods are called when handling the "button pressed" events. Calling everything from the handlers doesn't always work.
Okay, I want to create a Tic Tac Toe(X-O) game with GUI, I have drawn it but I'm somehow unable to function it. I do know how to use .addActionListener, and I can set a certain button to "X" or "O", but how do I make it so the first button click turns it to X, the second to O and so on. .addActionListener wont work properly because I have to use it on a certain button, but I don't know which button will be pressed.
Here's the code I've written.
public class XO {
public static void main(String[] args) {
JFrame myForm=new JFrame("X-O");
myForm.setSize(255, 300);
myForm.setLocation(0, 0);
JButton []buttons=new JButton[10];
int x=40, y=0;
for(int i=0;i<10;i++)
{
if(i%3==0)
{
y+=50; x=40;
}
buttons[i]=new JButton((i+1)+"");
buttons[i].setSize(50, 50);
buttons[i].setLocation(x, y);
myForm.add(buttons[i]);
x+=50;
}
buttons[9].setText("Start Over");
buttons[9].setLocation(80, 205);
buttons[9].setSize(70, 50);
buttons[9].setMargin(new Insets(0, 0, 0, 0));
buttons[0].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
buttons[0].setText("X");
buttons[0].setForeground(Color.BLUE);
buttons[0].setFont(new Font("Arial", Font.PLAIN, 30));
buttons[0].setMargin(new Insets(0, 0, 0, 0));
buttons[0].setEnabled(false);
}
});
myForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myForm.setLayout(null);
myForm.setVisible(true);
}
}
As you can see, I will have to use .setActionListener on a specific button, but I dont know which button will be pressed. I hope you get what I mean.
Thanks in advance.
Outside of your main method, you can declare a static counter to determine whose move it is:
private static int turnCount = 0;
Add the action listeners for each of your buttons within your loop, and determine what to set the button text as based on the counter. Be sure to increment your turnCount variable within the action listener:
for(int i=0;i<10;i++)
{
if(i%3==0)
{
y+=50; x=40;
}
buttons[i]=new JButton((i+1)+"");
buttons[i].setSize(50, 50);
buttons[i].setLocation(x, y);
int butNum = i;
buttons[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String currentPlayer = turnCount % 2 == 0 ? "X" : "O";
buttons[butNum].setText(currentPlayer);
buttons[butNum].setForeground(Color.BLUE);
buttons[butNum].setFont(new Font("Arial", Font.PLAIN, 30));
buttons[butNum].setMargin(new Insets(0, 0, 0, 0));
buttons[butNum].setEnabled(false);
turnCount++;
}
});
myForm.add(buttons[i]);
x+=50;
}
You can get the button that was pressed from the ActionEvent via the getSource method - just cast it to a JButton.
for (JButton button : buttons) { // one listener per button
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource(); // which button
button.setText(getPlayer()); // get X or O
button.setForeground(Color.BLUE);
button.setFont(new Font("Arial", Font.PLAIN, 30));
button.setMargin(new Insets(0, 0, 0, 0));
button.setEnabled(false);
togglePlayer(); // toggle between X and O
}
});
}
I moved the body of your code to a run method and added methods to get the player and toggle the player.
public static void main(String[] args) {
XO game = new XO();
game.run();
}
private String player = "X";
private String getPlayer() {
return player;
}
private void togglePlayer() {
player = "X".equals(player) ? "O" : "X";
}
public void run() {
JFrame myForm = new JFrame("X-O");
// rest of your code
// plus my actionListener changes
// ...
}
I have a small program, that creates a something, that could be considered a joystick. Everything works, but there is a glitch, I can't work out how to fix. If mouse moves too fast, the JLabel that I'm dragging around, sticks, as mouse has moved outside of the drawn box. I could increase the size of Jlabel, but then the "O" is offset too much from mouse. (I'd rather even decrease the size, but with this implementation, maximum mouse speed is too low).
Any ideas how to fix this?
This is the whole code, btw, anyone can go ahead and compile, to see what is the problem exactly, when mouse moves too fast.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass implements ActionListener, MouseListener, MouseMotionListener
{
int labelSize = 20;
int screenOffsetX = 58;
int screenOffsetY = 130;
JFrame frame;
JLabel xAxis;
JLabel move;
JLabel drag;
JLabel yAxis;
JLabel xAxisDrag;
JLabel yAxisDrag;
JLabel radio;
JLayeredPane panel;
Robot rob;
public static void main(String[] args)
{
new MainClass();
}
public MainClass()
{
frame = new JFrame("app");
frame.setLayout(null);
frame.setBounds(20, 20, 400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
move = new JLabel("Movement");
move.setBounds(150, 0, 100, 15);
frame.add(move);
xAxis = new JLabel("X");
xAxis.setBounds(100, 20, 50, 15);
frame.add(xAxis);
yAxis = new JLabel("Y");
yAxis.setBounds(200, 20, 50, 15);
frame.add(yAxis);
drag = new JLabel("Dragging");
drag.setBounds(150, 40, 100, 15);
frame.add(drag);
xAxisDrag = new JLabel("X");
xAxisDrag.setBounds(100, 60, 50, 15);
frame.add(xAxisDrag);
yAxisDrag = new JLabel("Y");
yAxisDrag.setBounds(200, 60, 50, 15);
frame.add(yAxisDrag);
radio = new JLabel("O");
radio.setBounds(0, 0, labelSize, labelSize);
radio.setOpaque(false);
radio.setEnabled(false);
panel = new JLayeredPane();
panel.setBounds(50, 100, 257, 257);
panel.setLayout(null);
panel.setBackground(new Color((float)1.0,(float)1.0,(float)1.0));
panel.setOpaque(true);
panel.add(radio);
radio.setLocation(128, 128);
frame.add(panel);
panel.addMouseMotionListener(this);
panel.addMouseListener(this);
frame.revalidate();
frame.repaint();
}
#Override
public void actionPerformed(ActionEvent e) {}
#Override
public void mouseDragged(MouseEvent e)
{
if(!(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257))
{
xAxis.setText("X: "+((e.getPoint().x/4)-32));
yAxis.setText("Y: "+((e.getPoint().y/4)-32));
}
if(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257)
{ //Neļauj iziet ārpus paneļa;
try
{
rob = new Robot();
if(e.getPoint().x<0)
rob.mouseMove((screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY()));
if(e.getPoint().y<0)
rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (screenOffsetY+frame.getY()));
if(e.getPoint().x>257)
rob.mouseMove((257+screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY()));
if(e.getPoint().y>257)
rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (257+screenOffsetY+frame.getY()));
}
catch (AWTException e1)
{
e1.printStackTrace();
}
}
if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) &&
(e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
)
{ //Ja kursors ir uz JLabel, tad pārvieto;
xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}
}
#Override
public void mouseMoved(MouseEvent e)
{
xAxis.setText("X: "+((e.getPoint().x/4)-32));
yAxis.setText("Y: "+((e.getPoint().y/4)-32));
}
#Override
public void mouseClicked(MouseEvent arg0) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e)
{
radio.setLocation(124, 124);
xAxisDrag.setText("X: "+0);
yAxisDrag.setText("Y: "+0);
}
}
Replace
if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) &&
(e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
)
{ //Ja kursors ir uz JLabel, tad pārvieto;
xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}
by:
if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) &&
(e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
)
{
//Has the mouse been clicked inside the radio before grabbing started ?
if(grab==0) //if it is the first iteration of the function mousedragged
grab=2; //proceed to "grab"
}
if(grab==2 && (e.getPoint().x>=0 && e.getPoint().x<panel.getWidth()) &&
(e.getPoint().y>=0 && e.getPoint().y<panel.getHeight())
)
{ //Ja kursors ir uz JLabel, tad pārvieto;
System.out.println(radio.getX()+" "+radio.getY());
xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}
if(grab!=2) // if grabbing is not started
grab=1; // first iteration is over
Update MouseReleased function to:
#Override
public void mouseReleased(MouseEvent e)
{
//this a new line
//grabbing is over
grab=0; // go back to idle state
radio.setLocation(124, 124);
xAxisDrag.setText("X: "+0);
yAxisDrag.setText("Y: "+0);
}
Finally , add a new field called grab to your class MainClass:
private int grab=0; //idle state
The meaning of grab:
1. grab=0 => It is the first time that the function MouseDragged is executed and "grabbing" is not started yet. In fact when you drag the mouse this function is iterated like an infinite loop until you release the mouse. So it is important to know when it has started.
2. grab=1 => Iteration has started but there is no "grabbing"
3. grab=2 => "grabbing" has started. You can notice the condition grab==0 before the affectation. In fact I tried to avoid the case when the mouse is dragged from somewhere else and goes through "radio" and then the radio is "grabbed". That's why I checked that it is the first iteration. In this case we have grab=1 => the radio is not grabbed
There are many questions about this, but they are all coming up with revalidate stuff.. my code works from within one class but doesnt if it gets triggered from another class.
I have a JPanel, i want to add a JPanel to it.
I have a method startGame() witch should remove the panel added to it currently.
The method startGame() works if i call it directly from the constructor.
The method startGame() does not work if i call it from another class.
public TopMenu topMenu;
public JPanel welcomeScreen;
public JPanel gameScreen;
public WelcomePanel() {
setLayout(null);
setSize(1000, 700);
init();
setBackground(Color.ORANGE);
}
public void init() {
topMenu = new TopMenu(this);
welcomeScreen = new WelcomeScreen();
gameScreen = new Game();
add(topMenu);
add(this.welcomeScreen);
}
public void startGame() {
remove(this.welcomeScreen);
add(gameScreen);
revalidate();
}
so if - after init - i would call startGame() it works, and it removes the welcomeScreen and adds gameScreen.
If i call that from an actionlistener in topMenu, it doesnt do anything. Neighter remove the old panel, or add the new one over the old panel.
Other classes:
WelcomeScreen
public class WelcomeScreen extends JPanel {
public WelcomeScreen()
{
setBounds(0, 50, GameBase.gameWidth, GameBase.gameHeight - 50);
setBackground(Color.GREEN);
}
}
Game
public class Game extends JPanel {
public Image background;
public Game()
{
background = new ImageIcon("src/game/images/mainMenu/MainMenu.png").getImage();
setBounds(50, 50, 700, 650);
setBackground(Color.GREEN);
}
public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, null);
}
}
TopMenu (basically: the actionlistener calls the startGame method on a button - tested and works.)
public class TopMenu extends JPanel implements ActionListener {
public topMenuButton playButton;
public topMenuButton forwardButton;
public topMenuButton menuButton;
public topMenuButton goToTheGameButton;
public Image background;
public static boolean playingNow = false;
public static boolean changingSettings = false;
public static boolean inTheMenu = true;
WelcomePanel welcomePanel;
public TopMenu(WelcomePanel welcomePanel) {
this.welcomePanel = welcomePanel;
background = new ImageIcon("src/game/images/TopBalk/Topbalk.png").getImage();
setLayout(null);
setSize(new Dimension(1000, 50));
setBackground(Color.GRAY);
setButtons();
}
public void setButtons() {
//initialise all buttons
playButton = new topMenuButton("src/game/images/TopBalk/Play_Button.png", "src/game/images/TopBalk/PlayGlow_Button.png", 110, 9, 43, 48, 0, "", 30, 34);
forwardButton = new topMenuButton("src/game/images/TopBalk/Forward_Button.png", "src/game/images/TopBalk/ForwardGlow_Button.png", 150, 9, 59, 48, 0, "", 30, 34);
menuButton = new topMenuButton("src/game/images/TopBalk/Menu_Button.png", "src/game/images/TopBalk/MenuGlow_Button.png", 20, 4, 109, 48, 0, "", 38, 86);
goToTheGameButton = new topMenuButton("src/game/images/TopBalk/goToTheGame_Button_needsRework.gif", "src/game/images/TopBalk/goToTheGameGlow_Button_needsRework.gif", 400, 4, 109, 48, 0, "", 38, 86);
//add actionlisteners to buttons
playButton.addActionListener(this);
forwardButton.addActionListener(this);
menuButton.addActionListener(this);
goToTheGameButton.addActionListener(this);
//add buttons that are needed now
addUsefulButtons();
}
public void addUsefulButtons() {
//add the usefull buttons
if (playingNow) {
add(playButton);
add(forwardButton);
}
if (inTheMenu) {
add(goToTheGameButton);
}
if(!inTheMenu){
add(menuButton);
}
if(changingSettings)
{
}
}
public void removeButtons() {
remove(playButton);
remove(forwardButton);
remove(menuButton);
}
#Override
public void actionPerformed(ActionEvent e) {
//If the player hits play or pause
if (e.getSource().toString().contains("Play") || e.getSource().toString().contains("Pauze")) {
//change the pause state of the game
GameLoop.paused = !GameLoop.paused;
//check if the game is paused
if (GameLoop.paused)
//change the play button image
playButton.setImage("src/game/images/TopBalk/Pauze_Button.png", "src/game/images/TopBalk/PauzeGlow_Button.png");
else {
//change the play button image
playButton.setImage("src/game/images/TopBalk/Play_Button.png", "src/game/images/TopBalk/PlayGlow_Button.png");
}
//if the player hits fast forward
} else if (e.getSource().toString().contains("Forward")) {
//do stuff to fast forward
System.out.println("Forward");
}
//if the player hits the menu button
else if (e.getSource().toString().contains("Menu_Button.png")) {
//do stuff to show the menu
System.out.println("Menu");
}
//if the goToTheGame button is pressed
else if (e.getSource().toString().contains("goToTheGame")){
welcomePanel.startGame();
}
//if there is no recognised action command
else{
//just display a message in the console.. WTF??
System.out.println("TopMenuActionlistener has unknown potentials!! Check the actionPerformed plz..");
}
}
public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, null);
}
}
Validating a component means laying out its subcomponents, which will then repaint them if they they need to move. Since you are using absolute positioning rather than a layout manager, this isn't going to do anything for you.
Instead of calling revalidate(), call repaint().
I have a Java program with two textareas and a button. I want to allow the user to write one one textarea using a touch pen or mouse and when he/she clicks the button, the drawn content should be send to textarea no 2.
So the textarea where the user is writing on, I gave a mousemotion listener with paintComponent method in it.
When I run the application, I have realized that texarea method getText() and setText() can't set or get the drawn content.
Is there way I can achieve the above task? I have also tried JPanel but it doesn't have the setContent() method.
I appreciate any help.
This is my first textarea where user is writing on with touchpen.
public class Area1 extends JTextArea {
int pointcount = 0;
Point[] points = new Point[10000];
public Area1() {
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent event) {
if (pointcount < points.length) {
points[pointcount] = event.getPoint();
++pointcount;
repaint();
}
}
});
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < pointcount; i++) {
g.fillOval(points[i].x, points[i].y, 4, 4);
}
}
}
this is my overall application with textarea2 and button
public class Handwriting extends JFrame {
private JTextArea area2;
private JButton b1;
Area1 area1;
public Handwriting() {
Box box = Box.createHorizontalBox();
area1 = new Area1();
area1.setRows(30);
area1.setColumns(30);
area2 = new JTextArea(30, 30);
b1 = new JButton("Copy");
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == b1) {
area2.setText(area1.getText());
}
}
});
box.add(area1);
box.add(b1);
box.add(area2);
add(box);
}
public static void main(String[] args) {
Handwriting hand = new Handwriting();
hand.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hand.setSize(500, 500);
hand.setVisible(true);
}
}
One possible solution would be to make one class as a Inner Class of the other , that way even private instance fields can be accessed