I have an array with multiple Grapics2D Objects and want to draw all of them in a Panel. Its not drawing anything. Does anyone know how to fix this?
for (Graphics2D g2d : models) {
myPanel.paint(g2d);
}
Related
I have the following task for my school in Java:
Create a GUI window with your own graphic. This graphic should be created in a separate JPanel class and drawn using the draw and fill methods of the java.awt.Graphics class (e.g. a house with a garden, a car, ...). The graphic should contain at least 5 different types of graphics (rectangle, oval, ...), at least one polygon (draw or fillPolygon (polygon p) method) and an arc (draw or fillArc method (int x, int y, int width, int height, int startAngle, int arcAngle)). The graphic should also contain at least 10 drawing elements and consist of at least 4 different colors.
But I don´t know how to use the class Graphics, so I don´t know how to create a Grahpics object and edit it. Does anyone know how to solve this? Thank you
You can use graphics with a JPanel;
class exampleclass extends JPanel {
exampleClass() {
...
}
#Override
public void paintComponent(Graphics g) {
...your code here...
}
}
For more information, look at; https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html
You can call paint method with, repaint();
Currently I have a JPanel with its paintComponent overridden with lots of image processing based on various states. When an event occurs (infrequent) many of the states change and the images that are drawn change. It doesn't seem the best way to keep doing all the processing every time the paintComponent is it possible to do something like when an event occurs draw everything to a Graphics2D instance and then merge this with the paintComponent one? Is this also the best way to do it?
As MadProgrammer suggested, storing rendered output can help you.
When the event that might change the image occurs, you can draw stuff to a BufferedImage like following.
private BufferedImage renderedImage;
public void triggerEvent(int width, int height) {
this.renderedImage = new BufferedImage(width, height, TYPE_INT_ARGB);
Graphics2D g = this.renderedImage.createGraphics();
// Paint things on g.
}
#Override
public void paintComponent(Graphics g) {
g.drawImage(this.renderedImage, 0, 0, this);
}
Hope this helps.
I'm trying to make a map editor that supports rectangles in different angles (so it draws rectangles using polygons). I want to catch a polygon by their location on the frame without using mathematical calculations.
Is there a command supports such thing?
I tried to catch polygons by their visual representations:
public void mousePressed(MouseEvent e){
Component component = getComponentAt(e.getX(), e.getY());
if(component instanceof wall){
but it doesn't work.
(If i was simply drawing rectangles i would use JPanel and use setbounds command to draw a rectangle, but i dont think i can make polygon-shaped JPanels)
You first need to create a List containing all the Polygons you want to paint:
Shape circle = new Ellipse2D.Double(0, 0, 30, 30);
List<Shape> shapes = new ArrayList<Shape>();
shapes.add( circle );
Then in your paintComponent() method you iterate through all the shapes in the List:
Graphics2D g2d = (Graphics2D)g.create();
for (Shape shape : shapes)
{
g2d.draw( shape );
}
g2d.dispose();
Then in the MouseListener you iterate through the List to see which Shape was clicked:
public void mousePressed(MouseEvent e)
{
for (Shape shape : shapes)
{
if (shape.contains(e.getPoint())
// do something
}
}
If i was simply drawing rectangles i would use JPanel and use setbounds command to draw a rectangle, but i dont think i can make polygon-shaped JPanels
For an alternative approach that does use a component check out Playing With Shapes. The classes there allow you to create a ShapeComponent by using any Shape.
You wouldn't use individual JPanels to draw each Polygon. You would use a single class that extends JPanel, then overrides the paintComponent() method to draw the Polygons. More info here.
Once you've drawn your Polygons to the JPanel, you can use the Polygon.contains() method to test whether the mouse was inside a JPanel. More info on that in the API.
I have coded a simple Java game where there are two rectangles on the screen, one of the rectangles moves and the other stays still, the moving Rectangle moves with keyboard arrow input and can move either up, down, left or right. The problem I am having is drawing my rectangles on the screen, I mean I have the 2 rectangles set up with my variables as shown:
Rectangle rectOne = new Rectangle(shiftX, shiftY,90,90);
Rectangle rectTwo = new Rectangle(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
I have made a render method to draw the things on the screen which I want shown:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
The problem I am having is showing my rectangles on the screen by writing the code in the render method, I could do the following:
g.fillRect(x, y,90,90);
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
Which makes 2 rectangles on the screen but I need the rectangles to be drawn using the Rectangle code written with the variables, I have been told this can be done using Graphics2D but I am wondering if there is a simpler way of just using the graphics function, if not could you please help me set this up?
Thank you in advance.
You can access attributes of the Rectangle instances easily:
g.fillRect(rectOne.getX(), rectOne.getY(), rectOne.getWidth(), rectOne.getHeight());
In any case mind that usually the Graphics object is a Graphics2D instance at runtime so this could work easily too:
Graphics2D g2d = (Graphics2D)g;
g2d.fill(rectOne);
Just use:
g.fillRect(myRect.getX(), myRect.getY(), myRect.getWidth(), myRect.getHeight());
where myRect is the rectangle you want to draw. You could even make a custom method drawRect(Graphics g, Rectangle myRect); if you have lots of rectangles to draw.
Using Graphics2D is not that difficult as well, as the provide Graphics object normally is a Graphics2D object:
Graphics2D g2d = (Graphics2D) g;
g2d.fill(myRect);
What I want to achieve is something like this:
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D) g;
MyShape c = new MyShape();
ga.draw(c);
}
I want that MyShape class to contain the info required to draw a circle with a number inside it.
So, I guess I need to create some kind of container/component, and drew what I need (the circle and the number) inside it, and then pass it further to the method I've pasted above.
The problem is I don't know what class to extend ... and the rest of the story.
A Shape is just that: a shape. A circle is a shape. A rectangle is a shape. But a circle with a number inside it is not a shape. My guess is that you in fact want something like this:
public class CircleWithNumberInside extends JComponent {
#Override
protected void paintComponent(Graphics g) {
// TODO draw a circle, and draw a number inside it.
}
}
You can certainly implement the Shape interface yourself, but there's no need when you can use an existing subclass, such as Ellipse2D. Just construct it with the same value for width and height. There's an example here that shows how to center an arbitrary glyph in an Ellipse2D.Double.
You have to extend the class Shape, which inside you would have to override the paintComponent so that the Graphics2D object knows what to draw.