I'm writing minesweeper and I want to put ovals on mine cells after a mine is clicked. But for now, I just want to put ovals on all cells just to check. I have written the code below. But when I run the program there is no ovals on buttons. I can not see the reason. I would be grateful if I get some suggestions.
public class Cell extends JButton{
...
public void painComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.drawOval(0,0,25,25);
}
public void draw() {
repaint();
}
...
}
public class Grid extends JPanel implements MouseListener{
...
public Grid(){
this.setLayout(new GridLayout(20,20));
cells = new Cell[20][20];
for(int i=0; i<20; i++) {
for(int j=0; j<20; j++) {
cells[i][j] = new Cell(i,j);
cells[i][j].addMouseListener(this);
cells[i][j].draw();
this.add(cells[i][j]);
}
}
plantMines();
setVisible(true);
}
...
}
but when I run the program there is no ovals on buttons.
public void painComponent(Graphics g) {
You made a typo. You spelt "paint" wrong.
When you override a method you should use:
#Override
protected void paintComponent(Graphics g)
This way if you make a typo the compiler will tell you that you are not overriding a method of the class.
Also, don't attempt to draw the oval by using custom painting. Instead you should be adding an Icon to the button. Then you can just change the Icon as required.
You can easily create an simple Icon to use. Here is an example of creating a square Icon:
import java.awt.*;
import javax.swing.*;
public class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JPanel panel = new JPanel( new GridLayout(2, 2) );
for (int i = 0; i < 4; i++)
{
Icon icon = new ColorIcon(Color.RED, 50, 50);
JLabel label = new JLabel( icon );
label.setText("" + i);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
panel.add(label);
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.setSize(200, 200);
f.setLocationRelativeTo( null );
f.setVisible(true);
}
}
Related
I have an Issue painting jcomponent
//class where the rectangle should drawed
public class Board extends JComponent
{
private Case[][] cases= new Case[10][10];
public Plateau() {
super();
this.setLayout(new GridLayout(10,10));
this.setSize(getPreferredSize());
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if ((i + j) % 2 == 0) {
cases[i][j] = new WhiteCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);
} else {
cases[i][j] = new BlackCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);
}
add(cases[i][j]);
}
}
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
}
//class Base for rectangle
public abstract class Case extends JComponent {
protected static final int LONGUEUR=60;
protected int x,y,width,height;
protected abstract void paintComponent(Graphics g);
public Dimension getPreferredSize() { return new Dimension(LONGUEUR, LONGUEUR);
}
}
///black Case
public class BlackCase extends Case
{
private Piece piece;
private static final long serialVersionUID = 1L;
public CaseNoire(int x, int y,int width,int height)
{
this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
public Dimension getPreferredSize() {
return new Dimension(LONGUEUR, LONGUEUR);
}
#Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.darkGray);
g.fillRect(x, y,width,height);
}
}
public class CaseWhite extends Case {
/**
*
*/
private static final long serialVersionUID = 1L;
public CaseBlanche(int x, int y,int width,int height)
{
this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
#Override
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.fillRect(x, y,width,height);
g.setColor(Color.BLACK);
g.drawString("X= "+x , 10, 10);
}
public Dimension getPreferredSize() {
return new Dimension(LONGUEUR, LONGUEUR);
}
}
//Main class
public class CheckersGame extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args )
{
CheckersGame checkers= new CheckersGame();
}
public CheckersGame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Jeu de Dames");
JPanel panelPrincipalDame = new JPanel(new GridBagLayout());
Board board = new Board();
GridBagConstraints c = new GridBagConstraints();
c.fill= GridBagConstraints.NONE;
c.gridx =0;
c.gridy = 0 ;
c.gridheight= 2;
c.gridwidth= 2;
panelPrincipalDame.add(plateau,c);
setSize(800, 700);
setContentPane(panelPrincipalDame);
![//setVisible(true);][1]
setResizable(false);
}
}
The result of this code is ( Note X+ 0 etc.. is just for debug purpose )
But what I want is this
Please why I get only one rectangle?
So much for listening to my suggestion to NOT create "CaseNoire" and "CaseBlanch" classes I gave in your last question: paintComponent does not paint correctly from two weeks ago. These classes are not needed. What happens if you ever want to give the user the flexibility to choose the colors of the squares. Your game logic should never be based on the class name or anything like that. So get rid of the classes and use the built in Swing features to color the background of the component.
I think the problem is because you created variables "x, y, width, height" in the Case class. I believe these variables already defined in the Component class, to represent the size/location of the component.
Get rid of the variables, you don't need to manage the size/location of each component because the GridLayout will do this for you.
Again, look at the example code I gave you that shows how to create a "ChessBoard".
I resolved it I just set the X=0 and Y= 0 in the paintComponent()
protected void paintComponent(Graphics g)
{
g.setColor(Color.darkGray);
g.fillRect(0, 0,width,height);
}
I have followed peeskillet and changed my codes according to his last idea. Here are the changed codes
CriclePanel class
public class CirclePanel extends JPanel {
int centerX, centerY, radius;
Color circle_color;
public void setCircle(int centerX, int centerY, int radius, Color c) {
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
circle_color = c;
revalidate();
repaint();
}
protected void paintComponent(Graphics g) {
System.out.println("Trying to draw circle");
super.paintComponent(g);
g.fillOval(centerX, centerY, radius*2, radius*2);
}
}
Grid Panel (added from palette and custom constructed)
myGridPanel = new JPanel(new GridLayout(8,8));
panels = new CirclePanel[8][8];
for (int i = 0; i < panels.length; i++) {
for (int j = 0; j < panels[i].length; j++) {
CirclePanel panel = new CirclePanel();
panels[i][j] = panel;
myGridPanel.add(panel);
}
}
Trying to add draw circle:
if(dType==DiceType.blackDice){
System.err.println("black #"+i+","+j);
panels[i][j].setCircle(x, y, radius, Color.BLACK);
}else{
System.err.println("white #"+i+","+j);
panels[i][j].setCircle(x, y, radius, Color.WHITE);
}
But the circles are not drawn on the grid panel and there is no grid also on the mother panel "myGridPanel". I am seeing that the paintComponant() of CirclePanel is not being called.
Output: http://s25.postimg.org/v8u398dun/no_Gridnd_Circle.png
As I mentioned in the comments, you can Refine your Design by not making every Circle a panel. Here is an example
Another option that would fit for your current design, would be to have a setter in each circle panel, for which you can pass a circle model.
public class CirclePanel extends JPanel {
private Circle circle;
public void setCircle(Circle cirlce) {
this.circle = circle;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (circle != null) {
// get the state from circle and paint
g.fillOval(circle.getX(), circle.getY(), getWidth(), getHeight());
}
}
}
Note: Circle in this case is not a panel
Then for the gridPanel give it a GridLayout and add all the CirclePanel to it.
JPanel gridPanel = new JPanel(new GridLayout(5, 5));
CirclePanel[][] panels = new CirclePanel[5][5];
for (int i = 0; i < panels.length; i++) {
for (int j = 0; j < panels[i].length; j++) {
CirclePanel panel = new CirclePanel();
panels[i][j] = panel;
gridPanels.add(panel);
}
}
Doing this, there will be empty CirclePanel in the gridPanel. Then when you want to paint a circle in one of the panels, you can do something like
Circle circle = new Circle(...);
panels[1][1].setCircle(circle);
UPDATE
Or now that I think about it, you don't even really need a Circle class, since you could just draw the circle a 0, 0, getWidth(), getHeight(). You could simple set a flag to draw. But if you want to add more state to the circle, then maybe you could keep a circle class. But if not, it might look something like
public class CirclePanel extends JPanel {
private boolean draw;
private Color color;
// setter for color
// setter for draw
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (draw) {
// get the state from circle and paint
g.fillOval(0, 0, getWidth(), getHeight());
}
}
}
Update with example
See the method setDraw. That's where I set the flag the draw or not draw. I call it in the mouse listener in the Circle class, but you can call it from anywhere.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CircleDemo {
private static final int GRID_SIZE = 5;
private Circle[][] circlePanels
= new Circle[GRID_SIZE][GRID_SIZE];
private JPanel gridPanel
= new JPanel(new GridLayout(GRID_SIZE, GRID_SIZE));
public CircleDemo() {
initCircles();
JFrame f = new JFrame();
f.add(gridPanel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private void initCircles() {
for (int i = 0; i < circlePanels.length; i++) {
for (int j = 0; j < circlePanels[i].length; j++) {
Circle circle = new Circle();
circlePanels[i][j] = circle;
gridPanel.add(circle);
}
}
}
class Circle extends JPanel {
private boolean draw = false;
private Color color = Color.BLUE;
public Circle() {
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
if (isDraw()) {
setDraw(false);
} else {
setDraw(true);
}
}
});
}
public boolean isDraw() {
return draw;
}
public void setDraw(boolean draw) {
this.draw = draw;
repaint();
}
public void setColor(Color color) {
this.color = color;
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(75, 75);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (draw) {
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new CircleDemo();
}
});
}
}
I'm trying to create a program that runs an animation similar to the one on this video but I'm having trouble adding more squares. I tried to add all the squares to an array list but I couldn't figure out where it goes.
so far this is my code:
public class Animation extends JFrame{
CrazySquares square = new CrazySquares();
Animation(){
add(new CrazySquares());
}
public static void main (String[] args){
Animation frame = new Animation();
frame.setTitle("AnimationDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 250);
frame.setVisible(true);
}
}
class CrazySquares extends JPanel{
private final int numberOfRectangles=100;
Color color=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
private int x=1;
private int y=1;
private Timer timer = new Timer(30, new TimerListener());
Random random= new Random();
int randomNumber=1+(random.nextInt(4)-2);
Random rand= new Random();
int rando=1+(rand.nextInt(4)-2);
CrazySquares(){
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width=getWidth();
int height=getHeight();
g.setColor(color);
g.fillRect(x+width/2,y+(int)(height*.47), 20, 20);
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
x += rando;
y+= randomNumber;
repaint();
}
}
}
You've got code to paint out one rectangle, here:
int width=getWidth();
int height=getHeight();
g.setColor(color);
g.fillRect(x+width/2,y+(int)(height*.47), 20, 20);
Now what I would recommend, would be that you port these values into a Square object. Or, better yet, use the Rectangle object. If you went with the custom approach:
public class Square
{
public Square(int x, int y, int height, int width)
{
// Store these values in some fields.
}
public void paintComponent(Graphics g)
{
g.fillRect() // Your code for painting out squares.
}
}
Then, all you need to do, is call each object's paintComponent method in some list. Let's assume you have some List:
List<Square> squares = new ArrayList<Square>();
for(Square sq : squares)
{
sq.paintComponent(g);
}
this is the code so far. I know its nasty but its because i've been trying different things a none of them work. i really appreciate your help. thanks. #ChrisCooney
public class SquaresAnimation extends JFrame{
SquaresAnimation(){
add(new CrazySquares());
}
public static void main (String[] args){
SquaresAnimation frame = new SquaresAnimation();
frame.setTitle("AnimationDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 250);
frame.setVisible(true);
}
}
class Square{
private int x;
private int y;
public int height;
public int width;
Square(int x, int y, int height, int width)
{
// Store these values in some fields.
this.x=x;
this.y=y;
this.height=height;
this.width=width;
}
public void paintComponent(Graphics g)
{
g.setColor(Color.CYAN);
g.fillRect(x+width/2,y+(int)(height*.47), 20, 20);
}
}
class CrazySquares extends JPanel {
private final int numberOfRectangles=100;
Color color=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
private int x=1;
private int y=1;
Random random= new Random();
int randomNumber=1+(random.nextInt(4)-2);
Random rand= new Random();
int rando=1+(rand.nextInt(4)-2);
private Timer timer = new Timer(30, new TimerListener());
List<Square> squares = new ArrayList<Square>();
CrazySquares(Graphics g){
timer.start();
for(Square sq : squares){
sq.paintComponent(g);
}
}
}
//protected void paintComponent(Graphics g) {
//super.paintComponent(g);
// }
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
I've been trying to get this rectangle to move that I've created using a for loop. All that's happening with this code is that there is an original rectangle and then a new one next to that rectangle. No animation happens, only those two rectangles show on the window. What are some methods to get this rectangle to animate?
import java.awt.*;
import javax.swing.*;
public class Gunman extends JComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
public int x = 10;
public int y = 10;
public int width = 8;
public int height = 10;
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect (x, y, width, height);
g.fillRect (x, y, width, height);
for(int i = 0; i<=1024; i++){
g.setColor(Color.red);
g.drawRect(x++, y, width, height);
g.fillRect(x++, y, width, height);
}
}
}
Don't have program logic in a paint or paintComponent method, and by logic, I mean the for loop with "motion" as that just won't work. You want to
Almost never draw in a JComponent's paint method but rather in its paintComponent method.
Don't forget to call the super.paintComponent(g) method too, often as the first method call in the paintComponent(g) override.
Use a Swing Timer to step wise change the x and y values
call repaint() on the JComponent after the changes are made
For example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gunman extends JComponent {
private static final long serialVersionUID = 1L;
private static final int PREF_W = 900;
private static final int PREF_H = 700;
private static final int TIMER_DELAY = 30;
public int rectX = 10;
public int rectY = 10;
public int width = 8;
public int height = 10;
public Gunman() {
new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent actEvt) {
if (rectX < PREF_W && rectY < PREF_H) {
rectX++;
rectY++;
repaint();
} else {
((Timer)actEvt.getSource()).stop();
}
}
}).start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(rectX, rectY, width, height);
g.fillRect(rectX, rectY, width, height);
}
public int getRectX() {
return rectX;
}
public void setRectX(int rectX) {
this.rectX = rectX;
}
public int getRectY() {
return rectY;
}
public void setRectY(int rectY) {
this.rectY = rectY;
}
private static void createAndShowGui() {
Gunman mainPanel = new Gunman();
JFrame frame = new JFrame("Gunman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
There are numerous ways to animate. Here is another example. Notice the location of repaint() inside a background thread. This paints directly on a JFrame. Use paintComponent() when painting on JPanels.
public static void main(String args[]) throws Exception {
new JFrame("Draw a red box") {
Point pointStart = new Point(50,50);
Point pointEnd = new Point(200,200);
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) {
g.setColor(Color.RED);
g.drawRect(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}}{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 300);
setLocation(300, 300);
setVisible(true);
Thread t = new Thread(new Runnable() {
public void run() {
while (pointEnd.x > 0 && pointEnd.y > 0) {
pointEnd = new Point(--pointEnd.x, --pointEnd.y);
repaint();
try {
Thread.sleep(22);
} catch (InterruptedException e) {
e.printStackTrace();
}}
pointStart = null;
pointEnd = null;
}});
t.setDaemon(true);
t.start();
}};}
UPDATE: Ok previous answer was not so good from the old memory, here is the quickest, cheapest, most dirty way to get some animation quicksmart, you can copy and compile the code as is:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Test extends JFrame {
public Gunman g = new Gunman();
public static void main( String[] args ) {
Test t = new Test();
t.setSize( 800, 600 );
t.setVisible( true );
t.getContentPane().add( t.g );
while ( true ) {
t.g.x = t.g.x + 1;
t.g.y = t.g.y + 1;
t.repaint();
try {
Thread.sleep( 100 );
} catch ( InterruptedException e ) {
}
}
}
public void paintComponent( Graphics g ) {
g.clearRect( 0, 0, 800, 600 );
}
}
class Gunman extends JComponent {
private static final long serialVersionUID = 1L;
public int x = 10;
public int y = 10;
public int width = 8;
public int height = 10;
public void paintComponent( Graphics g ) {
g.setColor( Color.red );
g.fillRect( x, y, width, height );
}
}
There are ALOT of shortcuts in this, as Hovercraft of Eels has said, this is not an 'ideal' way to do it, but it has the basic structure. You have a canvas (I have used the JFrame, again not really recommended), and you add a component to it. You must override paintComponent (if you are using swing, which I do recommend you do), and this will draw your component.
You then need to alter your component's position in some way (recommend a proper method call on the object that does this), and ask the canvas to repaint itself.
I have included the wait so you can see what's happening, but if you are thinking of game programming, you should look into creating a game loop to manage this, I recommend Killer game programming in java, you can get a free ebook version with a quick google search.
I have a problem in my application using line primitives and JLables. I try to explain it:
I have to draw a vehicle route using lines to represent roads and JLabels to represent cities. I need the use of JLabels because each JLabel has a Listener that shows a dialog with information about the city.
I redefine paint() method of my main JPanel. In that method I first in invoke the super.paint(), then I draw the lines and finally I add the Labels to the JPanel.
The problem is that the lines overlap the labels regardless the matter the order of painting them. Is there any suggestion?
You can also override paintComponent() or paintChildren() methods of the JPanel.
In the paintChildren() call your lines drawing and then super to draw JLabels.
anothe way should be
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class AddVertexDemo {
public AddVertexDemo() {
}
private static void createAndShowUI() {
JFrame frame = new JFrame("AddVertexDemo");
frame.getContentPane().add(new Gui().getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowUI();
}
});
}
}
class DrawingPanel extends JPanel {
private static final int RADIUS = 6;
private static final long serialVersionUID = 1L;
private List<Shape> vertexList = new ArrayList<Shape>();
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (vertexList.size() > 1) {
Shape s0 = vertexList.get(0);
Shape s1 = null;
for (int i = 0; i < vertexList.size(); i++) {
s1 = vertexList.get(i);
drawConnectingLine(g, s0, s1);
s0 = s1;
}
s1 = vertexList.get(0);
//drawConnectingLine(g2, s0, s1);
}
for (Shape shape : vertexList) {
g2.setColor(Color.blue);
g2.fill(shape);
g2.setColor(Color.blue.darker().darker());
g2.draw(shape);
}
}
private void drawConnectingLine(Graphics g, Shape s0, Shape s1) {
Rectangle r0 = s0.getBounds();
Rectangle r1 = s1.getBounds();
int x0 = r0.x + r0.width / 2;
int y0 = r0.y + r0.height / 2;
int x1 = r1.x + r1.width / 2;
int y1 = r1.y + r1.height / 2;
g.drawLine(x0, y0, x1, y1);
}
public void addVertex(Point p) {
int x = p.x - RADIUS;
int y = p.y - RADIUS;
int w = 2 * RADIUS;
int h = w;
vertexList.add(new Ellipse2D.Double(x, y, w, h));
repaint();
}
public void removeVertex(Point p) {
if (vertexList.size() > 0) {
for (int i = vertexList.size() - 1; i >= 0; i--) {
if (vertexList.get(i).contains(p)) {
vertexList.remove(i);
repaint();
return;
}
}
}
}
}
class Gui {
private static final Dimension DRAWING_PANEL_SIZE = new Dimension(600, 500);
private JPanel mainPanel = new JPanel(new BorderLayout());
private DrawingPanel drawingPanel = new DrawingPanel();
private JToggleButton addVertexBtn = new JToggleButton("Add Vertex");
private JToggleButton removeVertexBtn = new JToggleButton("Remove Vertex");
Gui() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(addVertexBtn);
buttonPanel.add(removeVertexBtn);
DrawPanelMouseListener mouseListener = new DrawPanelMouseListener();
mouseListener.setDrawingPanel(drawingPanel);
mouseListener.setGui(this);
drawingPanel.addMouseListener(mouseListener);
drawingPanel.setPreferredSize(DRAWING_PANEL_SIZE);
drawingPanel.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.add(drawingPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
}
public JPanel getMainPanel() {
return mainPanel;
}
public boolean isAddingVertex() {
return addVertexBtn.isSelected();
}
public boolean isRemovingVertex() {
return removeVertexBtn.isSelected();
}
public void setAddingVertex(boolean addingVertex) {
addVertexBtn.setSelected(addingVertex);
}
public void setRemovingVertex(boolean removingVertex) {
removeVertexBtn.setSelected(removingVertex);
}
}
class DrawPanelMouseListener extends MouseAdapter {
private DrawingPanel drawingPanel;
private Gui gui;
public DrawPanelMouseListener() {
}
public void setGui(Gui gui) {
this.gui = gui;
}
public void setDrawingPanel(DrawingPanel drawingPanel) {
this.drawingPanel = drawingPanel;
}
#Override
public void mousePressed(MouseEvent me) {
if (gui.isAddingVertex() && gui.isRemovingVertex()) {
gui.setAddingVertex(false);
gui.setRemovingVertex(false);
return;
}
if (gui.isAddingVertex()) {
drawingPanel.addVertex(me.getPoint());
gui.setAddingVertex(false);
}
if (gui.isRemovingVertex()) {
drawingPanel.removeVertex(me.getPoint());
gui.setRemovingVertex(false);
}
}
}
I'm not sure that this is the right way to do this but you can try this:
Create 2 panels. One for drawing lines and another for drawing buildings(labels).
Add both panels in LayeredPane of JFrame. Add panel with line in lower layer then panel with labels.
You can use LayeredPanes in other ways also to solve your problem. Learn more here: How to use Layered Panes in java