Multiple Graphics2D Objects - java

I have a Graphics object of JPanel and that is working fine:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class GraphicsTest extends JPanel
{
private Graphics2D g2d;
private String state;
private int x, y;
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setClip(0, 0, getWidth(), getHeight());
g2d.setColor(Color.BLACK);
g2d.drawString("STATE: " + state, 5, 15);
g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);
g2d.setColor(Color.red);
Rectangle2D r2d = new Rectangle2D.Double(x,y,10,10);
g2d.draw(r2d);
Test t = new Test();
super.add(t);
repaint();
}
public void setState(String state) { this.state = state; }
public String getState() { return state; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
I was experimenting with a new Graphics component and when I instantiate a new Test and add it in GraphicsTest nothing happens. What is it that I am doing wrong?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
public class Test extends JComponent
{
private Graphics2D g2d;
private String state;
private int x, y;
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g2d = (Graphics2D) g.create();
g2d.setColor(Color.GREEN);
g2d.fill(new Rectangle2D.Double(60, 60,
10, 10));
repaint();
}
public void setState(String state) { this.state = state; }
public String getState() { return state; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
Thanks!

You should add Test component only once and set a layout:
public GraphicTest()
{
super();
setLayout(new BorderLayout());
add(new Test(),BorderLayout.CENTER);
}
Also, do not save Graphics2D objects because they will be invalid once paintComponent is finished and do not call repaint inside paintComponent. Use timers if you need animation and call repaint in setXXX methods that change the look of the component.

Related

How to move different shapes with MouseListener

I have a program that paints different shapes that I put in the ArrayList, it works fine to iterate with shapes to paint them but my methods for moving them doesn't work. Is it something wrong with my move() method?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyShapes extends JPanel {
private Point2D.Float position;
private final DifferentShapes[] shapes = new DifferentShapes[]{new Circle(), new Triangle(), new Square()};
MovingAdapter ma = new MovingAdapter();
public MyShapes() {
addMouseMotionListener(ma);
addMouseListener(ma);
}
interface DifferentShapes {
void paint(Graphics2D graphics);
boolean contains(int x, int y);
void move(int dx, int dy);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D graphics = (Graphics2D) g;
for (DifferentShapes shape : this.shapes) {
shape.paint(graphics);
}
}
class MovingAdapter
public class MovingAdapter extends MouseAdapter {
private int x;
private int y;
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
final int dx = e.getX() - x;
final int dy = e.getY() - y;
for (DifferentShapes shape : shapes) {
if (shape.contains(x, y)) {
shape.move(dx, dy);
}
}
x += dx;
y += dy;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shapes World");
MyShapes m = new MyShapes();
m.setDoubleBuffered(true);
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class Circle
class Circle implements MyShapes.DifferentShapes {
public Circle() {
}
public static Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);
public void paint(Graphics2D graphics) {
Graphics2D circle = (Graphics2D) graphics;
circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
circle.setColor(new Color(0, 0, 117));
circle.fill(myCr);
}
#Override
public boolean contains(int x, int y) {
if (myCr.contains(x, y)) {
}
return true;
}
public void move(int dx, int dy) {
myCr.x += dx;
myCr.y += dy;
}
}
When I use regular if statement it works fine but not in the for loop and my move() method
The logic is right and you are correctly changing the coordinates. You just need to call repaint() in order to make the the changes display.
if (shape.contains(x, y)) {
shape.move(dx, dy);
repaint();
}

Draw shapes on scrollable JPanel

I'm trying to draw rectangles on fairly large images in order to get the pixel coordinates of objects within the image. I am able to display the image and make it scrollable, or display the image and be able to draw rectangles on top of it....but not both.
It's obvious that I'm drawing the image on top of the canvas that I'm trying to draw the rectangles on, but I can't for the life of me figure out how to make it all coexist.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DrawRect extends JPanel {
int x, y, x2, y2;
private static final long serialVersionUID = 1L;
private BufferedImage image;
private JPanel canvas;
public static void main(String[] args) {
JPanel p = new DrawRect();
JFrame f = new JFrame();
f.setContentPane(p);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
DrawRect() {
x = y = x2 = y2 = 0; //
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
try {
this.image = ImageIO.read(new URL("https://previews.123rf.com/images/victoroancea/victoroancea1201/victoroancea120100059/12055848-tv-color-test-pattern-test-card-for-pal-and-ntsc.jpg"));
}catch(IOException ex) {
Logger.getLogger(DrawRect.class.getName()).log(Level.SEVERE, null, ex);
}
this.canvas = new JPanel() {
private static final long serialVersionUID = 1L;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
canvas.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
JScrollPane sp = new JScrollPane(canvas);
setLayout(new BorderLayout());
add(sp, BorderLayout.CENTER);
}
public void setStartPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setEndPoint(int x, int y) {
x2 = (x);
y2 = (y);
}
public void drawRect(Graphics g, int x, int y, int x2, int y2) {
int px = Math.min(x,x2);
int py = Math.min(y,y2);
int pw=Math.abs(x-x2);
int ph=Math.abs(y-y2);
g.drawRect(px, py, pw, ph);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
drawRect(g, x, y, x2, y2);
}
}
Your image-drawing JPanel must be the same JPanel that draws the rectangle and that has the MouseAdapter added to it. For instance:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class DrawRect2 extends JPanel {
public static final String IMG_PATH = "https://previews.123rf.com/images/victoroancea"
+ "/victoroancea1201/victoroancea120100059"
+ "/12055848-tv-color-test-pattern-test-card-for-pal-and-ntsc.jpg";
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private DrawingPanel drawingPanel;
public DrawRect2(Image img) {
drawingPanel = new DrawingPanel(img);
JScrollPane scrollPane = new JScrollPane(drawingPanel);
MyMouse myMouse = new MyMouse();
drawingPanel.addMouseListener(myMouse);
drawingPanel.addMouseMotionListener(myMouse);
setLayout(new BorderLayout());
add(scrollPane);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
Image img = null;
try {
URL url = new URL(IMG_PATH);
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
DrawRect2 mainPanel = new DrawRect2(img);
JFrame frame = new JFrame("DrawRect2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class DrawingPanel extends JPanel {
private Image img;
private Rectangle rectangle;
public DrawingPanel(Image img) {
this.img = img;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
if (rectangle != null) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setXORMode(Color.WHITE);
g2.draw(rectangle);
g2.dispose(); // since we created this object
}
}
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
if (img == null) {
return super.getPreferredSize();
} else {
int w = img.getWidth(this);
int h = img.getHeight(this);
return new Dimension(w, h);
}
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
}
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyMouse extends MouseAdapter {
private Point p1;
#Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
}
#Override
public void mouseDragged(MouseEvent e) {
if (p1 != null) {
createRect(e);
}
}
private void createRect(MouseEvent e) {
Point p2 = e.getPoint();
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
int width = Math.abs(p1.x - p2.x);
int height = Math.abs(p1.y - p2.y);
Rectangle r = new Rectangle(x, y, width, height);
((DrawingPanel) e.getSource()).setRectangle(r);
((DrawingPanel) e.getSource()).repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
if (p1 != null) {
createRect(e);
}
p1 = null;
}
}
So in this paintComponent method, I draw both the image and the Rectangle, using Graphics2D XOR mode to help show the lines regardless of the background color:
#SuppressWarnings("serial")
public class DrawingPanel extends JPanel {
private Image img;
private Rectangle rectangle;
public DrawingPanel(Image img) {
this.img = img;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
if (rectangle != null) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setXORMode(Color.WHITE);
g2.draw(rectangle);
g2.dispose(); // since we created this object
}
}
I also have this method:
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
To allow the MouseListener/Adapter to pass in the Rectangle into this JPanel.
Rectangle r = new Rectangle(x, y, width, height);
((DrawingPanel) e.getSource()).setRectangle(r);
((DrawingPanel) e.getSource()).repaint();

Java Graphics Are Not Displaying

I am trying to code the game Pong on Java but am having difficulty displaying the graphics. Nothing appears, just a blank gray default screen when I try to run the program. I am using a Mac, and am running this code on Eclipse.
Any help would be much appreciated.
Thank you.
"Pong.java"
package pong;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Pong implements ActionListener{
public static Pong pong;
public int width = 700, height = 700;
public Renderer renderer;
public Paddle player1;
public Paddle player2;
public JFrame jframe;
public Pong(){
Timer timer = new Timer(20, this);
jframe = new JFrame("Pong");
renderer = new Renderer();
jframe.setSize(width, height);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.add(renderer);
start();
timer.start();
}
public void start(){
player1 = new Paddle(this, 1);
player2 = new Paddle(this, 2);
}
public void update() {
}
public void render(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
player1.render(g);
player2.render(g);
}
#Override
public void actionPerformed(ActionEvent e) {
update();
renderer.repaint();
}
public static void main(String[] args){
pong = new Pong();
}
}
"Renderer.java"
package pong;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Renderer extends JPanel {
private static final long serialVersionUID = 1L;
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Pong.pong.render(g);
}
}
"Paddle.java"
package pong;
import java.awt.Color;
import java.awt.Graphics;
public class Paddle {
public int paddleNumber;
public int x, y, width = 100, height = 500;
public int score;
public Paddle(Pong pong, int paddleNumber){
this.paddleNumber = paddleNumber;
if(paddleNumber == 1){
this.x = 0;
}
else if (paddleNumber == 2){
this.x = pong.width - width;
}
this.y = pong.height / 2 - this.height / 2;
}
public void render(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(x, y, width, height);
}
}

Coloring 2 Ovals with Graphics2D

I want to Color 2 Ovals in Java with Graphics2D. Oval1(point) Green and Oval2(point2) Blue. The Problem is that Oval2 becomes green and Oval1 black. Does somebody knows what the problem is?
left(oval1) right(oval2)
Here is the SSCCE:
GameView.java
import javax.swing.JFrame;
import stackoverflow.GameView;
public class GameView extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private Game game = new Game();
public GameView(){
JFrame gameFrame = new JFrame("Game");
gameFrame.setSize(500, 300);
gameFrame.setVisible(true);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.add(game);
game.runGame();
}
public static void main(String[] args) throws InterruptedException {
new GameView();
}
}
Game.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import stackoverflow.Point;
public class Game extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private Point point = new Point(Color.GREEN);
private Point point2 = new Point(Color.BLUE);
public void runGame(){
repaint();
point2.setX(300);
}
public void paint(Graphics g) {
point.paint(g);
point2.paint(g);
}
}
Point.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class Point {
private int x = 100;
private int y = 100;
private int width = 10;
private int height = 10;
private Color color;
public Point(Color color){
this.color = color;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, width, height);
g2d.setColor(this.color);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Change the order of these calls...
g2d.fillOval(x, y, width, height);
g2d.setColor(this.color);
And you should be painting in JPanel.paintComponent instead.

Custom JComponent not displaying in Custom JPanel

I've tried the add() method but nothing is displayed when I try to add Test to GraphicsTest. How should I be adding it? Can someone show me? I've included the code I'm using.
This is my way and it's not working. Can someone show me or make me aware of what the problem actually is?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class Test extends JComponent
{
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.red);
g2d.drawString("Hello", 50, 50);
g2d.dispose();
}
}
Here is the other class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class GraphicsTest extends JPanel
{
private Graphics2D g2d;
private String state;
private int x, y;
GraphicsTest()
{
Test t = new Test();
t.setVisible(true);
add(t);
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawString("STATE: " + state, 5, 15);
g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);
g2d.setColor(Color.red);
Rectangle2D r2d = new Rectangle2D.Double(x, y, 10, 10);
g2d.draw(r2d);
g2d.dispose();
}
public void setState(String state) { this.state = state; }
public String getState() { return state; }
public void setX(int x) { this.x = x; repaint(); }
public void setY(int y) { this.y = y; repaint(); }
}
Your problem is g2d.dispose(). Take those out and it should work. I suspect this may cause different behavior on different jvms. It appears what is happening is the g2d object is used to draw stuff on the GraphicsTest object. Then the same g2d object tries to draw stuff on on the Test object, but it has been disposed so it can't draw anything. There are cases where you want to call g2d.dispose() but this is not one of them.
The other thing I did to make your code work was I changed the layout manager:
setLayout(new BorderLayout());
add(t, BorderLayout.CENTER);
The default layout should be Flow Layout. I'm not sure off hand why it wouldn't work with Flow Layout.

Categories

Resources