I have been having trouble rotating one of my sprites in a game I'm working on. I have followed a tutorial about rotating images on JPanels about the center of the image (which was very well-done). I even created a simple project that works just fine.
However, when I tried to use the same technique on my game, my sprite will not rotate. I have determined that the problem is drawing the sprite, as I have checked in the paintComponent(Graphics g) method via a println() statement that the rotation value is updated properly and that the repaint() method is being called when appropriate.
Here is the relevant code to the issue (excludes unnecessary methods and such):
Highest-level class:
public abstract class GameObject extends JPanel {
protected BufferedImage image;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw sprite
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, 0, 0, null);
// Clean up
g.dispose();
g2.dispose();
}
}
Lowest-level class:
// Entity is a subclass of GameObject.
// It does not override paintComponent.
// All it does is add an update method that is called every game tick.
public abstract class MicroObject extends Entity {
protected Location location; // variable to store position and rotation
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(this.getWidth() / 2, this.getHeight() / 2);
g2.rotate(Math.toRadians(location.getRotation()));
// In the following two lines, image is inherited from GameObject
g2.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2.drawImage(image, 0, 0, null);
g2.dispose();
g.dispose();
}
}
I know this isn't necessarily a unique question, but I've looked at all of the "duplicate" threads, and they've all left me with similar answers, but the same problem in the end. I would appreciate it if someone took the time to look at my code and see where I went wrong.
Thank you all!
Don't dispose those Graphics objects! They're re-used by Swing to draw children components & borders.
The reason why it's not working is because GameObject is disposing the graphics object before MicroObject can use it.
Also, there's no reason to draw the image twice. Remove the code in GameObject's paintComponent().
Lastly, just use classes. There's no reason for those to be abstract.
So:
Highest-level class:
public class GameObject extends JPanel {
protected BufferedImage image;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Don't draw sprite. Subclass will do that.
// Don't clean up. Swing does that for us.
}
}
Lowest-level class:
// Entity is a subclass of GameObject.
// It does not override paintComponent.
// All it does is add an update method that is called every game tick.
public class MicroObject extends Entity {
protected Location location; // variable to store position and rotation
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(this.getWidth() / 2, this.getHeight() / 2);
g2.rotate(Math.toRadians(location.getRotation()));
// In the following two lines, image is inherited from GameObject
g2.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2.drawImage(image, 0, 0, null);
}
}
Related
I'm new to Java and don't know exactly what the cause.Let me explain the issue
I created a Rectangle Shape and its working, then i thought about changing its color to black for some testing but it seems not working below is my code.
When i call the method from paintComponent itself then its working but if i do the same from any other method then its not changing the color. I tried calling the method repaint also but still the same
public class Meme extends JPanel {
Rectangle2D.Float myRect = new Rectangle2D.Float(90, 90, 90, 90);
Graphics2D graphics2d;
public void DRAW() {
graphics2d.setColor(new Color(0, 0, 200));
graphics2d.fill(myRect);
}
public void ChangeColour() {
System.out.println("Called");
graphics2d.setPaint(Color.BLACK);
System.out.println("Called2");
graphics2d.fill(myRect);
System.out.println("Called3");
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
graphics2d = (Graphics2D) g;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
DRAW();
}
}
Button click listener method
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
meme1.ChangeColour();
}
As far as I can remember, whenever you change some properties (color in this case), you have to call repaint. This will invoke a call to paintComponent and the frame will be drawn once again.
In your case, I am guessing even if you call repaint after changing color, the DRAW method gets called again in paintComponent which resets the changed color back to (0, 0, 200). Therefore, you don't see any change in the screen. But when you call changeColor in paintComponent method (assuming after the call to DRAW), the change of color persists and does not get overridden.
POSSIBLE SOLUTION
Just keep the color stored somewhere else. Like
Color myColor = new Color(0,0,200);
then in DRAW:
private void DRAW() {
graphics2d.setColor(myColor);
graphics2d.fill(myRect);
}
and in ChangeColor:
private void ChangeColour() {
myColor = Color.BLACK;
}
Hope it helps.
update your function like this
public void ChangeColour() {
System.out.println("Called");
graphics2d.setColor(new Color(1, 1, 200));
System.out.println("Called2");
graphics2d.fill(myRect);
System.out.println("Called3");
}
Painting in Swing is both passive and destructive. That is, a paint pass can occur at anytime for any number of reasons, many which you don't control. Destructive means, on each paint pass you are expected to repaint the entire component from scratch.
In Swing, you update the state you want change and then call repaint to trigger a new paint pass.
Painting should only ever paint the current state, it should never try and change it
public class Meme extends JPanel {
Rectangle2D.Float myRect = new Rectangle2D.Float(90, 90, 90, 90);
private Color color;
public void draw(Graphics2D graphics2d) {
graphics2d.setColor(color);
graphics2d.fill(myRect);
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public void ChangeColour() {
color = Color.BLACK;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
graphics2d = (Graphics2D) g.create();
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
draw(graphics2d);
graphics2d.dispose();
}
}
Also, the graphics context passed to your component is shared with all the other components, so it's important that any significant changes you make to the context are undone before the method exists - in most cases, it's just a simple case of calling create on the Graphics context to snapshot it state and dispose (on the copy you created) when you're done
I am trying to do a little program on Eclipse. The program goes like this: when I click for the 1st time on thr Panel on the frame, a line has to be drawn regarding the Y position of my mouse listener.The line takes all the width of the panel. On the 2nd click, another line has to be drawn, again regarding the Y position of where I clicked. After, I'll put a little circle between the 2 lines and make a little animation with it.
But now, I have a problem. When I click on the panel, a line is drawn, but if i click another time, the first line disappears and the 2nd line takes it place...
This is the code of the painComponent and my mousr listener. What is wrong with it ?
public Lines() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
posY=e.getY();
posX=e.getX();
nbClic=e.getClickCount();
repaint();
}
});
setBackground(Color.black);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
if(nbClic>=1){
line1=new Line2D.Double(0, posY, getWidth(), posY);
g2d.draw(line1);
repaint();
}
if(nbClic>=2){
g2d.setColor(Color.YELLOW);
line2=new Line2D.Double(0, posY, getWidth(), posY);
g2d.draw(line2);
}
repaint();
}
Painting is an event that draws the entire component. You can't depend on past events because they are erased each time a repaint happens.
You would need to keep something like a List and each time you create a new line, you add it to the List.
List<Integer> yClicks = new ArrayList<>();
... {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
yClicks.add(e.getY());
repaint();
}
});
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
for(int y : yClicks) {
g2d.draw(new Line2D.Double(0, y, getWidth(), y));
}
g2d.dispose();
}
Also:
Never call repaint inside paintComponent! This will cause an endless cycle of repaints.
paintComponent is a protected method and should remain so unless there is a compelling reason to make it public.
Be careful changing the state of the Graphics object passed in to paintComponent because it is used elsewhere. Usually we create a local copy which is disposed when we are done.
how to turn several graphics objects in to one?
(this part of code should generate tetris figure, where generate() create a figure)
public void paint(Graphics g){
Figure f = generate();
int length = f.getX()[0].length;
for(int j =0; j<f.getX().length;j++){
int xr=xs+10;
ys = 0;
for(int i=0;i<length;i++){
if (f.getX()[j][i] == 1){
int yr = ys+10;
Rectangle p = new Rectangle(xs,ys,xr,yr);
g.setColor(f.getY());
g.drawRect(p.x, p.y, p.width, p.height);
g.fillRect(p.x, p.y, p.width, p.height);
//g.translate(xs+40, ys+40);
}
ys+=10;
}
xs+=10;
}
xs=0;
ys=0;
//g.setColor(Color.white);
//g.drawRect(45, 95, 55, 105);
}
Well, I think you are starting with Java 2D, since your code has some problems.
First of all, you always need to call the paint version of the super class. This should be done because the component needs to have a chance to render itself properly. Take a look.
#Override
public void paint( Graphics g ) {
// you MUST do this
super.paint(g);
// continue here...
}
If you are dealing with a JFrame you will override the paint method. If you are working with some JComponent child, like JPanel, you need to override the paintComponent method, which has the same signature of paint, but it is protected, not public. You can override paint too, but in these cases (JComponent and its children), paint is a method that delegates the paint work to three methods (paintComponent, paintBorder, and paintChildren), so the best option is to override paintComponent.
Another detail. The best way to work with graphics is to create a new graphics context based in the current one and dispose of it after using it. Take a look:
#Override
public void paint( Graphics g ) {
// you MUST do this
super.paint(g);
Graphics newG = g.create();
// or Graphics2D newG2d = (Graphics2D) g.create();
// do your work here...
newG.dispose(); // disposes the graphics context
}
The graphics context that is created using the create method is a copy of the current graphics context (with the same states), but changing it does not affect the original one, so doing this, you will not mess with the state of the original graphics context.
To finish, I think that you need to have a draw method in your figure that receives the graphics context. So, the Figure instance will be responsible to draw itself. Something like:
public class Figure {
// figure's members...
public void drawMe( Graphics2D g2d ) {
// do the draw work here...
}
}
public class MyFrame extends JFrame {
#Override
public void paint( Graphics g ) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g.create();
// let's suppose that figureList is a List<Figure> or a Figure[]
for ( Figure f : figureList ) {
f.drawMe( g2d );
}
g2d.dispose();
}
}
Of course, you can create a new graphics context for each Figure if its draw method changes the graphics context too "deeply", like doing translations and rotations. You just need to dispose the new ones after using them.
I assume you are trying to put multiple components inside of an enclosing component so that you can move/manipulate them together.
One suggestion would be to add each of your objects to a panel object, like JPanel.
However it is somewhat unclear what you are trying to achieve exactly.
I want to make an application in which I can draw a path on a canvas. The problem is that I have to update this canvas continuously.
Currently I'm able to do it, but I have to redraw all the path every time and so I have to store all the points in memory. I would prefer to simply update the draw by adding a new point.
Is it possible?
Currently my code is:
public class MyCanvas extends Canvas{
private static final long serialVersionUID = 1L;
public MyCanvas(){}
public void paint(Graphics graphics){
super.paint(graphics);
graphics.setColor(Color.green);
// points is an ArrayList of Point2D
for (Iterator iterator = points.iterator(); iterator.hasNext();) {
Point2D point2d = (Point2D) iterator.next();
graphics.fillOval((int)((canvas.getWidth()/2.0) + point2d.getX()), (int)((canvas.getHeight()/2.0) + point2d.getY()), 5, 5);
}
}
}
Thanks!
EDIT
This is the current solution:
PanelCanvas canvasPanel;
...
public void drawCircle(int x, int y){
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.green);
g2d.setBackground(Color.white);
g2d.fillOval((int)((panelCanvas.getWidth() / 2.0) + x/10.0), (int)((panelCanvas.getHeight() / 2.0) + y/10.0), 5, 5);
panelCanvas.repaint();
}
public class CanvasPanel extends JPanel{
public void paintComponent(Graphics graphics){
super.paintComponents(graphics);
Graphics2D g2d = (Graphics2D)graphics;
g2d.setBackground(Color.white);
g2d.drawImage(bufferedImage, null, 0, 0);
}
}
Draw the points (whatever) to a BufferedImage. During paint(), draw the BufferedImage.
Note though, that the JRE can draw thousands of objects in paint without any visual artifacts or slow-down.
The canvas is embedded in a Swing GUI. What do you suggest for replacing AWT.Canvas?
JComponent for complete custom rendering, JPanel for custom rendering combined with components. It sounds like the JComponent would be better suited to this use-case.
For either of those, override paintComponent(Graphics) instead of paint(Graphics). The rest of the advice is the same.
I have a problem making an inner class that extends from JPanel to draw anything on it. I overrided the paintComponent method, and whatever I set to draw from here works fine, but using another method to draw does not work.
Here is my inner class code:
private class Plot extends JPanel {
public Plot() {
this.setBackground(Color.WHITE);
}
#Override
public void paintComponent(Graphics graphic) {
super.paintComponent(graphic);
Graphics2D graphic2d = (Graphics2D) graphic;
graphic2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphic2d.drawOval(0, 0, this.getWidth() - 1, this.getHeight() - 1);
}
public void drawTitle(final String title) {
Graphics2D graphic2d = (Graphics2D) this.getGraphics();
graphic2d.setColor(Color.red);
graphic2d.drawString(title, 1, 10);
}
}
Notice the drawTitle method. I just want a custom text to be shown. In my outer class which extends from a JFrame I create an instance of this inner class like this:
private Plot plot;
/** Creates new form GraphicsView */
public GraphicsView() {
initComponents();
plot = new Plot();
this.add(plot, BorderLayout.CENTER);
}
public void drawTitle(final String title) {
this.plot.drawTitle(title);
}
I even create a convenient method to call the inner class drawTitle method (with the same name). I do this because I want this JFrame outer class to be visible on button click, once it is visible (which ensures the init of the graphics) I call the outer class drawTitle which in turn calls the inner class method with the same name and where the string show be drawn... but this does not work, I can't see it on the panel. Here is my button click event:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
GraphicsView view = new GraphicsView();
view.setVisible(true);
view.drawTitle("Hello");
}
Thanks in advance, I will appreciate any help. :)
I overrided the paintComponent method, and whatever I set to draw from here works fine
Well, there is the answer to the question. Do all your drawing from the paintComponent() method.
but using another method to draw does not work.
Don't use the getGraphics() method. You should only ever use the Graphics objects passed to the paintComponent() method.
You can't control when Swing repaints() a component. Therefore every time the component is repainted the paintComponent() method is invoked and your other custom painting code will be lost.
Just call the drawTitle() function in the paintComponent override and pass the graphics as an argument. Something like this:
#Override
public void paintComponent(Graphics graphic) {
super.paintComponent(graphic);
Graphics2D graphic2d = (Graphics2D) graphic;
graphic2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphic2d.drawOval(0, 0, this.getWidth() - 1, this.getHeight() - 1);
drawTitle(graphic, title);
}
public void drawTitle(Graphics g, final String title) {
Graphics2D graphic2d = (Graphics2D) g;
graphic2d.setColor(Color.red);
graphic2d.drawString(title, 1, 10);
}
Also try to make the title a data member of the class. This might prove helpful later.