I want the racket to be positioned by using the getWidth method for the gamePanel, but it returns 0. It works fine for the frame, and it worked for the panel as well, but I decided to rewrite alot of the code and now I cant get it to work. Help is appreciated :)
import java.awt.BorderLayout;
import java.awt.Color;
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.JLabel;
import javax.swing.JPanel;
public class Oppgave1 extends JFrame {
public static void main(String[] args) {
JFrame frame = new Oppgave1();
frame.setTitle("Oppgave 1");
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private JLabel jlbBallCount = new JLabel("Ballcount");
private JLabel jlbTimer = new JLabel("Timer");
private JButton jbtNewBall = new JButton("New Ball");
private Racket racket;
private Ball ball;
public Oppgave1() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(jlbBallCount);
buttonPanel.add(jlbTimer);
buttonPanel.add(jbtNewBall);
JPanel gamePanel = new JPanel();
gamePanel.add(racket = new Racket (100, 60));
gamePanel.add(ball = new Ball(10, 3, racket));
this.add(gamePanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public void paint(Graphics g){
racket.draw(g);
ball.draw(g);
}
}
Don't override the paint() method of a JFrame.
You added the racket and ball to the game panel. The game panel will now paint these components automatically.
If you want to be able to move these components then you must set the layout to null and initially set the bounds of the components. Then when you want to move the component you just invoke the setLocation() method and Swing will paint the component in its new position.
Related
I've been trying to create two layered images within a JFrame using JLayeredPane however I'm not able to draw a shape using the paint() function within the Canvas class.
Here is what I wrote:
package com.baduk.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Game extends Canvas implements Runnable {
private ImageIcon icon;
private JLabel back;
private JLayeredPane layer;
/**
*
*/
private static final long serialVersionUID = -3441156857004256039L;
public Game() {
//Background image with depth 0
icon = new ImageIcon("C:/Users/scaraven/Downloads/baduk_board.png");
back = new JLabel(icon);
back.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
//Create JFrame
JFrame frame = new JFrame("BADUK");
frame.setSize(new Dimension(icon.getIconWidth()+5,icon.getIconHeight()+25));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
//Create JLayeredPane
layer = new JLayeredPane();
layer.add(back,new Integer(1));
layer.add(this,new Integer(0));
frame.setContentPane(layer);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
#Override
public void run() {
// TODO Auto-generated method stub
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(10, 10, 100, 100);
}
}
According to what I wrote, A Jframe should be generated with an image as the background and a black rectangle on top, however I only get the image. Does anyone know what is wrong with the code I wrote and what I need to change?
I am trying to display a background image on the JFrame using a JLabel. The code runs and the buttons appear, but the image does not. I have researched for solutions, yet I have not found one for my code specifically. Any help would be greatly appreciated.
/**
* Adds details to interface and programs buttons
*
* Imani Davis
* Final Project
*/
import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;
public class Use_PF_Interface extends JFrame implements Pet_Fish_Interface
{
// instance variables - replace the example below with your own
private JFrame window;
private JPanel panel1, panel2, panel3;
private JLabel lblBackgroundImage = new JLabel();
private JButton feedButton = new JButton("Feed Fish");
private JButton playGamesButton = new JButton("Play Game");
/**
* Constructor for objects of class Use_PF_Interface
*/
public Use_PF_Interface()
{
setTitle("Virtual Pet Fish");
setSize(650, 650);
//initializes panels and panel layout
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
panel3.setLayout(new FlowLayout());
lblBackgroundImage.setLayout(new FlowLayout());
//sets background image of panel
lblBackgroundImage.setIcon(new ImageIcon("C:\\Users\\This PC\\Desktop\\OCEAN2.JPEG"));
panel1.add(lblBackgroundImage);
validate();
//adds button to panels
panel2.add(feedButton);
panel2.add(playGamesButton);
//add panels to frame
add(panel1);
add(panel2);
}
}
JFrame uses a BorderLayout by default, a BorderLayout can only manage a single component within any of the five available positions it provides, this means that panel2 is most likely the only component getting shown.
An alternative is to add you components to the JLabel, but remember, JLabel doesn't have a default layout manager. Also, remember, JLabel only uses the icon and text properties to calculate its preferred size, so if the contents require more space, they will be clipped.
Start by having a look at How to Use BorderLayout for more details
Also, remember, most Swing components are opaque generally, so you need to set them transparent when you want to do something like this
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Use_PF_Interface extends JFrame {
// instance variables - replace the example below with your own
private JPanel panel2;
private JLabel lblBackgroundImage = new JLabel();
private JButton feedButton = new JButton("Feed Fish");
private JButton playGamesButton = new JButton("Play Game");
/**
* Constructor for objects of class Use_PF_Interface
*/
public Use_PF_Interface() {
setTitle("Virtual Pet Fish");
setSize(650, 650);
//initializes panels and panel layout
panel2 = new JPanel();
panel2.setOpaque(false);
panel2.setLayout(new FlowLayout());
lblBackgroundImage.setLayout(new FlowLayout());
//sets background image of panel
lblBackgroundImage.setIcon(new ImageIcon("..."));
lblBackgroundImage.setLayout(new BorderLayout());
//adds button to panels
panel2.add(feedButton);
panel2.add(playGamesButton);
lblBackgroundImage.add(panel2);
add(lblBackgroundImage);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new Use_PF_Interface();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Try this,
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageInFrame {
public static void main(String[] args) throws IOException {
String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}
I am new to the swing and graphics and am trying to get this to work for drawing a string on a label by making an object class called DrawString. A panel currently pops up with nothing on it. I would like to thank you for any guidance you can give.
package view;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Run {
public static void main(String[] args) {
JPanel panel = new JPanel();
DrawString text = new DrawString();
JFrame window = new JFrame();
window.setVisible(true);
window.setSize(400, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.display("Boo");
panel.setLocation(((window.getWidth()/2)-(panel.getWidth()/2)),
((window.getHeight()/2)-(panel.getHeight()/2)));
panel.setSize(200, 200);
window.add(panel);
panel.add(text);
}
}
package view;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
#SuppressWarnings("serial")
public class DrawString extends JLabel{
String string;
Font font = new Font("TimesRoman",Font.PLAIN, 30);
public DrawString() {
super();
}
public void display(String string){
this.string=string;
repaint();
}
public void drawString(Graphics comp){
super.paintComponent(comp);
Graphics2D g = (Graphics2D) comp;
g.setFont(font);
g.drawString(string, JLabel.CENTER, JLabel.CENTER);
}
}
I am learning and experimenting…
It's not clear if you want to experiment with JLabel or custom painting. You might want to start with a working JLabel#setText() example or TextLayout#draw() example.
The JPanel called panel only shows up as one small red square up the top center, I have tried to set the size but it doesn't seem to do anything.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw extends JFrame{
private JPanel panel;
public Draw() {
super("title");
setLayout(new FlowLayout());
panel = new JPanel();
panel.setBackground(Color.RED);
add(panel, BorderLayout.CENTER);
}
}
The default, preferred size of a JPanel is 0x0. FlowLayout lays out components based on their preferred size, hence the component now has a preferred size of 1x1 (the line border adds a little weight).
You could try adding another component to panel...
panel.add(new JLabel("This is some text"));
Or override panels getPreferredSize method...
panel = new JPanel() {
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
Try this :
File Draw.java
package com.stackovfl;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
#SuppressWarnings("serial")
class Draw extends JFrame {
private JPanel panel;
public Draw() {
super("title");
setLayout(new FlowLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(200, 300));
panel.setBackground(Color.RED);
add(panel, BorderLayout.CENTER);
/* Important to get the layout to work */
pack();
/* Center the window */
setLocationRelativeTo(null);
/* Important if you want to see your window :) */
setVisible(true);
}
}
File Test.java (main method to launch the window) :
package com.stackovfl;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Draw();
}
});
}
}
I am a beginner, starting a simple project on GUI. The RectangleComponent should draw a Rectangle on the form with a button click. A rectangle won't draw with the following code, but if I put the same 2 lines of code outside the listener, it certainly works. I would appreciate any help.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class EllipseRectViewer {
/**
* #param args
*/
public static void main(String[] args)
{
final JFrame frame = new JFrame();
final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Rectangle and Ellipse Draw");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
frame.add(panel, BorderLayout.NORTH);
class RectangleDrawListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
RectangleComponent r2 = new RectangleComponent();
frame.add(r2);
}
}
JButton rectButton = new JButton("Rectangle");
ActionListener rectDrawListener = new RectangleDrawListener();
rectButton.addActionListener(rectDrawListener);
panel.add(rectButton);
frame.setVisible(true);
}
}
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class RectangleComponent extends JComponent
{
Rectangle rect;
public RectangleComponent()
{
rect = new Rectangle(20, 20, 30, 30);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.draw(rect);
}
}
Thank you.
After adding the RectangleComponent to the frame, either revalidate the newly added component or the frame's root pane:
public void actionPerformed(ActionEvent event) {
RectangleComponent r2 = new RectangleComponent();
frame.add(r2);
// Option 1
r2.revalidate();
// Option 2
frame.getRootPane().revalidate();
}
Note1: the frame itself can't be revalidated (upto JDK 1.6)
Note2: the frame itself can be revalidated (JDK 1.7+)
i think you need to revalidate() the frame.
frame.revalidate();
put it like this:
public void actionPerformed(ActionEvent event)
{
RectangleComponent r2 = new RectangleComponent();
frame.add(r2);
frame.revalidate();
}
Try to use LineBorder. Create a JPanel with LineBorder and add the JButton to the JPanel.
rect = new Rectangle(20, 20, 30, 30);
A second problem is that your component doesn't have a preferred size. Your component displays in a simple frame because you add the comonent to the center of a BorderLayout so the preferred size of the component is ignored. However, this won't work if you try to use the component when using other layout managers.
You should also override the getPreferredSize() method to return the preferred size of your component at a minimum you need to use:
return new Dimension(50, 50);
to accomodate the size and location of the painted rectangle.