I've been trying to come up with a program that for a physics project, but I'm having problems. Here's the code:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Cannon extends JFrame {
Cannon() {
setTitle("Cannonball Experiment");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new ImagePanel());
c.add(new ModifierPanel());
setSize(400,600);
setVisible(true);
}
class ImagePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(0,100,200,300);
}
}
class ModifierPanel extends Panel {
JLabel Text = new JLabel("Speed");
JTextField Tf = new JTextField(10);
ModifierPanel() {
add(Text);
add(Tf);
}
}
public static void main(String[] args) {
Cannon frame = new Cannon();
}
}
ModifierPanel displays fine but the JPanel, which I added before the Modifier, is not being displayed.
JPanel ImageJPanel doesn't show anything, so its dimension is (0,0).
Try to give your JPanel a dimension overriding method getPreferredSize like this:
class ImagePanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(0,100,200,300);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500,300); //for example
}
}
You're using FlowLayout that honors getPreferredSize so you won't have any problems. Try and let me know...
try buffered image in image panel ..
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(new File("specify image name and path"));
} catch (IOException ex) {
// exception...
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
Related
So guys I want to use the code I have to set the background of the jFrame without adding anything from another class (like using this code in a jPanel then adding that panel to a jFrame). I wanna do everything in this class. I really have no idea what to do, so I tried this out but this code is not displaying the image!
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class panel extends JFrame{
Image img;
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
g.dispose();
}
public panel(){
img=new ImageIcon(getClass().getResource("bg_login.jpg")).getImage();
setExtendedState(JFrame.MAXIMIZED_BOTH);
pack();
setVisible(true);
}
public static void main(String[] args){
new panel();
}
}
there is no paintcomponent() method for jframe as it's not a jcomponent but a container .you can make a panel and overide paintcomponent method then setcontentpane of jframe to that panel
example
public class panel extends JPanel {
Image img;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
}
// g.dispose();
}
public panel() {
img=new ImageIcon(getClass().getResource("bg_login.jpg")).getImage();
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
panel panel = new panel();
jFrame.setContentPane(panel);
jFrame.pack();
jFrame.setVisible(true);
}
}
I am trying to draw a rectangle in the class "Graphics", but for some reason the rectangle does not appear, but the program returns no errors. I have never experience issues such as this before so I am rather confused.
Main()
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main
{
public Main()
{
JFrame window = new JFrame();
Sound soundCall = new Sound();
Graphics graphicsCall = new Graphics();
final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(container);
window.setSize(600, 400);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new Main();
}
});
}
Graphics()
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Graphics extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(500, 500, 500, 500);
}
}
EDIT FOR HOVERCRAFT
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main
{
public Main()
{
JFrame window = new JFrame();
Sound soundCall = new Sound();
Draw drawCall = new Draw();
final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(drawCall);
window.setSize(600, 400);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new Main();
}
});
}
}
Through adding this window.getContentPane().add(drawCall); asks me to change drawCall to a Component
EDIT 2:
public class Draw
{
public class Graphics extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
}
}
}
ERROR
The method add(Component) in the type Container is not applicable for the arguments (Draw)
You add your graphicsCall variable to nothing, and so it will not be displayed. Solution: add it to a container such as that JPanel that you just created, or perhaps directly to the JFrame's contentPane.
i.e., change this:
JFrame window = new JFrame();
Sound soundCall = new Sound();
Graphics graphicsCall = new Graphics();
final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(container);
to this:
JFrame window = new JFrame();
Sound soundCall = new Sound();
Graphics graphicsCall = new Graphics();
// final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(soundCall);
As an aside, you will want to re-name that class from Graphics to something else, or else you risk confusing yourself or your compiler since there already exists a critical Java class with that name.
Also, avoid using setSize(...). Better to have your drawing JPanel override getPreferredSize() and to call pack() on your JFrame.
Edit
As per MadProgrammer's astute observation, you're drawing outside of the bounds of your component.
Edit 2
Regarding your latest code, this:
public class Draw
{
public class Graphics extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
}
}
}
is useless dreck. Why are you needlessly wrapping a class inside of a class? Instead why not simply:
public class Draw extends JPanel {
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
}
#Override
public Dimension getPreferredSize() {
// return an appropriate Dimension here
}
}
I have two classes: one is JFrame and second is JPanel. When i add JPanel object in JFrame class it makes not error but did not show JPanel result. It only shows blank JFrame.
This is my JPanel class:
public class grafix extends JPanel {
#Override
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2= (Graphics2D) g;
Rectangle r = new Rectangle(15,10,200,300);
g2.draw(r);
g2.setColor(Color.blue);
g2.fillOval(50, 50, 30,30);
g2.drawString("Hello World", 120, 50);
}
}
And this is my JFrame Class:
public class JFrame extends javax.swing.JFrame {
public JFrame() {
initComponents();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
grafix gf = new grafix(); // object created of JPanel class
frame.getContentPane().add(gf);// by adding object
frame.pack();
frame.setVisible(true);
}
});
}
}
No error being mentioned in Netbean but it did not show any drawing but frame only.
The graphics are shown but your frame is too small to show the panel graphics. Your Grafix panel component is using the default preferred size of 0x0 to the frame reveals nothing. Override getPreferredSize in the class to allow the correct size to be set when the frame is packed
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 400);
}
I have an applet which has a Panel. In the Panel a button is added which on clicking will remove the current Panel and a new Panel will be added to the current Applet.
But I am not getting the desired output !!!
I want to replace the Display Panel currently added to the Applet by a new Panel from a ActionListener.
Kindly tell the mistake !!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class Init extends JApplet {
public Display ref;
public NewDisplay ref2;
public class Display extends JPanel implements ActionListener {
public Display() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton1.setText("New Game");
add(jButton1);
jButton1.addActionListener(this);
}
public javax.swing.JButton jButton1;
#Override
public void actionPerformed(ActionEvent e) {
String x = e.getActionCommand();
if (x.equals("New Game")) {
System.out.println("clicked");
//ref.setVisible(false);
this.removeAll();
//add(ref2);
add(ref2);
invalidate();
revalidate();
repaint();
}
}
}
public class NewDisplay extends JPanel {
public NewDisplay() {
setSize(800, 600);
}
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
g.fillRect(0, 0, 800, 600);
}
}
#Override
public void init() {
ref = new Display();
ref2 = new NewDisplay();
add(ref);
setSize(800,600);
}
}
You should NOT be using the setSize() method to set the size of a component.
Layout managers use the preferred size of the component. You should be overriding the getPreferredSzie() method of your panel to returnthe desired size.
public class NewDisplay extends JPanel {
public NewDisplay() {
// setSize(800, 600);
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(800, 600);
}
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
g.fillRect(0, 0, 800, 600);
}
}
Or a better solution is to use a Card Layout and just sway the panels in and out. See How to Use Card Layout.
I have a JScrollPane and on top of it I have a JPanel named 'panel1'.
I want some rectangles to be drawn on this JPanel.
I have a class named DrawRectPanel which extends JPanel and does all the drawing stuff.
The problem is that, I tried to draw the rectangles on panel1 by writing the following code :
panel1.add(new DrawRectPanel());
but nothing appeared on panel1
then I tried, just as a test to the class DrawRectPanel :
JFrame frame = new JFrame();
frame.setSize(1000, 500);
Container contentPane = frame.getContentPane();
contentPane.add(new DrawRectPanel());
frame.show();
This worked, and produced the drawings but on a separate JFrame
How can I draw the rectangles on panel1 ?
Thanks in advance.
EDIT :
code for DrawRectPanel
public class DrawRectPanel extends JPanel {
DrawRectPanel() {
Dimension g = new Dimension(400,400);
this.setPreferredSize(g);
System.out.println("label 1");
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("label 2");
g.setColor(Color.red);
g.fillRect(20, 10, 80, 30);
}
}
only label 1 is printed on the screen
still no idea,
for example
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class CustomComponent extends JFrame {
private static final long serialVersionUID = 1L;
public CustomComponent() {
setTitle("Custom Component Graphics2D");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void display() {
add(new CustomComponents());
pack();
// enforces the minimum size of both frame and component
setMinimumSize(getSize());
setVisible(true);
}
public static void main(String[] args) {
CustomComponent main = new CustomComponent();
main.display();
}
}
class CustomComponents extends JComponent {
private static final long serialVersionUID = 1L;
#Override
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
#Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}
instead of adding
contentPane.add(new DrawRectPanel());
you should do
contentPane.add(panel1);
Because you already have new DrawRectPanel in panel1. But in your code you are adding another instance of DrawRectPanel in contentPane. And never added panel1 in none of your container.
to fix your problem, change "paintComponent" to "paint" when the window repaints automatically, it should work.