Text won't appear on game? - java

I have a game program and I want to add a title to my main menu. I added the graphics and added a string of text to be displayed but it won't show up. I added a comment were the problem may be.
My main code:
package Main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame("Tennis Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Menu graphics = new Menu();
frame.add(graphics);
frame.setLayout(null);
final JButton b = new JButton("Play");
b.setFocusPainted(false);
b.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
b.setBounds(110, 100, 80, 40);
b.setForeground(Color.BLACK);
b.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseEntered(MouseEvent evt) {
b.setForeground(Color.RED);
}
public void mouseExited(MouseEvent evt) {
b.setForeground(Color.BLACK);
}
});
frame.add(b);
frame.setSize(300,400);
frame.setVisible(true);
}
}
My menu code with the title:
package Main;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import javax.swing.*;
public class Menu extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.yellow);
g.setFont(new Font("Arial", Font.BOLD, 30));
g.setColor(Color.BLACK);
g.drawString("TENNIS GAME", 40, 60);
}
}

When you use frame.setLayout(null), sub-Components aren't automatically sized, you'll need to do it yourself: Add graphics.setBounds(0, 0, 300, 100);

Related

how to change the background color in a window application in java

I want to create an application where pressing a button changes the background color, but I don't know why the color won't change. I can't change it at all. Swapping the button color and its font works, but I can't change the background color.
package okno;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Kolory extends JFrame implements ActionListener {
static JButton FirstButton;
Color [] colors= {Color.GREEN,Color.RED,Color.BLACK};
JLayeredPane screen;
public Kolory() {
super("Zmiana kolorów");
screen = new JLayeredPane();
getContentPane().add(screen).setBackground(Color.RED);;
screen.setPreferredSize(new Dimension(500,500));
FirstButton = new JButton("Pierwszy przycisk");
screen.add(FirstButton);
FirstButton.setBounds(30, 30, 150, 20);
FirstButton.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new Kolory();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent e) {
Random r = new Random();
int counter = r.nextInt(3);
screen.setBackground(colors[counter]);
}
}

How can we put value on text field on output screen?

I want to put value in txtf1 at output screen and get it. How can we put value on text field on output screen?
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class demog extends JPanel implements ActionListener{
private TextField textf, txtf1;
public void jhand(){
textf = new TextField();
textf.setSize(40, 40);
textf.setText("20");
textf.setEditable(false);
textf.setBackground(Color.WHITE);
textf.setForeground(Color.BLACK);
//textf.setHorizontalAlignment(SwingConstants.CENTER);
textf.setLocation(15, 15);
//textf.addActionListener(this);
txtf1 = new TextField();
txtf1.setSize(40, 40);
txtf1.getText();
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
//txtf1.setHorizontalAlignment(SwingConstants.CENTER);
txtf1.setLocation(50, 50);
JFrame frame = new JFrame("demo");
JPanel p = new JPanel();
p.setOpaque(true);
p.setBackground(Color.WHITE);
p.setLayout(null);
frame.setContentPane(p);
frame.setSize(500,500);
frame.setVisible(true);
p.add(textf);
p.add(txtf1);
}
public void actionPerformed(ActionEvent evt) {
String text = textf.getText();
System.out.println(text);
}
public static void main(String... args){
demog g = new demog();
g.jhand();
}
}
You have to change some of your code in order to work. You had some problem in your code which I resolved them for you in the following code. See the comments to learn some in swing ;-)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
// Use upper Case in the start of you class names:
public class Demog extends JPanel implements ActionListener {
private JTextField textf, txtf1;
public Demog() {
jhand();
}
public void jhand() {
setLayout(new FlowLayout()); // Always set the layout before you add components
// you can use null layout, but you have to use setBounds() method
// for placing the components. For an advanced layout see the
// tutorials for GridBagLayout and mixing layouts with each other.
textf = new JTextField(); // Do not mix AWT component with
// Swing (J components. See the packages)
//textf.setSize(40, 40); // Use setPreferredSize instead
textf.setPreferredSize(new Dimension(40, 40));
textf.setText("20");
textf.setEditable(false); // Text fields are for getting data from user
// If you need to show something to user
// use JLabel instead.
textf.setBackground(Color.WHITE);
textf.setForeground(Color.BLACK);
add(textf);
txtf1 = new JTextField();
//txtf1.setSize(40, 40); Use setPreferredSize instead
txtf1.setPreferredSize(new Dimension(40, 40));
txtf1.getText();
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
add(txtf1);
JButton b = new JButton("Click ME!");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent evt) {
String text = textf.getText();
JOptionPane.showMessageDialog(Demog.this, "\"textf\" text is: "+text);
}
public static void main(String[] args) {
JFrame frame = new JFrame("demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Demog p = new Demog();
p.setBackground(Color.WHITE);
frame.setContentPane(p);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Good Luck.

Java is drawing 2 boxes

So I am making a little game to learn some graphical java and I am having trouble with a button. It is drawing 2, one is the correct size and in the correct location and then there is a very small button centered at the top of the application. THere should only be the one button at (0,0,200,50). I do not know what is wrong but here is the code for the button, if you need something more then this let me know!
ImageIcon test = new ImageIcon("nhButton.png");
JButton jb = new JButton(test);
jb.setBounds(0, 0, 200, 50);
jb.setVisible(true);
add(jb);
EDIT1: the 2 classes where error will be: board.java:
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Board extends JPanel {
public Board() {
}
#Override
public void paintComponent(Graphics g) {
ImageIcon test = new ImageIcon("nhButton.png");
JButton jb = new JButton(test);
jb.setBounds(0, 0, 200, 50);
jb.setVisible(true);
add(jb);
}
private void drawRectangle(Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(x, y, width, height);
}
}
and the main:
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class main extends JFrame {
public main() {
initUI();
}
private void initUI() {
add(new Board());
setSize(800, 600);
setTitle("Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
main ex = new main();
ex.setVisible(true);
}
});
}
}
If you try to resize window, you will see that buttons are spawning.
This happens because of your paintComponent method, which is called every painting iteration.
You should move button addition, for example, to constructor which is called once:
public Board() {
ImageIcon test = new ImageIcon("nhButton.png");
JButton jb = new JButton(test);
jb.setBounds(0, 0, 200, 50);
jb.setVisible(true);
add(jb);
}

How to make JFrame paint only after I click button?

When I run the application the whole frame is painted black.
How can I make it so that it starts out clear then it gets painted when I press the button?
package card;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BdayCard extends JFrame {
JButton button1, button2;
JPanel panel;
BdayCard()
{
panel = new JPanel();
button1 = new JButton();
button1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
repaint();
}
});
panel.add(button1);
this.add(panel);
this.setTitle("It's Your Birthday!");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 450);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
g.fillRect(0, 0, 600, 450);
}
public static void main(String[] args){
new BdayCard();
}
}
your problem with your blackscreen is because you paint at:
g.fillRect(0, 0, 600, 450);
you are using the default colour which is black
I tried your code and used this:
g.setColor(Color.WHITE);
this clears your screen and then use a boolean and set it true when your button is pressed:
public void actionPerformed(ActionEvent e)
{
button=true;
repaint();
}
then finally use:
if(button){/*do stuff here*/}
in the paint method

Graphis2d drawString to generate german umlauts

I am trying to use Graphics2d to generate german unlauts and http://en.wikipedia.org/wiki/%C3%9F.
The output that I always see is two question marks. Any ideas about how to solve this?
Below is some code to print out the characters you want. My guess is that the font you are using may not have those characters if you are getting question marks. The font being reported when I run the example is LucidaGrande.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
public class DrawStringUmlaut extends JPanel {
public DrawStringUmlaut() {
setPreferredSize(new Dimension(getPreferredSize().width, 200));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("\u00f6", 10, 20);
g.drawString("\u00df", 40, 20);
g.drawString(g.getFont().getFontName(), 10, 40);
g.drawString(Integer.toString(g.getFont().getSize()) + " pt", 10, 60);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawStringUmlaut(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}
If your system uses a font which can display these characters (ü and ß) , it should work out of the box.
Try the following example:
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Graphics2dUmlaut extends Frame {
public void paint(Graphics g) {
Graphics2D g1 = (Graphics2D) g;
g1.drawString("\u00fc\u00df", 100, 100);
}
public static void main(String args[]) {
Frame frame = new Graphics2dUmlaut();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setSize(200, 200);
frame.setVisible(true);
}
}

Categories