How to remove an oval without removing other ovals in java AWT? - java

I am solving a Java problem about AWT. When you click the right button of Mouse. It will create an oval. If you click the left button, it will remove the oval you clicked. I have implemented the creation of ovals, but I cannot find any built-in functions about removing shapes in AWT. My code is below:
package chapter16;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Points extends JFrame{
public Points(){
add(new NewPanel4());
}
public static void main(String[] args){
Points frame = new Points();
frame.setSize(200, 200);
frame.setTitle("Add and Remove Points");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class NewPanel4 extends JPanel{
private boolean Add = false;//add points
private int x,y;//the x,y coordinate
public NewPanel4(){
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton()== MouseEvent.BUTTON3){
Add = true;
x = e.getX();
y = e.getY();
repaint();
}
else if(e.getButton() == MouseEvent.BUTTON1){
Add = false;
}
}
});
}
protected void paintComponent(Graphics g){
//super.paintComponent(g);
//Add oval if Add is true
if (Add == true){
g.fillOval(x, y, 10, 10);
}
//remove oval if Add is false
else if(Add == false){
//code to remove ovals
}
}
}
}

I have refactored your code to use a collection to store the oval. Hope it helps you. It is same as what Andrew suggested.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Points extends JFrame {
public Points() {
add(new NewPanel4());
}
public static void main(String[] args) {
Points frame = new Points();
frame.setSize(200, 200);
frame.setTitle("Add and Remove Points");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class NewPanel4 extends JPanel {
private List<Point> points = new ArrayList<>();
public NewPanel4() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
Point center = new Point(e.getX(), e.getY());
points.add(center);
repaint();
} else if (e.getButton() == MouseEvent.BUTTON1) {
removeOval(new Point(e.getX(), e.getY()));
repaint();
}
}
});
}
private void removeOval(Point selectedPoint) {
for (Iterator<Point> iterator = points.iterator(); iterator.hasNext();) {
Point center = iterator.next();
if (isInsideOval(selectedPoint, center)) {
iterator.remove();
break;
}
}
}
private boolean isInsideOval(Point point, Point center) {
int r = 5;
int dx = point.x - center.x;
int dy = point.y - center.y;
return (dx * dx + dy * dy <= r * r);
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
g2D.setColor(Color.BLUE);
for (Point point : points) {
g2D.fillOval(point.x- (10 / 2) , point.y- (10 / 2), 10, 10);
}
}
}
}

Related

Draw circle only, not ellipse

I have the below code that draws an ellipse when the user drags the mouse, however I only want a circle to be drawn and not an ellipse also.
I've tried a few things but haven't quite got it to work, from what I understand it places the circle inside a rectangle, so if I can lock that so it places it inside a square then I should get what I want? The code may not be the most efficient, I'm just trying to write this for a project I'm working, to be clear this is not school homework or anything. If I can get at least this circle aspect of the code working it will save me a lot of time in my project
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawingBoardWithMatrix extends JFrame {
public static void main(String[] args) {
DrawingBoardWithMatrix drawingBoardWithMatrix = new DrawingBoardWithMatrix();
}
public DrawingBoardWithMatrix() {
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent {
ArrayList<Shape> shapes = new ArrayList<>();
Point startDrag, endDrag;
public PaintSurface() {
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
Shape r = makeCircle(startDrag.x, startDrag.y, e.getX(), e.getY());
shapes.add(r);
startDrag = null;
endDrag = null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}
});
}
private void paintBackground(Graphics2D g2){
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(0, i, getSize().width, i);
g2.draw(line);
}
}
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2);
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
for (Shape s : shapes) {
g2.setPaint(Color.BLACK);
g2.draw(s);
// g2.setPaint(colors[(colorIndex++) % 6]);
// g2.fill(s);
}
if (startDrag != null && endDrag != null) {
g2.setPaint(Color.LIGHT_GRAY);
Shape r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
g2.draw(r);
}
}
private Ellipse2D.Float makeCircle(int x1, int y1, int x2, int y2) {
return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
}
Your makeCircle() draws an ellipse with width Math.abs(x1 - x2) and height Math.abs(y1 - y2). If you want to draw a circle, the width and height must be equal. There are several ways to accomplish this:
Use Math.abs(x1 - x2) for both width and height
Use Math.abs(y1 - y2) for both width and height
Decide between the two based on the mouse position
However, I am very concerned about your statement "I just don't have the time right now to learn Java properly" - this will come back to bite you. If you have any intention of having programming as a serious hobby or potentially a job down the line, you must expend a lot of effort to properly understand what you are doing.

shooting multiple bullets from the same class

the following code snippet I created has a rectangle shooting a bullet . I want the bullet class to be called everytime there is a new bullet "summoned" by the space bar and has it independent of the last bullet. what would be the first steps to creating multiple instances of bullet on the screen at once?
drawingComponent class :
package scratch;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.*;
public class drawingComponent extends JComponent implements KeyListener {
private Timer timer;
public int count = 0;
public Rectangle hello = new Rectangle(100, 300, 50, 50);
Integer x1 = hello.x;
Integer y1 = hello.y;
public Bullet bullet = new Bullet(hello.x, hello.y);
boolean fired = false;
public drawingComponent() {
addKeyListener(this);
}
public class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
count++;
bullet.fired();
repaint();
System.out.println(count + "seconds.");
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (fired == true) {
bullet.drawBullet(g);
}
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(255, 25, 0));
g2.setFont(new Font("monospace", Font.BOLD + Font.ITALIC, 30));
g2.drawString("nothing yet", 300, 320);
g2.fill(hello);
setFocusable(true);
requestFocus();
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
hello.y = hello.y - 1;
hello.setLocation(hello.x, hello.y);
repaint();
System.out.println(hello.y);
}
if (e.getKeyCode() == KeyEvent.VK_S) {
hello.y = hello.y + 1;
hello.setLocation(hello.x, hello.y);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_A) {
hello.x = hello.x - 1;
hello.setLocation(hello.x, hello.y);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_D) {
hello.x = hello.x + 1;
hello.setLocation(hello.x, hello.y);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
bullet.update(hello.x, hello.y);
fired = true;
if (count > 5) {
timer.stop();
}
timer = new Timer(50, new TimerListener());
timer.start();
repaint();
}
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
// fired = false;
repaint();
}
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
bullet class :
package scratch;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
public class Bullet {
Rectangle BulletRect = new Rectangle(20, 20, 20, 20);
public Bullet(int x, int y2) {
BulletRect.y = y2;
BulletRect.x = x;
}
public void fired() {
BulletRect.y = BulletRect.y + 50;
}
public void update(int x1, int y4) {
BulletRect.x = x1;
BulletRect.y = y4;
}
public void drawBullet(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.fill(BulletRect);
}
// get bullets position
//increment bullets position every so often
//when bullet goes off screen, clear it from game
}
also, if I may could pose another question , what route would best to go down if I wanted to start learning android from swing? thanks.
Here are a few pointers for you (I am assuming you are using Java 8).
Instead of public Bullet bullet = new Bullet(hello.x, hello.y); use private List<Bullet> bullets = new ArrayList<>();
When you create a new bullet (when you detect spacebar) add it to the list: bullets.add(new Bullet(x, y));
When you need to draw all the bullets in paintComponent you can use bullets.stream().forEach(bullet -> bullet.drawBullet(g));
There's no need for the fired field any longer: an empty bullets list is the same as not fired.
Each tick you'll need to remove bullets that aren't on the screen. This can be done with something like: bullets.removeIf(Bullet::isOffScreen); assuming you implement an isOffScreen method.

drawPolygon keeps drawing lines from starting (mousePressed) location to current (mouseDragged) location

So, I'm trying to dynamically draw a Polygon starting from when I click the mouse until I stop dragging and release. Instead of, for the purpose of this question, a square outline being drawn when I click, drag down, then right-across, then up, then left-across, this is what happens: http://imgur.com/t8ZN3Pp
Any suggestions?
Notes:
model.addPolygon() creates a Polygon with starting points and adds it to an ArrayList called 'polys'
model.addPolygonPoint() adds points to this created polygon that is stored in 'polys'
my paint function iterates through polys to paint
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
model.addPolygon(oldX, oldY);
}
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
model.addPolyPoint(currentX, currentY);
repaint();
}
.
.
. then in paintComponent:
for (ListIterator<Polys> iter =
model.polys.listIterator(model.polys.size());
iter.hasPrevious();){
graphics2D.draw(iter.previous().poly);
Full paintComponent:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
g.drawImage(image, 0, 0, null);
for (ListIterator<Polys> iter =
model.polys.listIterator(model.polys.size());
iter.hasPrevious();){
graphics2D.draw(iter.previous().poly);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class Testing {
private static int lastX;
private static int lastY;
private static int modX;
private static int modY;
private static final BasicStroke STROKE = new BasicStroke(2.0F);
private static final Point[] SHAPE = new Point[]{
new Point(10, 10),
new Point(10, 40),
new Point(60, 90),
new Point(50, 50)
};
public static void main(final String[] args) {
final JFrame frame = new JFrame("Polygon Movement");
final JPanel pane = new JPanel() {
#Override
public void paintComponent(final Graphics g1) {
final Graphics2D g = (Graphics2D) g1;
g.setColor(Color.RED);
g.translate(modX, modY);
g.setStroke(STROKE);
for (int i = 0; i < SHAPE.length; i++) {
g.drawLine(SHAPE[i].x, SHAPE[i].y, SHAPE[(i + 1) % SHAPE.length].x, SHAPE[(i + 1) % SHAPE.length].y);
}
}
};
pane.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
modX += e.getX() - lastX;
modY += e.getY() - lastY;
lastX = e.getX();
lastY = e.getY();
frame.repaint();
}
});
pane.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
lastX = e.getX();
lastY = e.getY();
}
});
pane.setPreferredSize(new Dimension(200, 200));
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
}
As you can see, I make a basic shape with defined points. It is the most effective way to do this, unless you wish to change the basic shape (here it is static). In that case, you find the point the mouse it 'grabbing' and modify that one. Either way, no adding or removing of points is needed. I use the terms lastX instead of oldX just by preference.
The BasicStroke is quite optional, same with casting to a Graphics2D object.
The line:
g.drawLine(SHAPE[i].x, SHAPE[i].y, SHAPE[(i + 1) % SHAPE.length].x, SHAPE[(i + 1) % SHAPE.length].y);
Should make some sense if you've tried this thing before. It iterates through all the points, drawing a line from the current (SHAPE[i]) to the next (SHAPE[(i + 1) & SHAPE.length).
The reason behind that logic, is that say you have 4 points, as we do here. The last iteration through them, you will be given i = 3. Due to this and the array only containing 4 indexes (0-3), we must get that value back down to zero. For simplicity I use the % SHAPE.length so there wouldn't be a need for special cases.
I also opted to use adapters seeing as there were only 2 methods needed of the 7 possible ones.
If you have any questions feel free to ask me about this.
~Legend
If all you want to do is draw a polygon. You could simply use the Shape API
This will allow you to "add" points to the shape and allow the shape to paint itself.
Here I use a simple Path2D shape, as it allows me to append points over time. I keep a running list of shapes, which allows me to generate multiple polygons as required
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.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawPolygon {
public static void main(String[] args) {
new DrawPolygon();
}
public DrawPolygon() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PolyPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class PolyPane extends JPanel {
private MouseHandler mouseHandler;
private Path2D currentShape;
private List<Path2D> lstPloys;
private Point lastPoint;
private Point currentPoint;
public PolyPane() {
lstPloys = new ArrayList<>();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
public void addNotify() {
super.addNotify();
addMouseListener(getMouseHandler());
addMouseMotionListener(getMouseHandler());
}
#Override
public void removeNotify() {
removeMouseListener(getMouseHandler());
removeMouseMotionListener(getMouseHandler());
super.removeNotify();
}
public MouseHandler getMouseHandler() {
if (mouseHandler == null) {
mouseHandler = new MouseHandler();
}
return mouseHandler;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (lastPoint != null) {
g2d.setColor(Color.RED);
g2d.fillOval(lastPoint.x - 2, lastPoint.y - 2, 4, 4);
}
if (currentShape != null) {
g2d.setColor(Color.RED);
g2d.draw(currentShape);
if (lastPoint != null && currentPoint != null) {
System.out.println(lastPoint + " - " + currentPoint);
g2d.setColor(new Color(255, 0, 0, 64));
g2d.draw(new Line2D.Float(lastPoint, currentPoint));
}
}
g2d.setColor(Color.BLACK);
for (Shape shape : lstPloys) {
g2d.draw(shape);
}
g2d.dispose();
}
public class MouseHandler extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (e.getClickCount() == 1) {
Point p = e.getPoint();
lastPoint = p;
if (currentShape == null) {
currentShape = new Path2D.Float();
currentShape.moveTo(p.x, p.y);
} else {
currentShape.lineTo(p.x, p.y);
}
repaint();
} else if (e.getClickCount() == 2) {
currentShape.closePath();
lstPloys.add(currentShape);
currentShape = null;
lastPoint = null;
repaint();
}
}
}
#Override
public void mouseMoved(MouseEvent e) {
if (currentShape != null) {
currentPoint = e.getPoint();
repaint();
} else {
currentPoint = null;
}
}
}
}
}
Take a look at Working with Geometry for more details

MousePressed not working

I've been trying to do this assignment and for it to work I need the event mousePressed to work but for some reason it's not responding to the mouse. Its purpose is to paint another yellow circle when the mouse is pressed.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CatchMonster extends JPanel
{
private int height = 300;
private int width = 600;
private final int delay = 4001;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY, xPoint, yPoint;
public CatchMonster() {
DotListener dot = new DotListener();
addMouseListener(dot);
timer = new Timer(delay, new timerListener());
x = 40;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(width, height));
setBackground(Color.black);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
private class timerListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
Random gn = new Random();
x = gn.nextInt(width);
y = gn.nextInt(height);
repaint();
}
}
private class DotListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
repaint();
}
#Override
public void mouseClicked(MouseEvent event) {
}
#Override
public void mouseEntered(MouseEvent event) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent event) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent event) {
// TODO Auto-generated method stub
}
}
}
I need the event mousePressed to work but for some reason it's not responding to the mouse. Its purpose is to paint another yellow circle when the mouse is pressed.
But it won't paint another circle as all it does is call repaint(), and how is that going to paint anything new? If you want it to create another circle, you'll have to give it logic to do so. For instance, if you want to paint more than one yellow oval, you'll want to create an ArrayList of Point objects and add a Point into that array list in the mousePressed method. Then in the paintComponent method, you can iterate through the array list, painting ovals for each Point it contains.
In addition, you want to change this:
public void paintComponent(Graphics g) {
super.paintComponents(g); // this is not the "super" method of paintComponent
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
to this:
public void paintComponent(Graphics g) {
super.paintComponent(g); // See the difference?
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CatchMonster extends JPanel {
private int height = 300;
private int width = 600;
private final int delay = 4001;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY, xPoint, yPoint;
private List<Point> points = new ArrayList<Point>();
public CatchMonster() {
DotListener dot = new DotListener();
addMouseListener(dot);
timer = new Timer(delay, new timerListener());
x = 40;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(width, height));
setBackground(Color.black);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
int radius = 30;
g.setColor(Color.green);
for (Point p : points) {
int x = p.x - radius;
int y = p.y - radius;
g.fillOval(x, y, 2 * radius, 2 * radius);
}
}
private class timerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random gn = new Random();
x = gn.nextInt(width);
y = gn.nextInt(height);
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CatchMonster());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class DotListener extends MouseAdapter {
public void mousePressed(MouseEvent event) {
points.add(event.getPoint());
repaint();
}
}
}

Swing components drawing inside each other

This is my fourth question here in about two weeks... I think I have a lot to learn.
Anyway, I'm hoping the sample I've prepared below will explain the problem better than I can, but since I changed all of my components to Swing rather than AWT, they've all been drawing every component in the panel inside themselves whenever I call repaint().
This code is essentially my program stripped down as much as I can without destroying the problem completely. I've stuck a couple of my custom listbox components in there to demonstrate the issue, and made the border red to make it slightly easier to see what's drawing where.
Test.java:
package test;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
public class Test extends JFrame
{
public static void main(String[] args)
{
Test test1 = new Test();
}
public Test()
{
super("Test Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(800, 480);
setLocationRelativeTo(null);
loadTestScreen();
}
public void loadTestScreen()
{
TestScreen newTestScreen = new TestScreen();
newTestScreen.setSize(new Dimension(getWidth() - getInsets().left - getInsets().right, getHeight() - getInsets().top - getInsets().bottom));
setContentPane(newTestScreen);
}
}
TestScreen.java:
package test;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.*;
import java.net.URI;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class TestScreen extends JPanel implements ComponentListener, MouseListener
{
int border, LT;
public TestScreen()
{
setLayout(null);
setOpaque(true);
setBackground(Color.WHITE);
addComponentListener(this);
border = 8;
LT = 4;
///////////////////////////////////////////////////////////
ArrayList<String> testList = new ArrayList<String>();
testList.add("1");
testList.add("2");
testList.add("3");
testList.add("4");
testList.add("5");
testList.add("6");
add(new ListBox(testList), 0);
add(new ListBox(testList), 1);
}
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke((float)(LT)));
g2d.setColor(new Color(255, 0, 0));
g2d.drawRoundRect(border, border, getWidth() - border - border, getHeight() - border - border, border + border, border + border);
}
public void componentHidden(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
public void componentMoved(ComponentEvent e)
{
componentResized(e);
}
public void componentResized(ComponentEvent e)
{
getComponent(0).setLocation(20, 20);
getComponent(0).setSize(100, 100);
getComponent(1).setLocation(200, 200);
getComponent(1).setSize(150, 150);
repaint();
}
public void mouseEntered(MouseEvent e)
{
repaint();
}
public void mouseExited(MouseEvent e)
{
repaint();
}
public void mouseReleased(MouseEvent e){} public void mouseDragged(MouseEvent e){} public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
repaint();
}
}
ListBox.java:
package test;
import java.beans.*;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.beans.PropertyChangeSupport;
import javax.swing.*;
public class ListBox extends JComponent implements MouseListener, MouseMotionListener, MouseWheelListener, ComponentListener
{
public ArrayList<String> data;
int border, LT;
int selectedIndex = 0;
int mousedIndex = -1;
public int viewedHeight = 0;
int itemHeight;
int numberOfDisplayedItems;
Font miniFont;
FontMetrics miniMetrics;
private final PropertyChangeSupport pcs = new PropertyChangeSupport( this );
public ListBox(ArrayList<String> list)
{
setVisible(true);
setOpaque(true);
border = 8;
LT = 4;
addMouseListener(this);
addMouseMotionListener(this);
addComponentListener(this);
addMouseWheelListener(this);
data = list;
miniFont = new Font("Calibri", Font.PLAIN, 15);
miniMetrics = getFontMetrics(miniFont);
itemHeight = miniMetrics.getAscent() + miniMetrics.getDescent() + border + border;
}
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(new Color(93, 138, 168));
g2d.setStroke(new BasicStroke((float)LT / 2));
g2d.setClip(-(LT / 2), -(LT / 2), getWidth() + LT, getHeight() + LT);
int cumulativeDist = -viewedHeight;
for (int i = 0; i < data.size(); i++)
{
if (selectedIndex == i)
g2d.fillRect(0, cumulativeDist, getWidth(), itemHeight);
cumulativeDist += itemHeight;
g2d.drawLine(0, cumulativeDist, getWidth(), cumulativeDist);
}
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setFont(miniFont);
g2d.setColor(new Color(42, 60, 76));
cumulativeDist = -viewedHeight + border + miniMetrics.getAscent();
for (int i = 0; i < data.size(); i++)
{
if (mousedIndex == i){
g2d.drawString(data.get(i), border + border / 2, cumulativeDist);
} else {
g2d.drawString(data.get(i), border, cumulativeDist);
}
cumulativeDist += itemHeight;
}
}
public String getSelectedItem()
{
return data.get(selectedIndex);
}
public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseDragged(MouseEvent e){}public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
int old = selectedIndex;
int mouseHeight = viewedHeight + e.getY();
int ID = mouseHeight / itemHeight;
System.out.println(mouseHeight / itemHeight);
selectedIndex = ID;
pcs.firePropertyChange("selectedIndex", old, selectedIndex);
}
public void componentHidden(ComponentEvent e){} public void componentShown(ComponentEvent e){} public void componentMoved(ComponentEvent e){}
public void componentResized(ComponentEvent e)
{
numberOfDisplayedItems = (int)((getHeight() / itemHeight) + 0.5);
repaint();
}
public void mouseWheelMoved(MouseWheelEvent e)
{
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
{
if (e.getUnitsToScroll() > 0)
{
viewedHeight += itemHeight;
}else{
viewedHeight -= itemHeight;
}
if (viewedHeight > (data.size() * itemHeight) - getHeight())
viewedHeight = (data.size() * itemHeight) - getHeight();
if (viewedHeight < 0)
{
viewedHeight = 0;
}
mouseMoved((MouseEvent)e);
repaint();
}
}
public void mouseMoved(MouseEvent e)
{
int mouseHeight = viewedHeight + e.getY();
int ID = mouseHeight / itemHeight;
mousedIndex = ID;
repaint();
}
public void mouseExited(MouseEvent e)
{
mousedIndex = -1;
repaint();
}
}
Thanks, and if you find any other glaring issues in my programming, please don't hesitate to tell me :P
The immediate problem—painting artifacts in ListBox—is caused by not honoring the opaque property. Using true promises to paint all of the bits, but your paintComponent() fails to do so.
Here are a few more suggestions:
Don't neglect the event dispatch thread.
Do use event listeners, but also consider the available adapters, which have convenient null implementations.
Don't reinvent existing components, and override paintComponent() only as a last resort.
Do learn to use layout managers and borders.
Yes there is much to learn, but this kind of experimentation is invaluable.
You should call setVisible(true); only once you've set up the component, at the end of your constructor.

Categories

Resources