I am trying to make a simple game in Java, All i need to do is draw an im onto the screen and then wait for 5 seconds then 'undraw it'.
Code (this class draws the image on the screen):
package com.mainwindow.draw;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class MainWindow extends JPanel{
Image Menu;
String LogoSource = "Logo.png";
public MainWindow() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(LogoSource));
Menu = ii.getImage();
Timer timer = new Timer(5, new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
});
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(Menu, 0, 0, getWidth(), getHeight(), null);
}
}
Code #2 (this creates the JFrame)
package com.mainwindow.draw;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class Frame extends JFrame {
public Frame() {
add(new MainWindow());
setTitle("Game Inviroment Graphics Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(640, 480);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new Frame();
}
}
Use some kind of flag to determine if the image should be drawn or not and simply change it's state as needed...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (draw) {
g2.drawImage(Menu, 0, 0, getWidth(), getHeight(), null);
}
}
Then change the state of the flag when you need to...
Timer timer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
draw = false;
repaint();
}
});
Note: It's not recommended to to override paint, paint is at the top of the paint change and can easily break how the paint process works, causing no end of issues. Instead, it's normally recommended to use paintComponent instead. See Performing Custom Painting for more details
Also note: javax.swing.Timer expects the delay in milliseconds...5 is kind of fast...
Related
I have three classes, the first one is the main window smthn like
public class Starter extends JFrame{
JButton b=new JButton("Game");
Starter(){
setSize(200,200);
add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TryGraph gg=new TryGraph();
}
});
setVisible(true);
}
public static void main(String [] args){
Starter g= new Starter();
}
}
Then the second class is a window that has a panel where the graphics is going to be drawn on
public class TryGraph {
static int w=640,h=480;
TryGraph(){
JFrame mF=new JFrame();
GPan pan=new GPan();
mF.setLocationRelativeTo(null);
mF.setResizable(false);
mF.setSize(w,h);
mF.add(pan);
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mF.setVisible(true);
pan.playGame();
}
public static void main(String []args){
TryGraph t=new TryGraph();
}
}
Then lastly the panel class which does the drawing
public class GPan extends JPanel{
private boolean running;
private BufferedImage image;
private Graphics2D g;
int x,y;
public GPan(){
x=TryGraph.w;
y=TryGraph.h;
init();
}
public void init(){
running=true;
image=new BufferedImage(x,y,BufferedImage.TYPE_INT_RGB);
g=(Graphics2D)image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
public void playGame(){
while(running){
g.setColor(new Color(102,102,102));
g.fillRect(0,0,x,y);
g.setColor(new Color(255,0,0));
g.fillOval(0, 0, 100,100);
repaint();
}
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(image,0,0,x,y,null);
g2.dispose();
}
}
The problem is if I'm using an event from the Starter class window to the TryGraph class window the GPan panel won't draw the graphics when it loops through play game() method. But when the TryGraph class is executed directly it works fine
A couple of problems I noticed.
When you call main in TryGraph, you create a new instance. But you already have an instance of TryGraph when the button was pressed.
Don't extend JFrame. It's bad practice. Just create an instance of it.
Only use one static main entry point.
The big problem is that you put repaint() in a tight loop. Don't do that. Just call repaint(). Remember that repaint is run on the Event Dispatch Thread. So if that thread is tied up, nothing else will work.
Don't dispose of your graphics context. You can do that if you create a new one but otherwise don't get rid of it.
Finally, put super.paintComponent() as the first statement in your JPanel paintComponent method.
I suggest you read about painting and the EDT in the Java Tutorials.
Here is the modified code, all in one file.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Starter {
JButton b = new JButton("Game");
JFrame frame = new JFrame();
Starter() {
frame.setSize(200, 200);
frame.add(b);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TryGraph gg = new TryGraph();
}
});
frame.setVisible(true);
}
public static void main(String[] args) {
Starter g = new Starter();
}
}
class TryGraph {
static int w = 640, h = 480;
TryGraph() {
JFrame mF = new JFrame();
GPan pan = new GPan();
mF.setLocationRelativeTo(null);
mF.setResizable(false);
mF.setSize(w, h);
mF.add(pan);
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mF.setVisible(true);
pan.playGame();
}
}
class GPan extends JPanel {
private boolean running;
private BufferedImage image;
private Graphics2D g;
int x, y;
public GPan() {
x = TryGraph.w;
y = TryGraph.h;
init();
}
public void init() {
running = true;
image = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
public void playGame() {
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(new Color(102, 102, 102));
g2.fillRect(0, 0, x, y);
g2.setColor(new Color(255, 0, 0));
g2.fillOval(0, 0, 100, 100);
// g2.drawImage(image,0,0,x,y,null);
g2.dispose();
}
}
One additional suggestion. In your original code I believe you implemented a listener by implementing the interface in a private class. Good idea except you should extend the adapter class for the given interface. For example, for mouseListener, extend MouseAdapter. It provides dummy methods so you don't have to create them yourself. It helps make your code more readable. Of course, single method interfaces don't have ( or need) adapters.
I have a code for paint in a JPanel, but i dont know how to change colors outside the method paint()
I give the code:
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
for (Shape linea : lineas) { // dibuja todos las elipses
g2.draw(linea);
}
}
I only know to change the color at that method using g2.setColor(Color.RED); but I want to change it in an actionListener of a MenuItem.
Simply add a Color variable in your class and use that variable to set the color on your Graphics. It is also more appropriate to override paintComponent rather than paint and calling the super-method is always recommended.
See this example that randomly sets the color of the panel.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestColoredPanel {
private static class ColoredPanel extends JPanel {
private Color color = Color.RED;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getColor());
g.fillRect(0, 0, getWidth(), getHeight());
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
}
protected void initUI() {
final JFrame frame = new JFrame(TestColoredPanel.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ColoredPanel panel = new ColoredPanel();
frame.add(panel);
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Random r = new Random();
panel.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
}
});
frame.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestColoredPanel().initUI();
}
});
}
}
I am working on an application that displays GPS tracks on a map. I want to draw the track as a colored path of arbitrary thickness. I found the GeneralPath class which seems to do just what I want. However, I want the colored path to also have a black border. I couldn't find anything about how to add a border to a path, so I came up with a quick hacky solution of drawing a thick black path first, and then drawing a thin colored path on top.
SSCCE:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PathBorder {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PathBorder window = new PathBorder();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public PathBorder() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
GeneralPath path;
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(5.5f));
g2d.setColor(Color.BLACK);
path = new GeneralPath();
path.moveTo(0, 0);
path.lineTo(100, 100);
path.lineTo(200, 100);
path.lineTo(100, 80);
g2d.draw(path);
g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.YELLOW);
g2d.draw(path);
}
};
frame.setBackground(Color.CYAN);
frame.add(panel);
}
}
Here is a pic (taken from the SSCCE) to highlight my problem. See the red circle below, notice how the outside border has a gap in it. I want that gap to be filled in so the border is continuous.
Here are some actual screenshots from my app of a real track:
If you look closely at the sharp hairpin turn in the lower right of the track, you'll see that the border gets lost briefly... the detailed pic below makes it more clear.
I am not sure exactly how to fix this, but I'm open to suggestions, either keeping with the GeneralPath strategy, or using a different idea entirely.
Experiment with the cap and join parameters for a better effect. E.G.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PathBorder {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PathBorder window = new PathBorder();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public PathBorder() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
GeneralPath path;
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
BasicStroke s = new BasicStroke(
5.5f,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2d.setStroke(s);
g2d.setColor(Color.BLACK);
if (path==null) {
path = new GeneralPath();
path.moveTo(0, 0);
path.lineTo(100, 100);
path.lineTo(200, 100);
path.lineTo(100, 80);
}
g2d.draw(path);
g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.YELLOW);
g2d.draw(path);
}
};
frame.setBackground(Color.CYAN);
frame.add(panel);
}
}
Problem: I am trying to update the canvas with new painting objects based on user action. The canvas dosent get updated.
What i have done: The user interacts with the DnD action,The transferrable object reaches the canvas, Calls an update graphics method created by me. And the method simply uses the aldready created graphics 2d object and draws images using it.I have checkd the DnD action,the object is properly recived at canvas class and i was able to print them out using System.out.println.
A sample code,that has a similar function to that of mine,
Paint class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class PaintPanel extends JPanel{
private Graphics2D drawImage;
public PaintPanel()
{
}
#Override
public void paint(Graphics g) {
drawImage = (Graphics2D) g;
drawImage.setColor(Color.WHITE);
drawImage.fillRect(0, 0, getWidth(), getHeight());
}
public void updateGraphics(int length,int width)
{
drawImage.setColor(Color.black);
drawImage.drawRect(100, 150, length, width);
repaint();
}
}
mainframe class:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainPaint extends JFrame{
public MainPaint()
{
setTitle("test paint");
setSize(400,400);
setLayout(new BorderLayout());
final PaintPanel paintPan = new PaintPanel();
JButton testButon = new JButton("Display shape");
add(paintPan,BorderLayout.CENTER);
add(testButon,BorderLayout.PAGE_END);
testButon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
paintPan.updateGraphics(50,50);
repaint();
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MainPaint();
}
}
Graphics2D drawImage; //source of the problem!
Don't attempt to cache a Graphics (or Graphics2D) instance! Instead:
Add the new objects to a list
Call repaint().
In paintComponent(Graphics) draw the list of objects.
An alternative to that is to use a BufferedImage as the drawing object. See this answer for an example.
Update - SSCCE based on latest code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPaint extends JFrame {
public MainPaint() {
setTitle("test paint");
setSize(400, 400);
setLayout(new BorderLayout());
final PaintPanel paintPan = new PaintPanel();
JButton testButon = new JButton("Display shape");
add(paintPan, BorderLayout.CENTER);
add(testButon, BorderLayout.PAGE_END);
testButon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
paintPan.updateGraphics(50, 50);
repaint();
}
});
setVisible(true);
}
public static void main(String[] args) {
new MainPaint();
}
}
class PaintPanel extends JPanel {
private int x, y;
private Color color = null;
public PaintPanel() {
setBackground(Color.ORANGE);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D drawImage = (Graphics2D) g;
if (color != null) {
drawImage.setColor(color);
drawImage.drawRect(100, 150, x, y);
}
}
public void updateGraphics(int length, int width) {
color = Color.RED;
x = length;
y = width;
repaint();
}
}
Note
There are still a number of things about that code that need changing. I decided to stop at the earliest variant that worked to display the rectangle on button click.
I think you need to call the validate() method.
I'm trying to build a simple paint tool. The mouseDrag events creates a new ellipse and causes my JPanel to repaint().
This works fine so far.
However, if I press any button (or any other UI component) before firing the mouseDrag event for the first time, the button is painted in the upper left corner of my panel.
I have isolated the code into this test application:
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame
{
public Test()
{
final JPanel paintPanel = new JPanel(){
#Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaintMode();
g2d.setStroke(new BasicStroke(1));
g2d.fillRect(100, 100, 10, 10);
}
};
paintPanel.setPreferredSize(new Dimension(300,300));
paintPanel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e)
{
paintPanel.repaint();
}
});
this.setLayout(new FlowLayout());
this.add(paintPanel);
this.add(new JButton("Dummy"));
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String... args)
{
new Test();
}
}
A Screenshot for "seeing" the problem in my Main application
+1 to #MadProgrammer's answers.
You should have super.paintComponent(..) as the first call in your overriden paintComponent()
Do not extend JFrame unnecessarily
Create and minipulate Swing components via EDT
Dont call setPrefferedSize() rather override getPrefferedSize()
Here is an example which incorporates my advice's and #MadProgrammer's:
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
JFrame frame;
public Test() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final PaintPanel paintPanel = new PaintPanel();
paintPanel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
paintPanel.addRect(e.getX(), e.getY());
}
});
frame.setLayout(new FlowLayout());
frame.add(paintPanel);
frame.add(new JButton("Dummy"));
frame.pack();
frame.setVisible(true);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
}
class PaintPanel extends JPanel {
public PaintPanel() {
addRect(100, 100);
}
ArrayList<Rectangle> rects = new ArrayList<>();
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaintMode();
for (Rectangle r : rects) {
g2d.setStroke(new BasicStroke(1));
g2d.fillRect(r.x, r.y, r.width, r.height);
}
}
public void addRect(int x, int y) {
rects.add(new Rectangle(x, y, 10, 10));
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
You're not calling super.paintComponent.
The graphics context used for a paint cycle is shared between all the components begin painted, this means if you don't take care to clear it before painting onto, you will end up with what ever was painted before you.
One of the jobs of paintComponent is to prepare the graphics for painting