java - JPanel Possibly Overlapping? -
so have problem code. whenever load game there red square in center of screen, , have not programmed so. have tried find error hours can't see it. think has panels or something. second thing when press button draw grid, small line appears. programmed bigger is, , not in right location either. below code, , appreciated!!
package com.thedevcorner; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jmenubar; import javax.swing.jpanel; public class game extends jpanel implements actionlistener { public static jbutton grid = new jbutton("show grid"); public static jpanel drawarea = new jpanel(); public static jmenubar menu = new jmenubar(); public static jpanel notdrawn = new jpanel(); public static boolean gridpressed = false; public game() { grid.addactionlistener(this); } public static void main(string args[]) { game game = new game(); jframe frame = new jframe(); frame.setvisible(true); frame.setsize(new dimension( toolkit.getdefaulttoolkit().getscreensize().width, toolkit .getdefaulttoolkit().getscreensize().height)); frame.settitle("game"); frame.setalwaysontop(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); menu.setsize(new dimension(1600, 20)); menu.setlocation(0, 0); notdrawn.setbackground(new color(255, 0, 50)); notdrawn.setsize(100, 900); notdrawn.add(grid); notdrawn.setlayout(null); grid.setsize(new dimension(100, 25)); grid.setlocation(0, 25); drawarea.setsize(new dimension((toolkit.getdefaulttoolkit() .getscreensize().width), toolkit.getdefaulttoolkit() .getscreensize().height)); drawarea.setlocation(100, 0); drawarea.setbackground(color.black); drawarea.add(menu); drawarea.add(game); frame.add(drawarea); frame.add(notdrawn); } public void paint(graphics g) { game game = new game(); if (gridpressed) { game.drawgrid(0, 0, g); } g.dispose(); repaint(); } public static void drawgrid(int x, int y, graphics g) { g.setcolor(color.white); g.drawline(x, y, 50, 300); } @override public void actionperformed(actionevent e) { if (e.getsource() == grid && gridpressed == true) { gridpressed = false; system.out.println("unpressed"); } if (e.getsource() == grid) { gridpressed = true; system.out.println("pressed"); } } }
there number of problems...
the red "square" seeing grid
button. reason it's red because of paint
method.
graphics
shared resource, is, each component painted on screen shares same graphics
context. because chose dispose
of context , because you've failed honor paint chain, you've screwed up.
don't ever dispose of graphics
context didn't create. prevent being painted again. call super.paintxxx
. paint chain complex , lot of important work. if you're going ignore it, ready have re-implement it.
null
layouts vary right choice, when you're laying out components. need separate components custom painting, otherwise components appear above custom painting.
this frame.setsize(new dimension(toolkit.getdefaulttoolkit().getscreensize().width, toolkit.getdefaulttoolkit().getscreensize().height))
not way maximize window. not take consideration possibility of things tasks bars. instead use frame#setextendedstate
as andreas has commented, actionperformed
logic wrong. should using if-statement
or flipping boolean
logic...
updated simple example
ps- static
not friend here...
import java.awt.borderlayout; import java.awt.color; import java.awt.graphics; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class game extends jpanel implements actionlistener { private gridpane gridpane; public game() { setlayout(new borderlayout()); sidebarpane sidebar = new sidebarpane(); sidebar.addactionlistener(this); add(sidebar, borderlayout.west); gridpane = new gridpane(); add(gridpane); } public static void main(string args[]) { game game = new game(); jframe frame = new jframe(); frame.setvisible(true); frame.setextendedstate(jframe.maximized_both); frame.settitle("game"); frame.setalwaysontop(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(game); } @override public void actionperformed(actionevent e) { if (e.getactioncommand().equalsignorecase("grid")) { gridpane.setgridon(!gridpane.isgridon()); } } public class gridpane extends jpanel { private boolean gridon = false; public gridpane() { setbackground(color.black); } public boolean isgridon() { return gridon; } public void setgridon(boolean value) { if (value != gridon) { this.gridon = value; repaint(); } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (gridon) { g.setcolor(color.white); g.drawline(0, 0, 50, 300); } } } public class sidebarpane extends jpanel { public jbutton grid; public sidebarpane() { setbackground(new color(255, 0, 50)); setlayout(new gridbaglayout()); grid = new jbutton("show grid"); grid.setactioncommand("grid"); gridbagconstraints gbc = new gridbagconstraints(); gbc.anchor = gridbagconstraints.north; gbc.weighty = 1; add(grid, gbc); } public void addactionlistener(actionlistener listener) { grid.addactionlistener(listener); } } }
Comments
Post a Comment