Image drawing on a Java component - java

I'm trying to create a simple application that draws graphics...
package Tests.Drawing;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;
public class DrawingTest extends JFrame
{
private Canvas drwArea;
private JButton btnClear;
public static void main(String[] args)
{
DrawingTest StartForm = new DrawingTest();
StartForm.setVisible(true);
}
public DrawingTest()
{
//Window...
this.setTitle("Drawing objects test00");
this.setBounds(0,0,510,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
//Drawing area...
drwArea = new Canvas();
drwArea.setBounds(0, 0, 400, 450);
drwArea.setBackground(Color.WHITE);
drwArea.setOpaque(true);
drwArea.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
drwArea.addMouseMotionListener(new MouseMotionAdapter()
{
#Override
public void mouseDragged(MouseEvent e)
{
//Write code to paint on the image...
}
});
this.getContentPane().add(drwArea);
//Clear button...
btnClear = new JButton("Clear");
btnClear.setBounds(410,50,70,30);
btnClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Write code to clear the image...
}
});
this.getContentPane().add(btnClear);
}
private class Canvas extends JLabel
{
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//The idea of overriding this method is
//achieving persistence...
}
}
}
I have seen that the typical component to draw on is a Jlabel (Is there anyone better by the way?). Using the method “getGraphics” I can use an object that has several methods to paint on the component. My problem is that instead of painting directly on the JLabel, I would like to paint on an image (in memory) and once the painting has finished, send the result to the JLabel. How can I do this? I'm a bit lost...
Thanks in advance.

I would like to paint on an image (in memory)
I suggest that you then create a BufferedImage object of the desired size, get its Graphics2D object by calling createGraphics() on it, and draw on it using this Graphics2D object.
and once the painting has finished, send the result to the JLabel
Then create an ImageIcon out of the BufferedImage above by simply calling
Icon myIcon = new ImageIcon(myBufferedImage);
and then setting the JLabel's icon via myLabel.setIcon(myIcon);
Also you could draw to a BufferedImage that is being displayed within a JPanel's paintComponent method, and this may be an even better way to go if you want to update the image while the program is running. For more on this, please have a look at some of these examples.
Other comments:
Don't draw with a Graphics object obtained by calling getGraphics() on a component. This will return a Graphics object that is short lived, risking disappearing graphics or worse, a NullPointerException. Instead, draw in the JPanel's paintComponent(...) method either directly, or indirectly by drawing on a BufferedImage (yes, you can get its Graphics object via getGraphics()) and then drawing the BufferedImage to the GUI within the paintComponent method.
While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

Related

Can't get Java's Swing to draw multiple (objects extending) JComponent

Background:
I'm tearing my hair here because I've used awt and Swing a couple of times, and I always run into a roadblock during my initial graphics setup and I never seem to have an "Of course, this thing again! I just have to something something"-moment. This time I can't seem to get even basic drawing to work!
Problem description:
I wish to draw a number of simple boxes in a JFrame (via a JPanel, if necessary). To this end I have a Sprite extends JComponent class which currently only draws simple geometric shapes but will eventually do things with BufferedImageor somesuch.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
#SuppressWarnings("serial")
public class Sprite extends JComponent {
private Rectangle rctBounds;
private Color clrFill;
public Sprite(int x, int y, Color color) {
this.rctBounds = new Rectangle(x, y, 100, 100);
this.clrFill = color;
}// Sprite(int,int,Color)
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(clrFill);
g.fillRect(rctBounds.x, rctBounds.y, rctBounds.width, rctBounds.height);
}// paintComponent(Graphics)
}// Sprite.class
The window for these to go in is obtained by a simple Win extends JFrame class á:
import javax.swing.JFrame;
public class Win extends JFrame{
public Win(){
setSize(800,600);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Finally, components are initialized and connected in the main method, in the class run, using JComponent's (I believe) add() method, called on an instance of Win:
import java.awt.Color;
public class run {
public static void main(String[] args) {
Win w = new Win();
Sprite s1 = new Sprite(10,100, Color.BLUE);
Sprite s2 = new Sprite(200,200, Color.RED);
Sprite s3 = new Sprite(300,200, Color.CYAN);
w.add(s1);
w.add(s2);
w.add(s3);
}//main()
}//run.class
Expectations vs. Reality:
Now, I expect a window with three colored boxes, as in this image (all boxes visible). Instead, only the final JComponent to be added shows up, as seen here (only cyan box painted). What's even more infuriating is that attempting to edit the JFrame's layout or adding a JPanel as an intermediary container for the Sprites results in a blank window.
Exasperated Plea:
I'm at a complete loss! Everything I find on here talks about improper use of paintComponent(Graphics) (using paint() instead, forgetting to #Override, not propagating the graphical context properly, etc) or to use a JPanel as an intermediary container. The most infuriating part of all this is that I've built two functioning programs using Swing before (school assignments) and, looking at that code, I cannot for the life of me figure out what I'm doing wrong here! Please help!

Java graphics PaintComponent problems. Can't seem to find the mistake

I'm making a program for drawing out an Image and it seems I've made a mistake and my program just doesn't want to draw out the image. Can someone please point out the mistake for mi because i really don't see it.
package basic_game_programing;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Practise extends JPanel {
public Image image;
//#####PAINT__FUNCTION#####
public void PaintComponent(Graphics g){
super.paintComponent(g);
ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
image = character.getImage();
g.drawImage(image,20,20,null);
g.fillRect(20, 20, 100, 100);
}
//######MAIN__FUCTION#######
public static void main(String[]args){
Practise panel = new Practise();
//SETTING UP THE FRAME
JFrame frame = new JFrame();
//
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(panel);
//SETTING UP THE PANEL
//
}
}
You're miscapitalizing paintComponent by using PaintComponent instead (note the first "P").
So Change PaintComponent to paintComponent.
Use the #Override annotation above the method to let the compiler tell you when you're making this kind of mistake.
Never read an image into a painting method since this slows down a method that needs to be fast, and makes the image be read in over and over when one read is enough.
The method should be protected not public.
Use ImageIO.read(...) to read in your image, and use resources and relative path within the jar file, rather than use files or ImageIcons.
Don't call setVisible(true) on the JFrame until after adding all components, else some might not show.
Do read the tutorials as most all of this is well explained there.
e.g.,
public class Practise extends JPanel {
private Image image;
public Practice() {
// read in your image here
image = ImageIO.read(.......); // fill in the ...
}
#Override // use override to have the compiler warn you of mistakes
protected void paintComponent(Graphics g){
super.paintComponent(g);
// never read within a painting method
// ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
// image = character.getImage();
g.drawImage(image, 20, 20, this);
g.fillRect(20, 20, 100, 100);
}
}

Cannot put image in Frame

I am a beginner programmer trying to create a Pacman game using Java eclipse. I am at the beginning of the process and I'm simply trying to get my main "Princess Pacman" character on the JFrame screen, but, I have this Override error popping up. I have also tried it with out override but it doesn't seem to be working for me that way either.
Here is my code:
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.awt.event.KeyEvent;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Pacman extends JFrame {
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static void main(String args[]){
Pacman gui = new Pacman();
gui.setVisible(true);
}
BufferedImage princess = null;
public Pacman(){
super("Princess Pacman");
//set size of playing space
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
princess =ImageIO.read(new File("images/Elsa.jpeg"));
}
catch (IOException e){
System.out.println("image not found");
}
}
#Override
public void draw(Graphics2D g){
g.drawImage(princess.getScaledInstance(100, 100, Image.SCALE_DEFAULT), 0, 0, this);
}
}
draw is not a method defined by JFrame or any of its parent classes, therefore it can't be override
draw is never called by anything that actually paints
You should avoid painting directly to top level containers, there's just to many things been painted onto it.
You can use a JLabel, but there are issues with this. Instead, create a custom class extending from JPanel and override its paintComponent method, making sure you call super.paintComponent before you render the image
Take a closer look at Painting in AWT and Swing, Performing Custom Painting and this for example
You're trying to override a method that does not exist in the JFrame class. Remove the override annotation.

Assistance with java repaint() method?

I'm trying to make a rectangle to move across the screen, but instead it's just repainting and not getting rid of the preceding rectangle making it look like a giant rectangle across the screen. Any help would be appreciated, here's my code:
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
#SuppressWarnings("serial")
public class Test extends JFrame implements ActionListener{
int x=50;
Timer tm = new Timer(30,this);
public static void main(String[] args){
new Test();
}
public Test(){
this.setSize(700, 500);
this.setTitle("Drawing Shapes");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
Graphics2D graph2 = (Graphics2D)g;
Shape Rect = new Rectangle2D.Float(x, 50, 50, 30);
graph2.setColor(Color.RED);
graph2.fill(Rect);
tm.start();
}
public void actionPerformed(ActionEvent e){
x=10+x;
repaint();
}
}
Draw in a JPanel that is held by and displayed within the JFrame. Don't draw directly in the JFrame as this risks drawing over things that shouldn't be messed with such as root panes, borders, child components,... Also you lose the benefits of automatic double buffering by drawing directly in the JFrame's paint method leading to choppy animation.
You should override the JPanel's paintComponent method not its paint method.
Always call the super's painting method in your own painting method (here it again should be paintComponent). This is your problem. So again, your paintComponent(Graphics g) override painting method should call super.paintComponent(g); on its first line. This will erase the old images.
You're breaking the paint chain...
public void paint(Graphics g){
// Broken here...
Graphics2D graph2 = (Graphics2D)g;
Shape Rect = new Rectangle2D.Float(x, 50, 50, 30);
graph2.setColor(Color.RED);
graph2.fill(Rect);
tm.start();
}
You MUST call super.paint. See Painting in AWT and Swing and Performing Custom Painting for more details about painting in Swing...
Top level containers are not double buffered and it is not recommended to paint directly to them, instead, create a custom component which extends from something like JPanel and override it's paintComponent method (calling super.paintComponent before performing any custom painting)
As a general rule of thumb, you should avoid extending from top level containers like JFrame, as you are not adding any new functionality to the class and they lock you into a single use-case, reducing the re-usability of your classes
DON'T call tm.start inside the paint method, you should do nothing in the paint methods except paint, never try and modify the state or otherwise perform an action which might indirectly modify the state of a component, this is a good way to have you program consume your CPU
To add to what other have already stated,
I've noticed that you use this.setLocationRelativeTo(null) for a simple application. Not saying it is bad, but you might want to check this thread to make sure it is what you want.
How to best position Swing GUIs?

graphics drawxxx method does not work

I am new to graphics.
I got this code from open source.It should paint String "HEeelo" on jframe,but it does not.Can anyone explain why it is not working properly and the principle of paint method?Why should it edit JFrame as it is just method which is not even called from main?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import javax.swing.JFrame;
public class view extends JFrame{
public view(){
this.setSize(new Dimension(250, 250));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
g.drawString("heello", 10, 10);
}
public static void main(String []args){
new view();
}
}
Get rid of that tutorial, that is not the way to do painting.
First the basic problems are that you should always invoke super.paint(...) when overriding a method. Secondly the text won't show because it is being painted under the title bar. You need to increase the y offset:
super.paint(g);
g.drawString("heello", 10, 40);
and the principle of paint method
Read the section from the Swing tutorial on Custom Painting for the proper way to do this. Basically you override the paintComponent() method of a JPanel and then add the panel to the frame. You should not do custom painting on a frame directly.

Categories