Java Frame shows my screen - java

I am starting to learn how to make graphics in Java. But if I draw something in my Frame and I run the program. those parts of the Frame where I did not draw anything there is the picture of the Frame running behind my Program. hw can I avoid this?
package de.sarah;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Framemg extends JFrame{
public Framemg() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Zeichnen mit Java");
setSize(400, 300);
setBackground(Color.yellow);
setVisible(true);
}
public void paint(Graphics g) {
g.drawString( "Hellooo", 120, 60 );
}
}
package de.sarah;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
public class Main{
public static void main(String[] args) {
Framemg Frame = new Framemg();
}
}

Don't override paint() of a JFrame.
Custom painting is done by overriding the paintComponent(...) method of a JPanel. Then you add the panel to the frame.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.

Related

Java (Button+Graphics)

I just started to work with Java.
I have no problems with creating window,button and any graphics.
But, i cant make window with many buttons, graphics and text boxes.
When i add buttons to my window i cant see graphics.
How can i do it?
code:
package today;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.Graphics;
public class mybuttonapp extends JFrame
{
private mybuttonapp()
{
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
//new mybuttonapp().setVisible(true);
//--
JFrame f=new JFrame("Button Example");
f.setVisible(true);
f.setSize(900, 600);
//f.setLayout(null);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.GREEN);
p1.setPreferredSize(new Dimension (100,100));
p1.setVisible(true);
f.add(p1);
My2d paint1 = new My2d();
JButton b1=new JButton("Click Here");
b1.setBounds(10,10,100,50);
p1.add(b1);
p1.add(paint1);
}
code 2:
package today;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.*;
import java.awt.event.*;
public class My2d extends JComponent
{
public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(300,300,50,50);
g2.fill(circle);
}
}
I think what you want to look at are layouts in swing. If I get what you ask and what you're doing you are overwriting whatever you set first.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
This link provides explanations to all of the swing layouts.
You need to check how LayoutManagers work in Java. They help you to put the elements in the right place within your container (JFrame/JPanel).
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html
Normally I work with a combination of BorderLayout and GridBagLayout, since it is quite flexible - but many users feel GridBagLayout is too complicated, especially for beginners.

Drawing Graphics with other classes

I have two classes below: And I'm trying to make it so that calling FilledFrame would make certain shapes. I've tested it with a Line below, but it doesn't show up on the GUI. Is there something I'm doing wrong here? I also need to make a Card class that is able to modify some rectangle's positions, so would I need to make a graphics object in the main and enter it into FilledFrame and my Card class?
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import javax.swing.*;
public class FilledFrame extends JFrame implements ActionListener {
static int FRAME_WIDTH = 500;
static int FRAME_HEIGHT = 100;
Graphics g = getGraphics();
public FilledFrame () {
g.drawLine(1, 1, 100, 100);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
}
private void createComponents() {
...
}
private void createCards() {
...
}
#Override
public void actionPerformed(ActionEvent e) {
}
public static void draw (Graphics g) {
g.fillRect(0, 50, 100, 150);
}
}
Main class:
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Float;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.Canvas;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class Main {
public static void main (String args[]) {
FilledFrame frame = new FilledFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
Is there something I'm doing wrong here?
Sorry to say, but you're guessing at how to do Swing drawing, and while this might work with simple Java concepts, it won't for something as complex as Swing graphics.
Problems include:
Trying to draw directly within the JFrame -- never do this, but instead draw in a JPanel.
Calling getGraphics() on a component to get a Graphics resource. This will lead to your getting an unstable and short-lived Graphics object that can lead to either failed painting or NullPointerExceptions
Not drawing in an appropriate painting method.
You're using int literals in your drawing method parameters rather than int variables, making any drawing that you do do, fixed -- you can never move or change it.
Instead I suggest that you:
Create a class that extends JPanel and paint in that class.
Draw within this class's protected void paintComponent(Graphics g) method.
Don't forget to call the super's paintComponent method within your override so that your GUI cleans up any dirty pixels.
Then place that JPanel into the JFrame that needs to display it.
Use int fields (variables) not int literals (so-called "magic" numbers) for most of your drawing method parameters so that your drawings can change if you so desire while the program runs.
And most important, read and study the standard Swing graphics tutorials.
Resources:
The Really Big Index: the main tutorial where you should start.
Using Swing Components: how to create Swing GUI's
Lesson: Performing Custom Painting: introductory tutorial to Swing graphics
Painting in AWT and Swing: advanced tutorial on Swing graphics

My program won't paint Here's the code:

This code won't fill the rectangle. I tried to extend JComponent but I received an error. How would I extend JComponent?
package com.lewis.GooseEgg;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class GooseEgg extends Canvas{ //I couldn't extend JComponent
//How would I also extend JComponent
protected void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0, 100, 100);
}
public static void main(String[] args) {
//This is just all the basic stuff I learned
JFrame frame = new JFrame();
GooseEgg goose = new GooseEgg();
frame.setResizable(false);
frame.setTitle("Goose Egg");
frame.add(goose);
frame.pack();
frame.setSize(900, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//The website keeps asking me to add more detail because it says my post is mostly code so I added this. If anyone wants to tutor me at Java
//I could pay money.
}
}
You don't need to extend from Canvas and JComponent: You should only extend from JComponent.
Also you should not do pack() and setSize(…). They both set the size of your frame, this does not make sense. Either do pack() (in which case you also need to specify a preferred size e.g. by overriding getPreferredSize() or setting it with setPreferredSize(…)) or do setSize(…).
Changing Canvas to JComponent and removing the pack() method call seems to produce the correct result.

JButton won't show?

So I'm looking at a tutorial here: http://zetcode.com/tutorials/javagamestutorial/basics/
At the very end, they show how to set an image as a background (which is simple). They use 2 codes: Board.java and Image.java (which I've copied below for your convenience).
For some reason, I can't seem to add a JButton to my GUI?
I tried adding the following code to Image.java after the line Image Main = new Image(); , but I can't figure out why it doesn't work. Normally I can just add a JButton to a JPanel using the add command and set the panel to visible with setVisible(true).
JButton start;
start = new JButton("Click Me");
Main.getContentPane().add(start);
I also tried adding a similar code to Board.java instead just to see if it'd work - and it of course didn't. How come my JButton won't show up? I know I'm doing something wrong, but I can't figure it out. Can someone help?
(1) Board.java
package bardejov;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Board extends JPanel {
Image bardejov;
public Board() {
ImageIcon ii = new ImageIcon(this.getClass().getResource("bardejov.jpg"));
bardejov = ii.getImage();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bardejov, 10, 10, null);
}
}
(2) Image.java
package bardejov;
import javax.swing.JFrame;
import javax.swing.JButton;
public class Image extends JFrame {
public Image() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(280, 240);
setLocationRelativeTo(null);
setTitle("Bardejov");
setVisible(true);
}
public static void main(String[] args) {
Image Main = new Image();
}
}
You need to first create a JPanel with Board and Button laid out correctly. Then set this JPanel as the content pane for the JFrame. You are trying to add two different components to the Frame via different methods which is causing the confusion. If you resize the frame may be you will see the button you added in the background.

paintComponent method not being called in Java

I'm doing this very basic and simple Swing tutorial as the first assignment from my software engineering course, and for some very strange reason, the paintComponent method isn't being invoked in my JPanel. Now I've worked with Java Swing in the past and I've never had any issues like this.
The tutorial I am using is found right on the Oracle site(easier to go to the site and look at the code since it's the same code I have).
Tutorial Link
Can anyone please explain to me why it isn't working for me?
My code:
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
public class PaintDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
private static void createGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame frame = new JFrame("Yay, first 2102 lab!!");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); // allows to close the program
DemoPanel panel = new DemoPanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
class DemoPanel extends JPanel {
public DemoPanel() {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
public void paintComponenet(Graphics g) {
super.paintComponent(g);
g.drawString("This is my custom panel!",10,20);
}
}
It is paintComponent(Graphics g), not paintComponenet(Graphics g).
At least you are calling the super.paintComponent(g) correctly.
If you annotate your paint* method with the #Override annotation, you will get a compile error that will help you understand what is going on.

Categories

Resources