Calling repaint() draws the new shape, but leaves the old shape there - java

I'm sure this is a simple answer, but I can't figure it out.
I am trying to make a basic shape that I can control in a window. Obviously it will be more involved when the whole project is complete, but I am working on the early steps still. I am using WindowBuilder to make the layout, and have a JPanel and a JButton. The JPanel draws a rectangle, and has a method to move it. The JButton calles that moving command. And that's it. The problem is in the repaint. The shape keeps all old versions of itself, and the button makes weird copies of itself. All these go away when I resize the window, which I thought was the same as calling repaint. Again, I'm sure is something simple I am missing. Below are my 2 classes.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Drawing {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Drawing window = new Drawing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Drawing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
drawpanel panel = new drawpanel();
panel.setBounds(58, 68, 318, 182);
frame.getContentPane().add(panel);
JButton btnMove = new JButton("move");
btnMove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.moves();
}
});
btnMove.setBounds(169, 34, 89, 23);
frame.getContentPane().add(btnMove);
}
}
^ This one, besides the buttonListener, was autocreated by WindowBuilder.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class drawpanel extends JPanel {
int x = 50, y = 50;
int sizeX = 50, sizeY = 50;
public void paintComponent( Graphics g) {
super.paintComponents(g);
g.setColor(Color.BLACK);
g.drawRect(x, y, sizeX, sizeY);
}
public void moves() {
x +=5;
repaint();
}
}
^ This one has my drawing of the shape and the moving/repainting method. It was written mostly from other examples I found on this site.
Thanks to any help in advanced.

public void paintComponent(Graphics g) {
super.paintComponents(g); // wrong method! (Should not be PLURAL)
Should be:
public void paintComponent(Graphics g) {
super.paintComponent(g); // correct method!

Related

Java Graphics Only Paint on Button Click

I'm not sure how java graphics work. It seems
to be executing something itself so I am trying to break it
down.
I'm trying to create the blank JPanel and then only draw to it
once the JButton has been clicked but it doesn't work
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class testGui {
// global ======================================================================
static gui gc_gui;
// main ========================================================================
public static void main(String[] args) {
gc_gui = new gui();
gc_gui.cv_frame.setVisible(true);
listeners();
}
// action listeners ============================================================
public static void listeners() {
ActionListener ll_square = new ActionListener() {
public void actionPerformed(ActionEvent event) {
gc_gui.cv_content.draw(graphic);
}
};
gc_gui.cv_button.addActionListener(ll_square);
}
// gui =========================================================================
public static class gui {
JFrame cv_frame;
JButton cv_button;
content cv_content;
public gui() {
cv_frame = new JFrame();
cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cv_frame.setTitle("Test GUI");
cv_frame.setSize(600, 400);
cv_frame.setLayout(new FlowLayout());
cv_button = new JButton("Square");
cv_content = new content();
cv_content.setBackground(Color.BLACK);
cv_content.setPreferredSize(new Dimension(500, 300));
cv_frame.add(cv_button);
cv_frame.add(cv_content);
}
}
// content =====================================================================
public static class content extends JPanel {
public void paint(Graphics graphic) {
super.paint(graphic);
}
public void update() {
super.repaint();
}
public void draw(Graphics graphic) {
Graphics2D graphic2D = (Graphics2D) graphic;
graphic2D.setPaint(Color.RED);
graphic2D.fillRect(10, 10, 100, 100);
}
}
}
I create the content JPanel without the draw function being
called and then I try to call it using my ActionListener
although it is crashing because of the graphic variable.
What is the correct way to use the java graphics utility?
UPDATE
Maybe I'm not asking this question right but it is possible to
create a blank image.
Then draw additional images to that images (squares) after a button
has been clicked?
not just updating the dimensions using global variables but generating
new images to that existing image
but it is possible to create a blank image. Then draw additional images to that images (squares) after a button has been clicked?
Check out Custom Painting Approaches for two common ways to do painting.
The example allows you to draws Rectangles with the mouse. In your case the logic will be simpler as you would just invoke the addRectangle(...) method when you click a button. Of course you need to set the size/location of the Rectangle somehow.
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.JPanel;
import javax.swing.border.LineBorder;
public class TestGui {
public static Content content;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Test GUI");
frame.setSize(429, 385);
frame.getContentPane().setLayout(null);
JButton cv_button = new JButton("Square");
cv_button.setBounds(10, 159, 70, 23);
frame.getContentPane().add(cv_button);
cv_button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Content.isToDraw = true;
content.paintImmediately(content.getBounds());
Content.isToDraw = false;
}
});
JButton cv_buttonClear = new JButton("Clear");
cv_buttonClear.setBounds(10, 179, 70, 23);
frame.getContentPane().add(cv_buttonClear);
cv_buttonClear.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
content.paintImmediately(content.getBounds());
}
});
content = new Content();
content.setBorder(new LineBorder(new Color(0, 0, 0)));
content.setBounds(87, 11, 287, 312);
frame.getContentPane().add(content);
frame.setVisible(true);
}
}
class Content extends JPanel {
public static Boolean isToDraw = false;
public void paintComponent(Graphics arg0) {
if (isToDraw) {
arg0.setColor(Color.RED);
arg0.fillRect(0, 0, getWidth(), getHeight());
} else {
super.paintComponent(arg0);
}
}
}

Java Isn't Displaying gui right

I have a simple gui and when I run is it is perfect, but occasionally it doesn't display right. I have a circle in a JPanel. When it runs right it displays in the middle for the JFrame like I intended it to, but when it does not work right the circle appears closer to the bottom of the screen. How can I fix this so it displays right each time? Is my code incorrect (I hope not! :) ), or is it some bug in java. So here is my code:
Update: It seems that the window height is changing.
Run.java--------------------------------------------------------------------------------
import javax.swing.JFrame;
public class Run {
public static void main(String args[]) {
Window w = new Window();
w.setSize(800, 500);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setVisible(true);
}
}
Window.java----------------------------------------------------------------------------
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
super("Wheel");
Gui g = new Gui();
add(g);
}
}
Gui.java--------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Gui extends JPanel {
private Color wheelColor = new Color(0, 0, 255);
public Gui() {
setOpaque(true);
setBackground(new Color(255, 0, 0));
}
public void paintComponent(Graphics g) {
g.setColor(wheelColor);
g.fillOval(40, 40, 420, 420);
}
}
Also I'm using Ubuntu, I don't know if that would affect it at all. Thanks in advance.
Make sure you create the UI within the context of the Event Dispatching Thread, see Initial Threads for more details.
Make sure you are calling super.paintComponent within the paintComponent method before you do any custom painting, see Painting in AWT and Swing for more details
Don't rely on magic numbers, the size of the window is the size of it's content + its frame decorations. You should get using getWidth and getHeight to determine the size of the Gui panel and overriding getPreferredSize to allow the window to determine how much room it needs to (optimally) display it
For example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Run {
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();
}
Window w = new Window();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.pack();
w.setLocationRelativeTo(null);
w.setVisible(true);
}
});
}
public static class Window extends JFrame {
public Window() {
super("Wheel");
Gui g = new Gui();
add(g);
}
}
public static class Gui extends JPanel {
private Color wheelColor = new Color(0, 0, 255);
public Gui() {
setOpaque(true);
setBackground(new Color(255, 0, 0));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(440, 440);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(wheelColor);
int width = getWidth();
int height = getHeight();
int x = (width - 420) / 2;
int y = (height - 420) / 2;
g.fillOval(x, y, 420, 420);
}
}
}
Also beware of naming your classes after classes that already exist within the default API, java.awt.Window already exists and can cause confusion not only for yourself, but for other developers ;)

Java JLayeredPane doesn't show it's elements

I am trying to use JLayeredPane to overlay one JPanel on top of another. For some reason the added panels don't show up.
This is the code that creates the JLayeredPane and adds elements to it:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;
public class CarAnimator extends JFrame {
public CarAnimator()
{
JLayeredPane racingOverlay = new JLayeredPane();
CarAnimatorJPanel animation = new CarAnimatorJPanel();
Racetrack racetrack = new Racetrack();
racingOverlay.add(racetrack,JLayeredPane.DEFAULT_LAYER);
racingOverlay.add(animation,new Integer(2));
racingOverlay.setBorder(BorderFactory.createTitledBorder("Can't see a thing"));
this.getContentPane().add(racingOverlay,BorderLayout.CENTER);
this.setLocationByPlatform(true);
this.setPreferredSize(new Dimension(850,650));
this.setMaximumSize(new Dimension(850,650));
this.setMinimumSize(new Dimension(850,650));
this.setResizable(false);//prevents user from resizing the window
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> {
(new CarAnimator()).setVisible(true);
});
}
}
This is the code for the racing track JPanel:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Racetrack extends JPanel
{
Graphics g = this.getGraphics();
#Override
public void paintComponent(Graphics g)
{
Color c1 = Color.green;
g.setColor( c1 );
g.fillRect( 150, 200, 550, 300 ); //grass
Color c2 = Color.black;
g.setColor( c2 );
g.drawRect(50, 100, 750, 500); // outer edge
g.drawRect(150, 200, 550, 300); // inner edge
Color c3 = Color.yellow;
g.setColor( c3 );
g.drawRect( 100, 150, 650, 400 ); // mid-lane marker
Color c4 = Color.white;
g.setColor( c4 );
g.drawLine( 425, 500, 425, 600 ); // start line
}
}
And this is the JPanel animation example, taken from Java: How to Program book (http://java.uom.gr/~chaikalis/javaLab/Java_HowTo_9th_Edition.pdf), adapted to work with my code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CarAnimatorJPanel extends JPanel
{
protected ImageIcon images[];
private int currentImage=0;
private final int ANIMATION_DELAY=50;
private int width;
private int height;
private Timer animationTimer;
public CarAnimatorJPanel()
{
try
{
File directory = new File("C://Users/eltaro/Desktop/Car images");
File[] files = directory.listFiles();
images = new ImageIcon[files.length];
for (int i=0;i<files.length;i++)
{
if (files[i].isFile())
{
images[i]=new ImageIcon(ImageIO.read(files[i]));
}
}
width = images[0].getIconWidth();
height = images[0].getIconHeight();
}
catch(java.io.IOException e)
{
e.printStackTrace();
}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
images[currentImage].paintIcon(this,g,0,0);
if(animationTimer.isRunning())
{
currentImage=(currentImage+1)%images.length;
}
}
public void startAnimation()
{
if(animationTimer==null)
{
currentImage=0;
animationTimer=new Timer(ANIMATION_DELAY, new TimerHandler());
animationTimer.start();
}
else
{
if(!animationTimer.isRunning())
{
animationTimer.restart();
}
}
}
public void stopAnimation()
{
animationTimer.stop();
}
#Override
public Dimension getMinimumSize()
{
return getPreferredSize();
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(width,height);
}
private class TimerHandler implements ActionListener
{
#Override
public void actionPerformed(ActionEvent actionEvent)
{
repaint();
}
}
}
When I add the two JPanels to the JLayeredPane inside CarAnimator constructor, they fail to show:
Some recommendations:
Since I'm guessing that your CarAnimatorJPanel sits on top of a background JPanel, you'd better set CarAnimatorJPanel to be non-opaque via setOpaque(false) so that the JPanel behind it can be seen.
When adding components to a JLayeredPane, you're essentially adding the components to a null layout. This means that you absolutely need to set the sizes (or override getSize()) of those components, not the preferred sizes.
As always, when dealing with image files, debug that you're able to adequately read in the image in a separate small test program. As usual, consider getting the image in as a resource and not as a File.
You have some program logic code in your paintComponent(Graphics g) method -- you change the state of the currentImage field. Never do this since you never have full control over when or even if paintComponent is called. This bit of code belongs in your animation loop, your Swing Timer's ActionListener.

I'm trying to draw a filled rectangle over an imageIcon using the coordinates of the image and the rectangle appears off

Eventually after I work out this small detail it will receive a building and room number to outline said building and room number so it is easy to locate but I can't get the rectangle to draw even close to acurately over a single room.
package programSTLApp;
/*
Program to request the classroom no. in STLCC and Display the location of
that classroom.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class STLApp extends JFrame
{
private JLabel imageLabel;
private JButton button;
private JPanel imagePanel;
private JPanel buttonPanel;
public STLApp()
{
super("My STLCC Class Locator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildImagePanel();
buildButtonPanel();
add(imagePanel, BorderLayout.CENTER);
add(buttonPanel,BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildImagePanel()
{
imagePanel = new JPanel();
imageLabel = new JLabel("Click the button to see the drawing indicating "
+ "the location of your class");
imagePanel.add(imageLabel);
}
private void buildButtonPanel()
{
buttonPanel = new JPanel();
button = new JButton("Get Image");
button.addActionListener(new ButtonListener());
buttonPanel.add(button);
}
private class ButtonListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
ImageIcon SiteLayoutFV = new ImageIcon("D:\\B120.jpg");
imageLabel.setIcon(SiteLayoutFV);
imageLabel.setText(null);
pack();
}
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.RED);
g.fillRect(55,740,164,815);
}
public static void main(String[] args)
{
new STLApp();
}
}
As has already being pointed out, top level containers ain't a studiable class for performing custom painting, there is just to much going with these containers to make it easy to paint to.
Instead, create yourself a custom component, extending from something like JPanel, and override it's paintComponent method.
Once you have the floor pane rendered, you can render you custom elements over the top of it.
How you store this information is up to you, but basically, you need some kind of mapping that would allow you to take the floor/room and get the Shape that should be rendered.
Because the floor map might float (it may not always be rendered at 0x0 for example), you need to be able to translate the coordinates so that the Shape will always match.
Take a look at...
Performing Custom Painting
2D Graphics
For more details
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FloorPlan {
public static void main(String[] args) {
new FloorPlan();
}
public FloorPlan() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage floorPlan;
private Rectangle myOffice = new Rectangle(150, 50, 32, 27);
public TestPane() {
try {
floorPlan = ImageIO.read(new File("floorPlan.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return floorPlan == null ? new Dimension(200, 200) : new Dimension(floorPlan.getWidth(), floorPlan.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (floorPlan != null) {
int x = (getWidth() - floorPlan.getWidth()) / 2;
int y = (getHeight() - floorPlan.getHeight()) / 2;
g2d.drawImage(floorPlan, x, y, this);
g2d.setColor(Color.RED);
g2d.translate(x, y);
g2d.draw(myOffice);
}
g2d.dispose();
}
}
}

How to make shapes "blink" or change colors in Java?

I'm trying to learn how to draw a shape, and be able to a) draw it, "freeze" the process, draw it in the color of the background, and then re-draw it in the original color and b) draw a shape and change its color. All I have so far is (for blinking):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Carlight extends JPanel{
Thread th=new Thread();
public void paintComponent (Graphics g){
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillOval(25, 25, 10, 10);
try{
th.sleep(10);
}catch(InterruptedException e){}
repaint();
g.setColor(Color.yellow);
g.fillOval(25, 25, 10, 10);
try{
th.sleep(10);
}catch(InterruptedException e){}
repaint();
g.setColor(Color.yellow);
g.fillOval(25, 25, 10, 10);
}
public Carlight(){
JFrame frame=new JFrame();
frame.setTitle("Carlights");
frame.add(this);
frame.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,150);
frame.setVisible(true);
}
public static void main(String[] args){
new Carlight();
}
}
How do I make this code work and how do I get a shape to change color?
import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class carlight extends JPanel
{
private Color lastColor = Color.YELLOW;
// For telling the panel to be repainted at regular intervals
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
#Override
public void paintComponent(Graphics g)
{
super.paintComponents(g);
if(lastColor.equals(Color.YELLOW))
{
lastColor = Color.GREEN;
}
else
{
lastColor = Color.YELLOW;
}
g.setColor(lastColor);
g.fillOval(25, 25, 10, 10);
}
public carlight()
{
JFrame frame = new JFrame();
frame.setTitle("Carlights");
frame.add(this);
frame.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 150);
frame.setVisible(true);
service.scheduleAtFixedRate(new Runnable()
{
public void run()
{
repaint();
}
}, 0, 1, TimeUnit.SECONDS);
}
public static void main(String[] args)
{
new carlight();
}
}
Ok,
Don't put sleep calls during a paintComponent call. It means you are
forcing the UI to hang/stall. This is very bad.
I have created a ScheduledExecutorService for regularly calling the repaint method.
The paint method changes the color of the lastColor. Most of the time you would be looking at some sort of model to find out of the state of it to choose which color you should be using.
If the problem is you aren't meant to change the color, but instead it is an off/on situation, you should have a boolean representing state and only draw the circle if it is on.

Categories

Resources